Ruby Algorithm Interview Challenge

I recently had a Junior Ruby Developer interview where I had to complete a coding challenge live. I didn’t finish in time and didn’t get the position, but I wanted to complete it anyway.

The Challenge:

Convert numbers into their word equivalent.

ie. 1 = “one”, 23 = “twenty three”, 1234 = “one thousand two hundred thirty four”


My Solution:

def convert_num(num)
	ones = {
		'1' => "one",
		'2' => "two",
		'3' => "three",
		'4' => "four", 
		'5' => "five",
		'6' => "six",
		'7' => "seven",
		'8' => "eight",
		'9' => "nine"
	}

	teens = {
		'11' => "eleven",
		'12' => "twelve",
		'13' => "thirteen",
		'14' => "fourteen",
		'15' => "fifteen",
		'16' => "sixteen",
		'17' => "seventeen",
		'18' => "eighteen",
		'19' => "nineteen"
	}

	tens = {
		'10' => "ten",
		'20' => "twenty",
		'30' => "thirty",
		'40' => "forty",
		'50' => "fifty",
		'60' => "sixty",
		'70' => "seventy",
		'80' => "eighty",
		'90' => 'ninety'
	}

	hundreds = {
		'100' => "one hundred",
		'1000' => "one thousand"
	}

big_num = []
#first check ones place
	string_num = num.to_s
	if ones.include?(string_num)
		ones[string_num]
#check teens
	elsif teens.include?(string_num)
		teens[string_num]
#check tens
	elsif tens.include?(string_num)
		tens[string_num]
#check hundreds
	elsif hundreds.include?(string_num)
		hundreds[string_num]
#split the number up 
	else
		num_array = string_num.split('')
		if num_array.length == 4
			big_num << ones[num_array[0]] + " thousand"
			big_num << ones[num_array[1]] + " hundred"
			big_num << tens[num_array[2] + '0']
			big_num << ones[num_array[3]]
		elsif num_array.length == 3
			big_num << ones[num_array[0]] + " hundred"
			big_num << tens[num_array[1] + '0']
			big_num << ones[num_array[2]]
		else
			big_num << tens[num_array[0] + '0']
			big_num << ones[num_array[1]]
		end
		big_num.join(" ")
	end
	
end

How I approached this challenge (my thinking process):
I was told that the highest number passed into this algorithm would be 9,999

  1. First I knew I needed to create a list of all the numbers and their written equivalent. Since the keys would not match an array index sequence, I decided to store the information in a hash.
  2. The ones place is its own hash because I knew I would need to use this hash again when dealing with hundreds and thousands.
  3. I placed the teens in their own hash because there’s no pattern to the written form and would only be used if one if the numbers in the teens hash appeared.
  4. Same goes for the tens. Twenty is different from two and thirty is different from three. So they get their own hash as well.
  5. If the number being passed matches exactly with any of the keys in these hashes, then I just need to use that key to grab the value and output it.
  6. If the number being passed does NOT match with any of the keys in my hashes, then I want to split the number up and search through the array created.
  7. If the length of the array is 4 (has 4 digits), then I know I need to add the word “thousand” after the first index (0), and “hundred” after the second index (1). I push these in order into a new array called big_num.
  8. The tens place is tricky as the numbers are all single digits. I add a ‘0’ to the end of the digit and search my tens hash for the matching key.
  9. I follow this same pattern for numbers that have a length of 3 and numbers with a length of 2.
  10. Then I join the elements in big_num together so that they form one string.

The following tests and outputs:

p convert_num(3)
  => "three"
p convert_num(15)
  => "fifteen"
p convert_num(90)
  => "ninety"
p convert_num(45)
  => "forty five"
p convert_num(202)
  => "two hundred two"
p convert_num(100)
  => "one hundred"
p convert_num(1200)
  => "one thousand two hundred"
p convert_num(9999)
  => "nine thousand nine hundred ninety nine"

Pretty sure that there can be some more refactoring, but will have to keep working through the logic to find a way to simplify it.

Advertisements

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s