-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ninety-Nine-Thousand.rb
82 lines (63 loc) · 1.81 KB
/
Ninety-Nine-Thousand.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#Ninety Nine Thousand Nine Hundred Ninety Nine - 5kyu
#Write a method that takes a number and returns a string of that number in English.
#For example
#number_to_english(27) # => 'twenty seven'
#Your method should be able to handle any number between 0 and 99999.
#If given numbers outside of that range or non-Integer numbers, the method should
#return an empty string.
def number_to_english(n)
numbers_to_name = {
1000 => "thousand",
100 => "hundred",
90 => "ninety",
80 => "eighty",
70 => "seventy",
60 => "sixty",
50 => "fifty",
40 => "forty",
30 => "thirty",
20 => "twenty",
19=>"nineteen",
18=>"eighteen",
17=>"seventeen",
16=>"sixteen",
15=>"fifteen",
14=>"fourteen",
13=>"thirteen",
12=>"twelve",
11 => "eleven",
10 => "ten",
9 => "nine",
8 => "eight",
7 => "seven",
6 => "six",
5 => "five",
4 => "four",
3 => "three",
2 => "two",
1 => "one",
0 => "zero"
}
str = ""
numbers_to_name.each do |num, name|
if n == 0
return str
elsif n > 99999
return str
elsif n.to_s.length == 1 && n/num > 0
return "#{name}"
elsif n < 100 && n/num > 0
return "#{name}" if n%num == 0
return "#{name} " + number_to_english(n%num)
elsif n/num > 0
return number_to_english(n/num) + " #{name} " + number_to_english(n%num)
end
end
end
puts number_to_english(4) == "four"
puts number_to_english(27) == "twenty seven"
puts number_to_english(102) == "one hundred two"
puts number_to_english(38_079) == "thirty eight thousand seventy nine"
puts number_to_english(82102713) == "eighty two million one hundred two thousand seven hundred thirteen"
puts number_to_english(6000)
puts number_to_english(0)