Advertisement
Guest User

Anagram Tests

a guest
Oct 3rd, 2011
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8.  
  9. public class Anagrams {
  10.  
  11.     public interface Function {
  12.         public int call() throws Exception;
  13.     }
  14.  
  15.     public static void main(String[] args) throws Exception {
  16.         if (args.length < 1) {
  17.             System.out.println("Usage: java Anagrams [file]");
  18.             System.exit(1);
  19.         }
  20.  
  21.         final File file = new File(args[0]);
  22.         int expectedNonAnagram = 65763;
  23.  
  24.         time("testWithHashMap", 10, new Function() {
  25.  
  26.             @Override
  27.             public int call() throws IOException {
  28.                 return testWithHashMap(file);
  29.             }
  30.         }, expectedNonAnagram);
  31.  
  32.         time("testWithSorting", 10, new Function() {
  33.  
  34.             @Override
  35.             public int call() throws IOException {
  36.                 return testWithSorting(file);
  37.             }
  38.         }, expectedNonAnagram);
  39.  
  40.         time("testWithArray", 10, new Function() {
  41.  
  42.             @Override
  43.             public int call() throws IOException {
  44.                 return testWithArray(file);
  45.             }
  46.         }, expectedNonAnagram);
  47.  
  48.     }
  49.  
  50.     public static void time(String name, int repetitions, Function function,
  51.             int expectedResult) throws Exception {
  52.         long total = 0;
  53.         for (int i = 0; i < repetitions; i++) {
  54.             System.gc();
  55.             long start = System.currentTimeMillis();
  56.             int result = function.call();
  57.             long end = System.currentTimeMillis();
  58.             if (result != expectedResult) {
  59.                 System.out.println("Oops, " + name + " is broken");
  60.                 return;
  61.             }
  62.             total += end - start;
  63.         }
  64.         System.out.println("Execution of " + name + " took "
  65.                 + (total / repetitions) + " ms on average");
  66.     }
  67.  
  68.     public static int testWithArray(File file) throws IOException {
  69.         BufferedReader reader = new BufferedReader(new FileReader(file));
  70.         try {
  71.             String line = reader.readLine();
  72.             int lineNumber = 2;
  73.             int[] firstLineChars = getCharNumberArray(line);
  74.             while ((line = reader.readLine()) != null) {
  75.                 int[] currentLineChars = getCharNumberArray(line);
  76.                 if (!Arrays.equals(firstLineChars, currentLineChars)) {
  77.                     return lineNumber;
  78.                 }
  79.                 lineNumber += 1;
  80.             }
  81.         } finally {
  82.             reader.close();
  83.         }
  84.         return -1;
  85.     }
  86.  
  87.     private static int[] getCharNumberArray(String line) {
  88.         int[] charNumbers = new int[26];
  89.         Arrays.fill(charNumbers, 0);
  90.         for (char c : line.toLowerCase().toCharArray()) {
  91.             if (Character.isAlphabetic(c)) {
  92.                 charNumbers[c - 'a'] += 1;
  93.             }
  94.         }
  95.         return charNumbers;
  96.     }
  97.  
  98.     public static int testWithSorting(File file) throws IOException {
  99.         BufferedReader reader = new BufferedReader(new FileReader(file));
  100.         try {
  101.             String line = reader.readLine();
  102.             int lineNumber = 2;
  103.             char[] firstLineChars = getSortedChars(line);
  104.             while ((line = reader.readLine()) != null) {
  105.                 char[] currentLineChars = getSortedChars(line);
  106.                 if (!Arrays.equals(firstLineChars, currentLineChars)) {
  107.                     return lineNumber;
  108.                 }
  109.                 lineNumber += 1;
  110.             }
  111.         } finally {
  112.             reader.close();
  113.         }
  114.         return -1;
  115.     }
  116.  
  117.     private static char[] getSortedChars(String line) {
  118.         StringBuilder builder = new StringBuilder();
  119.         for (char c : line.toLowerCase().toCharArray()) {
  120.             if (Character.isAlphabetic(c)) {
  121.                 builder.append(c);
  122.             }
  123.         }
  124.         char[] results = builder.toString().toCharArray();
  125.         Arrays.sort(results);
  126.         return results;
  127.     }
  128.  
  129.     public static int testWithHashMap(File file) throws IOException {
  130.         BufferedReader reader = new BufferedReader(new FileReader(file));
  131.         try {
  132.             String line = reader.readLine();
  133.             int lineNumber = 2;
  134.             Map<Character, Integer> firstLineCounts = countLetters(line);
  135.             while ((line = reader.readLine()) != null) {
  136.                 Map<Character, Integer> currentLineCounts = countLetters(line);
  137.                 if (!firstLineCounts.equals(currentLineCounts)) {
  138.                     return lineNumber;
  139.                 }
  140.                 lineNumber += 1;
  141.             }
  142.         } finally {
  143.             reader.close();
  144.         }
  145.         return -1;
  146.     }
  147.  
  148.     private static Map<Character, Integer> countLetters(String line) {
  149.         Map<Character, Integer> letterCounts = new HashMap<Character, Integer>();
  150.         for (char c : line.toLowerCase().toCharArray()) {
  151.             if (Character.isAlphabetic(c)) {
  152.                 Integer count = letterCounts.get(c);
  153.                 if (count == null) {
  154.                     count = 0;
  155.                 }
  156.                 count += 1;
  157.                 letterCounts.put(c, count);
  158.             }
  159.         }
  160.         return letterCounts;
  161.     }
  162. }
  163.  
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement