-
Notifications
You must be signed in to change notification settings - Fork 0
/
017.rb
134 lines (124 loc) · 2.21 KB
/
017.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# If the numbers 1 to 5 are written out in words: one, two, three, four, five,
# then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
#
# If all the numbers from 1 to 1000 (one thousand) inclusive were written out
# in words, how many letters would be used?
#
#
# NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
# forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
# letters. The use of "and" when writing out numbers is in compliance with British usage.
beginning = Time.now
def number_letter_count max
@count = 0
(1..max).each do |num|
digits = num.to_s.length
organize_by_digits(digits, num)
end
@count
end
def organize_by_digits digits, num
case digits
when 4
@count += "onethousand".length
when 3
three_digits(num)
when 2
two_digits(num)
when 1
one_digit(num)
end
end
def three_digits num
num = num.to_s
@count += one_digit_to_word(num[0].to_i).length + "hundred".length
unless num[1,2] == "00"
@count += "and".length
if num[1] == "0"
one_digit(num[2].to_i)
else
two_digits(num[1,2].to_i)
end
end
end
def two_digits num
if num >= 20
num = num.to_s
@count += two_digits_to_words(num[0].to_i).length + one_digit_to_word(num[1].to_i).length
else
@count += special_two_digits_to_words(num).length
end
end
def one_digit num
@count += one_digit_to_word(num).length
end
def one_digit_to_word num
case num
when 0
""
when 1
"one"
when 2
"two"
when 3
"three"
when 4
"four"
when 5
"five"
when 6
"six"
when 7
"seven"
when 8
"eight"
when 9
"nine"
end
end
def two_digits_to_words num
case num
when 2
"twenty"
when 3
"thirty"
when 4
"forty"
when 5
"fifty"
when 6
"sixty"
when 7
"seventy"
when 8
"eighty"
when 9
"ninety"
end
end
def special_two_digits_to_words num
case num
when 10
"ten"
when 11
"eleven"
when 12
"twelve"
when 13
"thirteen"
when 14
"fourteen"
when 15
"fifteen"
when 16
"sixteen"
when 17
"seventeen"
when 18
"eighteen"
when 19
"nineteen"
end
end
puts number_letter_count(1000)
puts "Time elapsed #{Time.now - beginning} seconds."