Hackerrank Day 1: Quartiles


My Solution in Ruby:

It was helpful to sketch this out on paper first.  Lots of math to confuse myself with.

I don’t think this is the most optimized solution, but it’s pretty easy to follow, at least IMO.

Screen Shot 2016-12-29 at 4.49.35 PM.png

Note to self:  

array.delete_at(n)  only returns the deleted value, NOT the array without the deleted value.

Hackerrank Day 1: Quartiles

Hackerrank Day 0: Means, Median, Mode


My Solution in Ruby: 


This was a good review on arrays and hashes.

  • Find the sum of all elements in an array with: array.inject(0){|sum, x| sum + x}
  • Store number of occurrences in hashes so you can retrieve key-value pairs based on its value or key attributes.
  • String can be converted to array with .split(‘ ‘).map(&:to_i).
Hackerrank Day 0: Means, Median, Mode

Hackerrank Day 7: Arrays


Given an array, A, of N integers, print ‘s elements in reverse order as a single line of space-separated numbers.

Input Format

The first line contains an integer, N (the size of our array).
The second line contains N space-separated integers describing array A‘s elements.

Output Format

Print the elements of array in reverse order as a single line of space-separated numbers.

Sample Input

1 4 3 2

Sample Output

2 3 4 1

My solution in Ruby:  


arr = arr.split(' ').map(&:to_i)

can also be written as:

arr_strings = arr.split(' ') 
arr = arr_strings.map {|n| n.to_i}

A couple other things to remember:

  • .strip removes any unnecessary characters from a string to let you focus on the important ones.  (It does not work on integers/fixnums)
  • splitting a string with space will convert it into an array
  • &: is a shorthand for calling a function on an enumerator.  For example, we can only use .to_i on one value.  If we want to .to_i multiple values (like in an array), we have to use .map (&:to_i) instead.
Hackerrank Day 7: Arrays

Rectangle Intersection

Write a function, `rec_intersection(rect1, rect2)` and returns the intersection of the two.

Rectangles are represented as a pair of coordinate-pairs: the bottom-left and top-right coordinates (given in `[x, y]` notation).

Hint: You can calculate the left-most x coordinate of the intersection by taking the maximum of the left-most x coordinate of each rectangle. Likewise, you can calculate the top-most y coordinate of the intersection by taking the minimum of the top most y coordinate of each rectangle.

This is probably the hardest problem I’ve encountered do date.  It’s a bit more logic than math and I definitely overcomplicated it by trying to make sense of it with math, which is not my strength..

Some helpful things to remember:

  • Plot out the coordinates and draw the rectangles on a piece of paper first.
  • Try not to confuse the (x,y) coordinates with arrays.  (This part tripped me up the most).
  • The trick is to use max and min to get the left and right coordinates of the intersection, respectively.   (This is a bit counterintuitive).
  • To access a value of a multidimensional array, use a[i][j], where i is the index of a, and j is the index of a[i] array.

Anyways, here’s the solution with my annotated comments:


Test Cases: 

Screen Shot 2016-11-30 at 2.37.47 PM.png

Rectangle Intersection

Word Unscrambler

Write a function word_unscrambler that takes two inputs: a scrambled word and a dictionary of real words. Your program must then output all words that our scrambled word can unscramble to.

Hint: To see if a string `s1` is an anagram of `s2`, split both strings into arrays of letters. Sort the two arrays. If they are equal, then they are anagrams.

My solution: 


Provided solution: 


Test Cases: 



Word Unscrambler