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

Bonfire: Chunky Monkey

The Chunky Monkey Bonfire took me about 40 minutes to solve. It was a real tough one and I had to Google to find some help.  I used the .push() method and the .slice() method. Came across Wulkan’s blog in my search and his solution to the challenge really helped me find mine. Thanks Wulkan!

The Challenge:

Bonfire: Chunky Monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a multidimensional array.

For example:
chunk(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]]
chunk([0, 1, 2, 3, 4, 5], 3)
 should return [[0, 1, 2], [3, 4, 5]]


The Solution:

function chunk(arr, size) {

  var newArr = [];
  for (i = 0; i < arr.length;) {
    newArr.push(arr.slice(i, i += size));
  }
  return newArr;
 
}

chunk(["a", "b", "c", "d"], 2, "");

I was having trouble with controlling the incrementation in the ‘for loop’ so I just took out the ‘i++’ and it worked!

Bonfire: Truncate a string

Using .splice() to solve FreeCodeCamp‘s Bonfire: Truncate a string.

The Challenge:

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a “…” ending.

Note that the three dots at the end add to the string length.

Try it yourself before viewing my solution! If you use Google Chrome as your browser, you can right click and Inspect Element to use the console to test your code.


The Solution:

function truncate(str, num) {

  if (str.length > num) {
    return str.slice(0, (num-3)) + "...";
  } else {
    return str;
  }
}

truncate("A-tisket a-tasket A green and yellow basket", 11, "");
truncate("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length);

Bonfire: Repeat a String Repeat a String

Three in a row! Are these challenges easier than the first Bonfires or is all this training really starting to improve my programming brain? (I hope it’s the latter!)

This is my solution to FreeCodeCamp‘s Bonfire: Repeat a string repeat a string. This one was much easier to complete as you only need to check if the num argument is less than zero.

The Challenge:

Repeat a given string (first argument) n times (second argument). Return an empty string if n is a negative number.

The Solution:

function repeat(str, num) {

  if (num < 0) {
    return "";
  } else {
    return str.repeat(num);
  }
}

repeat("*", 3, "");
repeat("abc", 3, "");

Bonfire: Confirm the Ending

On a roll! Just did two FreeCodeCamp Bonfire Algorithms back-to-back. This one took me about 20 minutes. Use .substr() method.

The Challenge:

Check if a string (first argument) ends with the given target string (second argument).

The Solution:

function end(str, target) {
  var lastLetter = str.substr(-(target.length), target.length);
  if (target === lastLetter) {
    return true;
  } else {
    return false;
  }
}
end("He has to give me a new name", "name")
end("Bastian", "n", "");

Use console.log(lastLetter); after defining the variable lastLetter to see what is printing. It really helps to see what is going wrong with your code.

Bonfire: Return Largest Numbers in Arrays

Took me about an hour, but I finally passed the Bonfire: Return Largest Numbers in Arrays from FreeCodeCamp’s Algorithm section. Whoohoo!

The Challenge:

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

The Solution:

function largestOfFour(arr) {

  var newArray = [];
  var x = 0;
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (x < arr[i][j]) {
        x = arr[i][j];       
      }
    }
   newArray.push(x);
    x = 0;
  }
  return newArray;
}


largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

There is a much easier way, by using the math.max.apply method, but I didn’t choose to use it.

JavaScript String Manipulation Challenge

Going through Udacity‘s free online JavaScript course, I came across this challenge:

Your Challenge

If given a string of a two word name formatted with any mix of capitalization, can you manipulate the string to ensure the first name has a capital first letter and the last name is totally capitalized? Assume there’s a space between the names. For instance, turning a string like "cAmEROn PittMAN" into "Cameron PITTMAN". Your answer should be a single string saved to the variable called finalName.

Take a look at MDN’s documentation on string methods for clues, then try it yourself!

==============================================

My solution:

Unnamed QQ Screenshot20151001081020

The breakdown:

Line 1: var name = "AlbERt EINstEiN";

A variable called name has the value AlbErt EINstEiN. All lines in JS have to end with a semicolon so that the system knows it is the end of a line.

Line 3: function nameChanger(oldName) {

A function called nameChanger will take in an argument called oldName. All functions will start with a curly brace after the function name.

Line 4: var finalName = oldName;

A variable called finalName has the value of oldName.

Line 6:  var nameArray = oldName.split(' ');

The value of oldName is taken and a .split() method is called to make a string array from oldName. The array values are created using a ‘ ‘ (space) as the separator. For example, if
oldName = "AlbERt EINstEiN";
oldName.split(' ');
would equal have the values: [AlbERt', 'EINstEiN']

Then these values would be saved in a new variable called nameArray.

Line 7: var lastName = nameArray[1].toUpperCase();

The second value (since arrays start with 0, nameArray[1] would be the second value) in nameArray is changed to all uppercase letters. This is stored in a new variable called lastName. For example, if
nameArray = ['AlbERt', 'EINstEiN']
nameArray[0] = ['AlbERt']
nameArray[1] = ['EINstEiN']
nameArray[1].toUpperCase();
would be equal to ['EINSTEIN']
var lastName = ['EINSTEIN']

Line 8: var firstName = nameArray[0].toLowerCase().split('');

The first value in nameArray is changed to all lowercase letters. Then the letters will be separated from each other since the .split(”) is an empty string. So if
nameArray[0] = ['AlbERt'],
firstName = ['a', 'l', 'b', 'e', 'r', 't'];

Line 9: var firstLetter = firstName.splice(1).join('');

firstName will start the array from the second value (1) and then the array will be joined together to form one string. For example:
firstName = ['a', 'l', 'b', 'e', 'r', 't'];
firstName.splice(1) = ['l', 'b', 'e', 'r', 't'];
firstLetter = ['l', 'b', 'e', 'r', 't'].join('') = ['lbert'];

.splice() method
.join() method

Line 10: var firstNameJoin = firstName[0].toUpperCase() + firstLetter;

The first value in the array firstName is changed to uppercase, then added to the value of firstLetter.
firstName = ['a', 'l', 'b', 'e', 'r', 't'];
firstName[0] = ['a'];
firstName[0].toUpperCase() = ['A']
firstNameJoin = ['A'] + ['lbert'] = ['Albert'];

Line 12: finalName = firstNameJoin + ' ' + lastName;

finalName = [‘Albert’] + [‘ ‘] + [‘EINSTEIN’] = [‘Albert EINSTEIN’];

Line 14: return finalName;

Saves the value of finalName.

Line 18: console.log(nameChanger(name));

'console.log' will show on the screen whatever is in the parentheses. Inside the parentheses, the function we made, nameChanger, is called with a parameter inserted called ‘name‘. ‘name‘ is the name of the variable that was defined in the beginning on Line 1.

There are many other ways to find a workable solution, so don’t think you HAVE to do it this way.