-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar_count.rb
More file actions
80 lines (72 loc) · 1.35 KB
/
char_count.rb
File metadata and controls
80 lines (72 loc) · 1.35 KB
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
def size(x)
singles = [
[/10$/, 'ten'],
[/11$/, 'eleven'],
[/12$/, 'twelve'],
[/13$/, 'thirteen'],
[/14$/, 'fourteen'],
[/15$/, 'fifteen'],
[/16$/, 'sixteen'],
[/17$/, 'seventeen'],
[/18$/, 'eighteen'],
[/19$/, 'nineteen'],
[/1$/, 'one'],
[/2$/, 'two'],
[/3$/, 'three'],
[/4$/, 'four'],
[/5$/, 'five'],
[/6$/, 'six'],
[/7$/, 'seven'],
[/8$/, 'eight'],
[/9$/, 'nine'],
[/1000$/, 'one thousand']
]
doubles = {
/2.$/ => 'twenty',
/3.$/ => 'thirty',
/4.$/ => 'forty',
/5.$/ => 'fifty',
/6.$/ => 'sixty',
/7.$/ => 'seventy',
/8.$/ => 'eighty',
/9.$/ => 'ninety'
}
anders = {
/1..$/ => 'one hundred',
/2..$/ => 'two hundred',
/3..$/ => 'three hundred',
/4..$/ => 'four hundred',
/5..$/ => 'five hundred',
/6..$/ => 'six hundred',
/7..$/ => 'seven hundred',
/8..$/ => 'eight hundred',
/9..$/ => 'nine hundred'
}
v = x.to_s
o = ''
singles.each do |r, s|
if v =~ r
o += s
break
end
end
doubles.each do |r, s|
if v =~ r
o += s
break
end
end
anders.each do |r, s|
if v =~ r
if o == ''
o += s
else
o += s
o += 'and'
end
end
end
puts o
o.gsub(/ /, '').size
end
(1..1000).map{|a| size(a)}.inject{|b, c| b + c}