-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers.rb
More file actions
179 lines (166 loc) · 4.69 KB
/
Copy pathnumbers.rb
File metadata and controls
179 lines (166 loc) · 4.69 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# coding: utf-8
require 'test/unit'
SMALL = %w[noll ett två tre fyra fem sex sju åtta nio tio elva tolv].freeze
def to_string(nr, genus = 'ett')
{
miljard: 1_000_000_000,
miljon: 1_000_000,
tusen: 1_000
}.each do |word, scale|
if nr >= scale && nr % scale == 0
return to_string(nr / scale, word == :tusen ? 'ett' : 'en') +
word.to_s + (word == :tusen || nr / scale % 10 == 1 ? '' : 'er')
end
end
if (100...1000).cover?(nr) && nr % 100 == 0
return to_string(nr / 100) + 'hundra'
end
case nr
when 1 then genus
when 0..12 then SMALL[nr]
when 13, 17, 19 then to_string(nr - 10).chomp('o') + 'tton'
when 14 then 'fjorton'
when 15, 16 then to_string(nr - 10) + 'ton'
when 18 then 'arton'
when 20 then 'tjugo'
when 40, 50, 60, 80 then to_string(nr / 10).chomp('a').chomp('t') + 'tio'
when 30, 70, 90 then to_string(nr / 10).chomp('a').chomp('o') + 'ttio'
when 21..99 then split(nr, 10)
when 101..999 then split(nr, 100)
when 1001..999_999 then split(nr, 1000)
when 1_000_001..999_999_999 then split(nr, 1_000_000)
when 1_000_000_001..999_999_999_999 then split(nr, 1_000_000_000)
end
end
def split(nr, scale)
to_string(nr / scale * scale) + to_string(nr % scale)
end
TABLE = {
fjorton: 14,
arton: 18,
nitton: 19,
tjugo: 20,
fyrtio: 40,
åttio: 80,
nittio: 90,
tusen: 1000,
miljon: 1_000_000,
miljard: 1_000_000_000
}.freeze
def to_number(word)
result = []
last_sum = chunkify(word.gsub(/ +/, '')).reduce(0) do |sum, chunk|
case chunk
when *SMALL
sum + SMALL.index(chunk)
when 'en', 'ett'
sum + 1
when /(tre|fem|sex|sju)t?ton/
sum + 10 + to_number($1)
when /(fjor|ar|nit)ton|tjugo|(fyr|åt|nit)tio/
sum + TABLE[chunk.to_sym]
when /(tre|fem|sex|sju)t?tio/
sum + to_number($1) * 10
when 'hundra'
[sum, 1].max * 100
when /(tusen|miljon|miljard)/
# Handle "tusen miljoner", "tusen miljarder", etc.
if (1000...1_000_000).cover?(result.last) && chunk.start_with?('m')
sum += result.last
result = result[0..-2]
end
# Save what we have so far...
result << [sum, 1].max * TABLE[$1.to_sym]
# ...and start on the next part.
0
else raise RuntimeError, chunk
end
end
result << last_sum
result.reduce(:+)
end
PARTS = %w[(?:tret fjor fem sex sjut ar nit)ton
tjugo
(?:tret fyr fem sex sjut åt nit)tio
hundra
tusen
milj(?:on ard)(?:er)?
en] + SMALL
def chunkify(word)
word.scan(/#{PARTS.join('|')}/)
end
class NumbersTest < Test::Unit::TestCase
def test_a_few
check 'noll', 0
check 'ett', 1
check 'två', 2
check 'tre', 3
check 'fyra', 4
check 'fem', 5
check 'sex', 6
check 'sju', 7
check 'åtta', 8
check 'nio', 9
check 'tio', 10
check 'elva', 11
check 'tolv', 12
check 'tretton', 13
check 'fjorton', 14
check 'femton', 15
check 'sexton', 16
check 'sjutton', 17
check 'arton', 18
check 'nitton', 19
check 'tjugo', 20
check 'tjugoett', 21
check 'tjugotvå', 22
check 'tjugotre', 23
check 'trettio', 30
check 'trettioett', 31
check 'fyrtio', 40
check 'fyrtiofyra', 44
check 'femtiofem', 55
check 'sextiosex', 66
check 'sjuttiosju', 77
check 'åttioåtta', 88
check 'nittionio', 99
check 'etthundra', 100
check 'etthundraett', 101
check 'etthundrafemtiosju', 157
check 'tvåhundra', 200
check 'tvåhundratio', 210
check 'sjuhundratrettiotvå', 732
check 'etttusen', 1000
check 'etttusentvåhundraåttioåtta', 1288
check 'tiotusen', 10_000
check 'tolvtusenfemhundrafyra', 12_504
check 'nittioåttatusensjuhundrasextiofem', 98_765
check 'enmiljon', 1_000_000
check 'tvåmiljonertrettiotusensjuhundra', 2_030_700
check 'enmiljard', 1_000_000_000
check 'etthundratjugotre miljarder fyrahundrafemtiosex miljoner ' \
'sjuhundraåttionio tusen etthundratjugotre'.delete(' '),
123_456_789_123
end
def test_difficult
assert_equal 21, to_number('tjugoett')
assert_equal 21, to_number('tjugoen')
assert_equal 100, to_number('hundra')
assert_equal 150, to_number('hundrafemtio')
assert_equal 150_000_000_000, to_number('hundrafemtio miljarder')
assert_equal 1000, to_number('tusen')
assert_equal 2_000_000_000, to_number('två tusen miljoner')
end
def test_many
nr = 1
while nr < 1_000_000_000_000
# p [nr, chunkify(to_string(nr)).join(' ')]
assert_equal nr, to_number(to_string(nr))
nr = nr * 18 / 17 + 1
end
end
def check(word, nr)
assert_equal word, to_string(nr)
assert_equal nr, to_number(word)
end
end