Skip to content

Commit a7a4b6a

Browse files
refactor 273
1 parent 815908b commit a7a4b6a

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed
Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 273. Integer to English Words
5+
*
46
* Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
57
68
For example,
79
123 -> "One Hundred Twenty Three"
810
12345 -> "Twelve Thousand Three Hundred Forty Five"
911
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
10-
Hint:
1112
13+
Hint:
1214
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
1315
Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
1416
There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
1517
*/
1618
public class _273 {
1719

18-
private String[] belowTen = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
19-
private String[] belowTwenty = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
20-
private String[] belowHundred = new String[]{"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
21-
private String[] overThousand = new String[]{"Thousand", "Million", "Billion"};
22-
23-
public String numberToWords(int num) {
24-
String result;
25-
if (num == 0) {
26-
return belowTen[num];
27-
}
20+
public static class Solution1 {
21+
private String[] belowTen = new String[]{"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
22+
private String[] belowTwenty = new String[]{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
23+
private String[] belowHundred = new String[]{"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
24+
private String[] overThousand = new String[]{"Thousand", "Million", "Billion"};
2825

29-
result = hundredHelper(num % 1000);
30-
num = num / 1000;
31-
int i = 0;
32-
while (i < 3 && num > 0) {
33-
if (num % 1000 > 0) {
34-
result = hundredHelper(num % 1000) + overThousand[i] + " " + result;
26+
public String numberToWords(int num) {
27+
String result;
28+
if (num == 0) {
29+
return belowTen[num];
3530
}
36-
num = num / 1000;
37-
i++;
38-
}
3931

40-
return result.trim();
41-
}
32+
result = hundredHelper(num % 1000);
33+
num = num / 1000;
34+
int i = 0;
35+
while (i < 3 && num > 0) {
36+
if (num % 1000 > 0) {
37+
result = hundredHelper(num % 1000) + overThousand[i] + " " + result;
38+
}
39+
num = num / 1000;
40+
i++;
41+
}
4242

43-
public String hundredHelper(int num) {
44-
String nstr = "";
45-
if (num >= 100) {
46-
nstr = belowTen[num / 100] + " Hundred ";
43+
return result.trim();
4744
}
48-
num = num % 100;
49-
if (num >= 20) {
50-
if (num % 10 != 0) {
51-
nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " ";
52-
} else {
53-
nstr = nstr + belowHundred[num / 10 - 2] + " ";
45+
46+
private String hundredHelper(int num) {
47+
String nstr = "";
48+
if (num >= 100) {
49+
nstr = belowTen[num / 100] + " Hundred ";
50+
}
51+
num = num % 100;
52+
if (num >= 20) {
53+
if (num % 10 != 0) {
54+
nstr = nstr + belowHundred[num / 10 - 2] + " " + belowTen[num % 10] + " ";
55+
} else {
56+
nstr = nstr + belowHundred[num / 10 - 2] + " ";
57+
}
58+
} else if (num >= 10) {
59+
nstr = nstr + belowTwenty[num % 10] + " ";
60+
} else if (num > 0) {
61+
nstr = nstr + belowTen[num] + " ";
5462
}
55-
} else if (num >= 10) {
56-
nstr = nstr + belowTwenty[num % 10] + " ";
57-
} else if (num > 0) {
58-
nstr = nstr + belowTen[num] + " ";
63+
return nstr;
5964
}
60-
return nstr;
6165
}
62-
6366
}

src/test/java/com/fishercoder/_273Test.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@
66

77
import static org.junit.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 5/13/17.
11-
*/
129
public class _273Test {
13-
private static _273 test;
10+
private static _273.Solution1 solution1;
1411
private static int num;
1512

1613
@BeforeClass
1714
public static void setup() {
18-
test = new _273();
15+
solution1 = new _273.Solution1();
1916
}
2017

2118
@Test
2219
public void test1() {
2320
num = 123;
24-
assertEquals("One Hundred Twenty Three", test.numberToWords(num));
21+
assertEquals("One Hundred Twenty Three", solution1.numberToWords(num));
2522
}
2623

2724
@Test
2825
public void test2() {
2926
num = 12345;
30-
assertEquals("Twelve Thousand Three Hundred Forty Five", test.numberToWords(num));
27+
assertEquals("Twelve Thousand Three Hundred Forty Five", solution1.numberToWords(num));
3128
}
3229

3330
@Test
3431
public void test3() {
3532
num = 1234567;
36-
assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", test.numberToWords(num));
33+
assertEquals("One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven", solution1.numberToWords(num));
3734
}
3835
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy