Guest User

Untitled

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