Advertisement
Guest User

Untitled

a guest
Jul 18th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import edu.princeton.cs.introcs.*;
  2. import edu.princeton.cs.algs4.ST;
  3. import edu.princeton.cs.algs4.SequentialSearchST;
  4. import edu.princeton.cs.algs4.LinearProbingHashST;
  5.  
  6. import java.lang.reflect.Constructor;
  7. import java.util.HashMap;
  8. import java.util.TreeMap;
  9. import java.util.Map;
  10.  
  11. // This can be used to test symbol table implementations.
  12. public class FrequencyCounter {
  13.  
  14.     interface SymbolTable<K,V> {
  15.         boolean contains(K key);
  16.         void put(K Key,V value);
  17.         V get(K Key);
  18.         Iterable<K> keys();
  19.     }
  20.  
  21.     public static class AdaptedSequentialSearchST
  22.             extends SequentialSearchST<String,Integer>
  23.             implements SymbolTable<String,Integer> {
  24.     }
  25.  
  26.     public static class AdaptedST
  27.             extends ST<String,Integer>
  28.             implements SymbolTable<String,Integer> { }
  29.  
  30.     public static class AdaptedLinearProbingHashST
  31.             extends LinearProbingHashST<String,Integer>
  32.             implements SymbolTable<String,Integer> { }
  33.  
  34.     static abstract class AdaptedMap
  35.             implements SymbolTable<String,Integer> {
  36.            
  37.             protected Map<String,Integer> map;
  38.    
  39.             public Iterable<String> keys() { return map.keySet(); }
  40.             public Integer get(String key) { return map.get(key); }
  41.  
  42.             public void put(String key,Integer value) { map.put(key,value); }
  43.  
  44.             public boolean contains(String key) {
  45.                 return map.containsValue(key); }
  46.     }
  47.  
  48.     public static class AdaptedHashMap extends AdaptedMap {
  49.         public AdaptedHashMap() { this.map = new HashMap<String,Integer>(); } }
  50.  
  51.     public static class AdaptedTreeMap extends AdaptedMap {
  52.         public AdaptedTreeMap() { this.map = new TreeMap<String,Integer>(); } }
  53.  
  54.     // TODO Tries
  55.  
  56.     private static void run(int minlen,String symbolTableClassName)
  57.         throws Exception {
  58.  
  59.         Class<?> cl = Class.forName(
  60.                 "danielf.src.java.programs.FrequencyCounter$Adapted" +
  61.                  symbolTableClassName);
  62.         Constructor<?> constructor = cl.getConstructor();
  63.         SymbolTable<String,Integer> st =
  64.             (SymbolTable<String,Integer>) constructor.newInstance();
  65.         while (!StdIn.isEmpty())
  66.         {
  67.             String word = StdIn.readString();
  68.             if (word.length() < minlen) continue;
  69.             if (!st.contains(word)) st.put(word,1);
  70.             else                    st.put(word,st.get(word)+1);
  71.  
  72.         }
  73.         String max = "";
  74.         st.put(max,0);
  75.         for (String word : st.keys()) {
  76.             if (st.get(word) > st.get(max))
  77.                 max = word;
  78.         }
  79.         StdOut.println(max + " " + st.get(max));
  80.  
  81.     }
  82.  
  83.     public static void main(String[] args) throws Exception {
  84.         try {
  85.             run(Integer.parseInt(args[0]),args[1]);
  86.         } catch (ClassNotFoundException e) {
  87.             System.err.println("ClassNotFoundException: " + e.getMessage());
  88.         } catch (NoSuchMethodException e) {
  89.             System.err.println("NoSuchMethodException: " + e.getMessage());
  90.         } catch (InstantiationException e) {
  91.             System.err.println("InstantiationException: " + e.getMessage());
  92.         }
  93.     }
  94.  
  95.     //class SymbolTable {
  96.     //    private
  97.     //}
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement