Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.StringTokenizer;
- public class Test {
- private static final String paragraph = "twelve thousand two hundred space two hundred and one Just remember that you're standing on a platform at twelve o'clock and "
- + "there are five trains that run every hour on nine tracks. A train can go as fast as two hundred miles "
- + "an hour. One of millions of billions... ";
- private static final Map<String, Long> NUMBER_MAP;
- static {
- final Map<String, Long> map = new HashMap<String, Long>();
- map.put("one", 1L);
- map.put("two", 2L);
- map.put("three", 3L);
- map.put("four", 4L);
- map.put("five", 5L);
- map.put("six", 6L);
- map.put("seven", 7L);
- map.put("eight", 8L);
- map.put("nine", 9L);
- map.put("ten", 10L);
- map.put("eleven", 11L);
- map.put("twelve", 12L);
- map.put("thirteen", 13L);
- map.put("forteen", 14L);
- map.put("fifthteen", 15L);
- map.put("sixteen", 16L);
- map.put("eighteen", 18L);
- map.put("nineteen", 19L);
- map.put("twenty", 20L);
- map.put("thirty", 30L);
- map.put("forty", 40L);
- map.put("fifty", 50L);
- map.put("sixty", 60L);
- map.put("seventy", 70L);
- map.put("eigthy", 80L);
- map.put("ninety", 90L);
- map.put("thirty", 30L);
- map.put("hundred", 100L);
- map.put("hundreds", 100L);
- map.put("thousand", 1000L);
- map.put("thousands", 1000L);
- map.put("million", 1000000L);
- map.put("millions", 1000000L);
- map.put("billion", 1000000000L);
- map.put("billions", 1000000000L);
- NUMBER_MAP = Collections.unmodifiableMap(map);
- }
- private static final Set<String> IGNORED_WORDS;
- static {
- final Set<String> set = new HashSet<String>();
- set.add("and");
- IGNORED_WORDS = Collections.unmodifiableSet(set);
- }
- public static void main(final String[] args) {
- final StringTokenizer tokenizer = new StringTokenizer(paragraph.replace(".", " ").toLowerCase());
- final StringBuilder phrase = new StringBuilder();
- final Set<String> numberSet = NUMBER_MAP.keySet();
- while (tokenizer.hasMoreTokens()) {
- final String token = tokenizer.nextToken();
- if (numberSet.contains(token)) {
- if (phrase.length() > 0) {
- phrase.append(" ");
- }
- phrase.append(token);
- } else if (!IGNORED_WORDS.contains(token)) {
- processPhrase(phrase.toString());
- phrase.setLength(0);
- }
- }
- processPhrase(phrase.toString());
- }
- private static long processPhrase(final String phrase) {
- if (phrase.isEmpty()) {
- return 0;
- }
- final List<String> words = Arrays.asList(phrase.split("\\s+"));
- long value = 0L;
- long lastValue = 1;
- for (int i = 0; i < words.size(); i++) {
- final String word = words.get(i);
- final long currentValue = NUMBER_MAP.get(word);
- if (i == words.size() - 1) {
- if (currentValue < lastValue) {
- value += currentValue;
- } else if (currentValue > lastValue) {
- value += lastValue * currentValue;
- lastValue = lastValue * currentValue;
- }
- continue;
- }
- if (i == 0) {
- lastValue = currentValue;
- continue;
- }
- if (currentValue < lastValue) {
- lastValue = currentValue;
- } else if (currentValue > lastValue) {
- value += lastValue * currentValue;
- lastValue = lastValue * currentValue;
- }
- }
- System.out.println(phrase + " = " + value);
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement