Skip to content

Commit 6e8d606

Browse files
committed
Updated 1 solution
1 parent 94732cd commit 6e8d606

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

Chp. 16 - More Problems (Moderate)/_16_08_English_Int/EnglishInt.java

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
// Solution is mostly from online book solutions
44
//
55
// Tricks:
6-
// - use arrays as data structure for the words
7-
// - split at the commas of a long number (into groups of 3), using a function called "numToStringl00"
8-
// - don't forget to consider negative numbers
6+
// - Use arrays as data structure for the words.
7+
// - Split the number at each comma into groups of 3 digits. Create a function to convert 3-digit numbers to words.
8+
// - Don't forget to consider negative numbers, and 0.
99

1010
public class EnglishInt {
1111

12-
private static String[] digits = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
13-
private static String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
14-
private static String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
15-
private static String[] bigs = {"", "Thousand", "Million", "Billion"};
12+
private static String[] first20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
13+
private static String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
14+
private static String[] bigs = {"", "Thousand", "Million", "Billion"};
1615

1716
public static String numToString(int num) {
1817
if (num < 0) {
@@ -24,53 +23,28 @@ public static String numToString(int num) {
2423
int bigsIndex = 0;
2524
StringBuffer sb = new StringBuffer();
2625

27-
/* We create the string from right to left, inserting in the front. This makes code clean and scalability easier */
26+
// We create the string from right to left, inserting in the front. This makes code clean and scalability easier
2827
while (num > 0) {
2928
if (num % 1000 != 0) {
3029
sb.insert(0, numToString100(num % 1000) + bigs[bigsIndex] + " ");
3130
}
3231
num /= 1000;
3332
bigsIndex++;
3433
}
35-
return sb.toString();
34+
return sb.toString().trim();
3635
}
3736

38-
public static String numToString100(int num) {
39-
if (num < 0) {
40-
return "Negative " + numToString(-1 * num);
41-
} else if (num == 0) {
42-
return "Zero";
43-
} else if (num > 999) {
44-
return null;
45-
}
46-
47-
StringBuffer sb = new StringBuffer();
48-
49-
/* Hundreds */
50-
if (num >= 100) {
51-
int digit = num / 100;
52-
sb.append(digits[digit] + " Hundred ");
53-
num = num % 100; // now we have a 2-digit number
54-
}
55-
56-
/* Tens */
57-
int tensDigit = num / 10;
58-
int singleDigit = num % 10;
59-
60-
if (num >= 10 && num <= 19) {
61-
sb.append(teens[singleDigit] + " ");
62-
return sb.toString();
63-
} else if (num >= 20) {
64-
sb.append(tens[tensDigit] + " ");
65-
}
66-
67-
/* Single Digit */
68-
if (singleDigit != 0) {
69-
sb.append(digits[singleDigit] + " ");
70-
}
71-
72-
return sb.toString();
37+
private static String numToString100(int num) { // assumes 0 < num < 1000
38+
if (num == 0)
39+
return "";
40+
else if (num < 20)
41+
return first20[num] + " ";
42+
else if (num < 100)
43+
return tens[num / 10] + " " + numToString100(num % 10);
44+
else
45+
return first20[num / 100] + " Hundred " + numToString100(num % 100);
7346
}
7447
}
7548

76-
// Time Complexity: O(1)
49+
// Time Complexity: O(1)
50+
// Space Complexity: O(1)

Chp. 16 - More Problems (Moderate)/_16_08_English_Int/Tester.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ public static void main(String[] args) {
66
for (int i = 0; i < 23; i++) {
77
test(i);
88
}
9+
test(0);
910
test(1000);
10-
test(2998);
11+
test(-2998);
1112
test(253513);
13+
test(1000010);
1214
test(10090034);
1315
}
1416

0 commit comments

Comments
 (0)