Last active
December 22, 2015 04:28
-
-
Save kmazanec/6416907 to your computer and use it in GitHub Desktop.
Portfolio 1: Methods and Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create a dictionary of english words mapped to number values | |
# Take in a number | |
# Start with the largest value in the dictionary, see if the number is evenly divisible by it | |
# IF it is, then add that word to the method called on the number with the value of the word divided out | |
# Return the string | |
WORD_MAP = { "Ninety" => 90, | |
"Eighty" => 80, | |
"Seventy" => 70, | |
"Sixty" => 60, | |
"Fifty" => 50, | |
"Fourty" => 40, | |
"Thirty" => 30, | |
"Twenty" => 20, | |
"Fifteen" => 15, | |
"Thirteen" => 13, | |
"Twelve" => 12, | |
"Eleven" => 11, | |
"Ten" => 10, | |
"Nine" => 9, | |
"Eight" => 8, | |
"Seven" => 7, | |
"Six" => 6, | |
"Five" => 5, | |
"Four" => 4, | |
"Three" => 3, | |
"Two" => 2, | |
"One" => 1 } | |
def in_words(num) | |
english_num = "" | |
if num >= 100 | |
english_num = in_words(num / 100) + "Hundred" | |
num %= 100 | |
elsif (num >= 16 && num <= 19) || num == 14 || num == 15 | |
return in_words(num - 10) + "teen" | |
end | |
WORD_MAP.each_pair do |word, digit| | |
if num > 0 && num >= digit | |
english_num << word | |
num -= digit | |
end | |
end | |
english_num | |
end | |
puts "10 in words should be Ten: #{ in_words(10) }" | |
puts "2 in words should be Two: #{ in_words(2) }" | |
puts "8 in words should be Eight: #{ in_words(8) }" | |
puts "18 in words should be Eightteen: #{ in_words(18) }" | |
puts "28 in words should be TwentyEight: #{ in_words(28) }" | |
puts "53 in words should be FiftyThree: #{ in_words(53) }" | |
puts "81 in words should be EightyOne: #{ in_words(81) }" | |
puts "100 in words should be OneHundred: #{ in_words(100) }" | |
puts "101 in words should be OneHundredOne: #{ in_words(101) }" | |
puts "222 in words should be TwoHundredTwentyTwo: #{ in_words(222) }" | |
puts "1776 in words should be SeventeenHundredSeventySix: #{ in_words(1776) }" | |
puts "21776 in words should be TwentyOneThousandSevenHundredSeventySix: #{ in_words(21776) }" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment