-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseIntReloaded.java
More file actions
77 lines (62 loc) · 2.16 KB
/
Copy pathParseIntReloaded.java
File metadata and controls
77 lines (62 loc) · 2.16 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
package kyu4;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
public class ParseIntReloaded {
private static final Map<String, Integer> numberMap = Map.ofEntries(
Map.entry("zero", 0),
Map.entry("one", 1),
Map.entry("two", 2),
Map.entry("three", 3),
Map.entry("four", 4),
Map.entry("five", 5),
Map.entry("six", 6),
Map.entry("seven", 7),
Map.entry("eight", 8),
Map.entry("nine", 9),
Map.entry("ten", 10),
Map.entry("eleven", 11),
Map.entry("twelve", 12),
Map.entry("thirteen", 13),
Map.entry("fourteen", 14),
Map.entry("fifteen", 15),
Map.entry("sixteen", 16),
Map.entry("seventeen", 17),
Map.entry("eighteen", 18),
Map.entry("nineteen", 19),
Map.entry("twenty", 20),
Map.entry("thirty", 30),
Map.entry("forty", 40),
Map.entry("fifty", 50),
Map.entry("sixty", 60),
Map.entry("seventy", 70),
Map.entry("eighty", 80),
Map.entry("ninety", 90)
);
private static final Map<String, Function<Integer, Integer>> multiplierMap = Map.ofEntries(
Map.entry("hundred", x -> x * 100),
Map.entry("thousand", __ -> 1000),
Map.entry("million", __ -> 1000000)
);
public static int parseInt(String numStr) {
String[] split = numStr.split(" ");
int multiplier = 1;
int number = 0;
for (int i = split.length - 1; i >= 0; i--) {
String s = split[i];
if (s.equals("and")) {
continue;
}
if (multiplierMap.containsKey(s)) {
multiplier = multiplierMap.get(s).apply(multiplier);
} else {
final int n = Arrays.stream(s.split("-"))
.map(numberMap::get)
.mapToInt(Integer::intValue)
.sum();
number += n * multiplier;
}
}
return number;
}
}