Hackerrank Day 1: Interquartile Range

screen-shot-2016-12-29-at-10-31-35-pm

screen-shot-2016-12-29-at-10-30-50-pm

My Solution in Ruby:

screen-shot-2016-12-29-at-10-29-31-pm

We can also store the elements and their corresponding frequencies in their own hash, which are stored in an array, and sort the array by the value of the element in the hashes, like so:

screen-shot-2016-12-29-at-11-28-28-pm

  • It’s possible to store hashes within an array.
  • sort! modifies current existing array.  sort creates a new sorted array.
    • array.sort!{ |x, y| x <=> y }  sorts current array in ascending order.
    • array.sort!{ |x, y| y <=> x }  sorts current array in descending order.
  • Array.new that takes two arguments (n, element) returns that element n times.
    • Array.new(3, true) #=> [true, true, true]

 

Advertisements
Hackerrank Day 1: Interquartile Range

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s