JAVA 66
Untitled By Antonisg on 29th March 2019 03:26:15 AM
  1. package Solution;
  2.  
  3.  
  4. import java.io.File;
  5. import java.lang.reflect.Method;
  6. import java.util.Arrays;
  7. import java.util.Collection;
  8. import java.util.HashSet;
  9. import java.util.Scanner;
  10. import java.util.TreeSet;
  11.  
  12. public class UnsortedSetTest {
  13.  
  14.     public static void main(String[] args) throws Exception {
  15.         String[] allFileNames = {"hounds.txt", "huckfinn.txt", "oz.txt", "war.txt", "ciaFactBook2008.txt"};
  16.         String[] noCIA = {"hounds.txt", "huckfinn.txt", "oz.txt", "war.txt"};
  17.         countWords(new BinarySearchTree<String>(), allFileNames[0]);
  18.         for(String s : allFileNames) {
  19.             System.out.println(s);
  20.             countWordsOurUnsortedSet(s);
  21.             countWordsOurBinarySearchTree(s);
  22.             countWordsOurHash(s);
  23.             countWordsCollection(new TreeSet<String>(), s);
  24.             int[] result = countWordsCollection(new HashSet<String>(), s);
  25.             System.out.println(result[0] + " total words.");
  26.             System.out.println(result[1] + " distinct words.");
  27.             System.out.println();
  28.            
  29.         }
  30.     }
  31.    
  32.     // return total num words, and num distinct words
  33.     public static int[] countWordsCollection(Collection<String> c, String fileName) throws Exception{
  34.         c.clear();
  35.         Scanner fileScanner = new Scanner(new File(fileName));  
  36.         Stopwatch st = new Stopwatch();
  37.         st.start();
  38.         int total = 0;
  39.         while(fileScanner.hasNext()){
  40.             c.add(fileScanner.next());
  41.             total++;
  42.         }
  43.         st.stop();
  44.         System.out.println("Time for " + c.getClass() + " : \n" + st);
  45. //        System.out.println(c.size() + " distinct words");
  46. //        System.out.println(total + " total words including duplicates: ");
  47.         assert total >= c.size();
  48.         System.out.println();
  49.         return new int[]{total, c.size()};
  50.     }
  51.    
  52.    
  53.     // GACKY GACKY GACKY repition. Look into removing repetition with reflection
  54.     // we assume there will be add and size methods
  55.     public static int[] countWordsOurHash(String fileName) throws Exception {
  56.         Scanner fileScanner = new Scanner(new File(fileName));  
  57.         Stopwatch st = new Stopwatch();
  58.         UnsortedHashSet<String> c = new UnsortedHashSet<String>();
  59.         st.start();
  60.         int total = 0;
  61.         while(fileScanner.hasNext()) {
  62.             c.add(fileScanner.next());
  63.             total++;
  64.         }
  65.         st.stop();
  66.         System.out.println("Time for our hashtable (closed address hashing): \n" + st);
  67. //        System.out.println(c.size() + " distinct words");
  68. //        System.out.println(total + " total words including duplicates: ");
  69.         assert total >= c.size();
  70.         System.out.println();
  71.         return new int[]{total, c.size()};
  72.     }
  73.    
  74.     public static int[] countWordsOurUnsortedSet(String fileName) throws Exception {
  75.         Scanner fileScanner = new Scanner(new File(fileName));  
  76.         Stopwatch st = new Stopwatch();
  77.         UnsortedSet<String> c = new UnsortedSet<String>();
  78.         st.start();
  79.         int total = 0;
  80.         while(fileScanner.hasNext()){
  81.             c.add(fileScanner.next());
  82.             total++;
  83.         }
  84.         st.stop();
  85.         System.out.println("Time for our unsorted set based on ArrayList: \n" + st);
  86. //        System.out.println(c.size() + " distinct words");
  87. //        System.out.println(total + " total words including duplicates: ");
  88.         assert total >= c.size();
  89.         System.out.println();
  90.         return new int[]{total, c.size()};
  91.     }
  92.    
  93.     public static int[] countWordsOurBinarySearchTree(String fileName) throws Exception {
  94.         Scanner fileScanner = new Scanner(new File(fileName));  
  95.         Stopwatch st = new Stopwatch();
  96.         BinarySearchTree<String> c = new BinarySearchTree<String>();
  97.         st.start();
  98.         int total = 0;
  99.         while(fileScanner.hasNext()){
  100.             c.add(fileScanner.next());
  101.             total++;
  102.         }
  103.         st.stop();
  104.         System.out.println("Time for our binary search tree: \n" + st);
  105. //        System.out.println(c.size() + " distinct words");
  106. //        System.out.println(total + " total words including duplicates: ");
  107.         assert total >= c.size();
  108.         System.out.println();
  109.         return new int[]{total, c.size()};
  110.     }
  111.    
  112.    
  113.     // a try at reflection. Not working on Binary Search tree from class.
  114.     // Hunch. Due to add method taking in Comparable, not Object!
  115.     // Alterantives: search list of methods for name?
  116.     public static int[] countWords(Object c, String fileName) throws Exception {
  117.         Scanner fileScanner = new Scanner(new File(fileName));  
  118.         Stopwatch st = new Stopwatch();
  119.         System.out.println(Arrays.toString(c.getClass().getMethods()));
  120.         Method addMethod = c.getClass().getMethod("add", Object.class);
  121.         st.start();
  122.         int total = 0;
  123.         while(fileScanner.hasNext()){
  124.             addMethod.invoke(c, fileScanner.next());
  125.             total++;
  126.         }
  127.         st.stop();
  128.         System.out.println("Time for " + c.getClass() + ": "+ st);
  129.         Method sizeMethod = c.getClass().getMethod("size");
  130.         int distictWords = (Integer) sizeMethod.invoke(c);
  131. //        System.out.println(distictWords + " distinct words");
  132. //        System.out.println(total + " total words including duplicates: ");
  133.         System.out.println();
  134.         return new int[]{total, distictWords};
  135.     }
  136. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Recent Pastes
Raw Paste

Login or Register to edit or fork this paste. It's free.