Skip to content

Commit 5a68918

Browse files
committed
add solution interger to english words
1 parent 5eeaff3 commit 5a68918

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

go/integer-to-english-words.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package leetcode
2+
3+
var (
4+
nwTens = []string{"Zero", "Ten", "Twenty", "Thirty", "Forty",
5+
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
6+
nwBasic = []string{"Zero", "One", "Two", "Three", "Four",
7+
"Five", "Six", "Seven", "Eight", "Nine", "Ten",
8+
"Eleven", "Twelve", "Thirteen", "Fourteen",
9+
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
10+
)
11+
12+
func simpleNumberToWords(num int) string {
13+
hundred := 0
14+
result := ""
15+
if num > 99 {
16+
hundred = num / 100
17+
result = nwBasic[hundred] + " " + "Hundred"
18+
}
19+
num = num - hundred*100
20+
if num > 19 {
21+
tens := num / 10
22+
left := num % 10
23+
if left != 0 {
24+
if len(result) != 0 {
25+
result = result + " " + nwTens[tens] + " " + nwBasic[left]
26+
} else {
27+
result = nwTens[tens] + " " + nwBasic[left]
28+
}
29+
} else {
30+
if len(result) != 0 {
31+
result = result + " " + nwTens[tens]
32+
} else {
33+
result = nwTens[tens]
34+
}
35+
}
36+
} else {
37+
if num != 0 {
38+
if result != "" {
39+
result = result + " " + nwBasic[num]
40+
} else {
41+
result = nwBasic[num]
42+
}
43+
44+
}
45+
}
46+
47+
return result
48+
}
49+
50+
func numberToWords(num int) string {
51+
if num == 0 {
52+
return "Zero"
53+
}
54+
result := ""
55+
if num > (1000000000 - 1) {
56+
current := num / 1000000000
57+
result = simpleNumberToWords(current) + " " + "Billion"
58+
num = num % 1000000000
59+
}
60+
if num > (1000000 - 1) {
61+
current := num / 1000000
62+
if result != "" {
63+
result = result + " " + simpleNumberToWords(current) + " " + "Million"
64+
} else {
65+
result = simpleNumberToWords(current) + " " + "Million"
66+
}
67+
68+
num = num % 1000000
69+
}
70+
if num > (1000 - 1) {
71+
current := num / 1000
72+
if result != "" {
73+
result = result + " " + simpleNumberToWords(current) + " " + "Thousand"
74+
} else {
75+
result = simpleNumberToWords(current) + " " + "Thousand"
76+
}
77+
num = num % 1000
78+
}
79+
if result != "" {
80+
if num != 0 {
81+
result = result + " " + simpleNumberToWords(num)
82+
}
83+
} else {
84+
result = simpleNumberToWords(num)
85+
}
86+
87+
return result
88+
}

go/integer-to-english-words_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func TestSimpleNumberToWords(t *testing.T) {
6+
tests := []struct {
7+
input int
8+
expect string
9+
}{
10+
{
11+
input: 0,
12+
expect: "",
13+
}, {
14+
input: 1,
15+
expect: "One",
16+
}, {
17+
input: 19,
18+
expect: "Nineteen",
19+
}, {
20+
input: 10,
21+
expect: "Ten",
22+
}, {
23+
input: 20,
24+
expect: "Twenty",
25+
}, {
26+
input: 21,
27+
expect: "Twenty One",
28+
}, {
29+
input: 29,
30+
expect: "Twenty Nine",
31+
}, {
32+
input: 99,
33+
expect: "Ninety Nine",
34+
}, {
35+
input: 100,
36+
expect: "One Hundred",
37+
}, {
38+
input: 123,
39+
expect: "One Hundred Twenty Three",
40+
}, {
41+
input: 120,
42+
expect: "One Hundred Twenty",
43+
}, {
44+
input: 201,
45+
expect: "Two Hundred One",
46+
}, {
47+
input: 999,
48+
expect: "Nine Hundred Ninety Nine",
49+
},
50+
}
51+
52+
for _, item := range tests {
53+
actual := simpleNumberToWords(item.input)
54+
if actual != item.expect {
55+
t.Fatalf("%d expect %s, actual %s\n", item.input, item.expect, actual)
56+
}
57+
}
58+
}
59+
60+
func TestNumberToWords(t *testing.T) {
61+
tests := []struct {
62+
input int
63+
expect string
64+
}{
65+
{
66+
input: 0,
67+
expect: "Zero",
68+
}, {
69+
input: 1,
70+
expect: "One",
71+
}, {
72+
input: 20,
73+
expect: "Twenty",
74+
}, {
75+
input: 120,
76+
expect: "One Hundred Twenty",
77+
}, {
78+
input: 999,
79+
expect: "Nine Hundred Ninety Nine",
80+
}, {
81+
input: 1000,
82+
expect: "One Thousand",
83+
}, {
84+
input: 12345,
85+
expect: "Twelve Thousand Three Hundred Forty Five",
86+
}, {
87+
input: 1000000,
88+
expect: "One Million",
89+
}, {
90+
input: 1234567,
91+
expect: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven",
92+
}, {
93+
input: 1234567891,
94+
expect: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One",
95+
},
96+
}
97+
98+
for _, item := range tests {
99+
actual := numberToWords(item.input)
100+
if actual != item.expect {
101+
t.Fatalf("%d expect %s, actual %s\n", item.input, item.expect, actual)
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)