Advertisement
Guest User

Untitled

a guest
Jun 29th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.StringTokenizer;
  9.  
  10. public class Test {
  11.  
  12.   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 "
  13.       + "there are five trains that run every hour on nine tracks. A train can go as fast as two hundred miles "
  14.       + "an hour. One of millions of billions... ";
  15.  
  16.   private static final Map<String, Long> NUMBER_MAP;
  17.   static {
  18.     final Map<String, Long> map = new HashMap<String, Long>();
  19.     map.put("one", 1L);
  20.     map.put("two", 2L);
  21.     map.put("three", 3L);
  22.     map.put("four", 4L);
  23.     map.put("five", 5L);
  24.     map.put("six", 6L);
  25.     map.put("seven", 7L);
  26.     map.put("eight", 8L);
  27.     map.put("nine", 9L);
  28.     map.put("ten", 10L);
  29.     map.put("eleven", 11L);
  30.     map.put("twelve", 12L);
  31.     map.put("thirteen", 13L);
  32.     map.put("forteen", 14L);
  33.     map.put("fifthteen", 15L);
  34.     map.put("sixteen", 16L);
  35.     map.put("eighteen", 18L);
  36.     map.put("nineteen", 19L);
  37.     map.put("twenty", 20L);
  38.     map.put("thirty", 30L);
  39.     map.put("forty", 40L);
  40.     map.put("fifty", 50L);
  41.     map.put("sixty", 60L);
  42.     map.put("seventy", 70L);
  43.     map.put("eigthy", 80L);
  44.     map.put("ninety", 90L);
  45.     map.put("thirty", 30L);
  46.     map.put("hundred", 100L);
  47.     map.put("hundreds", 100L);
  48.     map.put("thousand", 1000L);
  49.     map.put("thousands", 1000L);
  50.     map.put("million", 1000000L);
  51.     map.put("millions", 1000000L);
  52.     map.put("billion", 1000000000L);
  53.     map.put("billions", 1000000000L);
  54.     NUMBER_MAP = Collections.unmodifiableMap(map);
  55.   }
  56.  
  57.   private static final Set<String> IGNORED_WORDS;
  58.   static {
  59.     final Set<String> set = new HashSet<String>();
  60.     set.add("and");
  61.     IGNORED_WORDS = Collections.unmodifiableSet(set);
  62.   }
  63.  
  64.   public static void main(final String[] args) {
  65.     final StringTokenizer tokenizer = new StringTokenizer(paragraph.replace(".", " ").toLowerCase());
  66.     final StringBuilder phrase = new StringBuilder();
  67.     final Set<String> numberSet = NUMBER_MAP.keySet();
  68.    
  69.     while (tokenizer.hasMoreTokens()) {
  70.       final String token = tokenizer.nextToken();
  71.      
  72.       if (numberSet.contains(token)) {
  73.         if (phrase.length() > 0) {
  74.           phrase.append(" ");
  75.         }
  76.         phrase.append(token);
  77.       } else if (!IGNORED_WORDS.contains(token)) {
  78.         processPhrase(phrase.toString());
  79.         phrase.setLength(0);
  80.       }
  81.     }
  82.    
  83.     processPhrase(phrase.toString());
  84.   }
  85.  
  86.   private static long processPhrase(final String phrase) {
  87.     if (phrase.isEmpty()) {
  88.       return 0;
  89.     }
  90.    
  91.     final List<String> words = Arrays.asList(phrase.split("\\s+"));
  92.    
  93.     long value = 0L;
  94.     long lastValue = 1;
  95.    
  96.     for (int i = 0; i < words.size(); i++) {
  97.       final String word = words.get(i);
  98.       final long currentValue = NUMBER_MAP.get(word);
  99.      
  100.       if (i == words.size() - 1) {
  101.         if (currentValue < lastValue) {
  102.           value += currentValue;
  103.         } else if (currentValue > lastValue) {
  104.           value += lastValue * currentValue;
  105.           lastValue = lastValue * currentValue;
  106.         }
  107.         continue;
  108.       }
  109.      
  110.       if (i == 0) {
  111.         lastValue = currentValue;
  112.         continue;
  113.       }
  114.      
  115.       if (currentValue < lastValue) {
  116.         lastValue = currentValue;
  117.       } else if (currentValue > lastValue) {
  118.         value += lastValue * currentValue;
  119.         lastValue = lastValue * currentValue;
  120.       }
  121.     }
  122.    
  123.     System.out.println(phrase + " = " + value);
  124.     return value;
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement