-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_number.rb
67 lines (52 loc) · 1.29 KB
/
human_number.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
class HumanNumber
def initialize(str)
@str = str
end
def process
if @str.length < 4
@str
else
elements = @str.split("")
new_elements, idx = [], 0
while(true) do
new_elements.unshift(elements.pop)
break if elements.length == 0
idx += 1
if idx % 3 == 0 &&
new_elements.unshift(",")
end
end
new_elements.join("")
end
end
end
require 'minitest/autorun'
class HumanNumberTest < Minitest::Test
def test_process_1
assert_equal "", HumanNumber.new("").process
end
def test_process_2
assert_equal "351", HumanNumber.new("351").process
end
def test_process_3
assert_equal "3,512", HumanNumber.new("3512").process
end
def test_process_4
assert_equal "35,128", HumanNumber.new("35128").process
end
def test_process_5
assert_equal "351,212", HumanNumber.new("351212").process
end
def test_process_6
assert_equal "1,234,351", HumanNumber.new("1234351").process
end
def test_process_7
assert_equal "12,343,519", HumanNumber.new("12343519").process
end
def test_process_8
assert_equal "123,435,190", HumanNumber.new("123435190").process
end
def test_process_9
assert_equal "1,234,351,902", HumanNumber.new("1234351902").process
end
end