@Test public void testFindLastIndexStartingWith() { ArrayList<String> words = new ArrayList<>(Arrays.asList(wordsArray)); SimpleDictionary dict = new SimpleDictionary(words, 0); assertEquals(0, dict.findLastIndexStartingWith("a")); assertEquals(8, dict.findLastIndexStartingWith("cat")); assertEquals(14, dict.findLastIndexStartingWith("li")); assertEquals(14, dict.findLastIndexStartingWith("liv")); }
@VisibleForTesting int findLastIndexStartingWith(String prefix) { int startIndex = 0; int endIndex = words.size(); int mid; while (startIndex < endIndex) { if (startIndex + 1 == endIndex) { if (words.get(startIndex).startsWith(prefix)) { return startIndex; } else { return -1; } } mid = (startIndex + endIndex) / 2; if (words.get(mid).startsWith(prefix)) { if (mid + 1 >= endIndex || !words.get(mid + 1).startsWith(prefix)) { return mid; } else { startIndex = mid; } } else { int comparison = words.get(mid).compareTo(prefix); if (comparison < 0) { startIndex = mid; } else { endIndex = mid; } } } return -1; }
SimpleDictionary implements GhostDictionary { @VisibleForTesting int findLastIndexStartingWith(String prefix) { int startIndex = 0; int endIndex = words.size(); int mid; while (startIndex < endIndex) { if (startIndex + 1 == endIndex) { if (words.get(startIndex).startsWith(prefix)) { return startIndex; } else { return -1; } } mid = (startIndex + endIndex) / 2; if (words.get(mid).startsWith(prefix)) { if (mid + 1 >= endIndex || !words.get(mid + 1).startsWith(prefix)) { return mid; } else { startIndex = mid; } } else { int comparison = words.get(mid).compareTo(prefix); if (comparison < 0) { startIndex = mid; } else { endIndex = mid; } } } return -1; } }
SimpleDictionary implements GhostDictionary { @VisibleForTesting int findLastIndexStartingWith(String prefix) { int startIndex = 0; int endIndex = words.size(); int mid; while (startIndex < endIndex) { if (startIndex + 1 == endIndex) { if (words.get(startIndex).startsWith(prefix)) { return startIndex; } else { return -1; } } mid = (startIndex + endIndex) / 2; if (words.get(mid).startsWith(prefix)) { if (mid + 1 >= endIndex || !words.get(mid + 1).startsWith(prefix)) { return mid; } else { startIndex = mid; } } else { int comparison = words.get(mid).compareTo(prefix); if (comparison < 0) { startIndex = mid; } else { endIndex = mid; } } } return -1; } SimpleDictionary(InputStream wordListStream); @VisibleForTesting  SimpleDictionary(ArrayList<String> words, long randomSeed); }
SimpleDictionary implements GhostDictionary { @VisibleForTesting int findLastIndexStartingWith(String prefix) { int startIndex = 0; int endIndex = words.size(); int mid; while (startIndex < endIndex) { if (startIndex + 1 == endIndex) { if (words.get(startIndex).startsWith(prefix)) { return startIndex; } else { return -1; } } mid = (startIndex + endIndex) / 2; if (words.get(mid).startsWith(prefix)) { if (mid + 1 >= endIndex || !words.get(mid + 1).startsWith(prefix)) { return mid; } else { startIndex = mid; } } else { int comparison = words.get(mid).compareTo(prefix); if (comparison < 0) { startIndex = mid; } else { endIndex = mid; } } } return -1; } SimpleDictionary(InputStream wordListStream); @VisibleForTesting  SimpleDictionary(ArrayList<String> words, long randomSeed); @Override boolean isWord(String word); @Override String getAnyWordStartingWith(String prefix); @Override String getGoodWordStartingWith(String prefix); }
SimpleDictionary implements GhostDictionary { @VisibleForTesting int findLastIndexStartingWith(String prefix) { int startIndex = 0; int endIndex = words.size(); int mid; while (startIndex < endIndex) { if (startIndex + 1 == endIndex) { if (words.get(startIndex).startsWith(prefix)) { return startIndex; } else { return -1; } } mid = (startIndex + endIndex) / 2; if (words.get(mid).startsWith(prefix)) { if (mid + 1 >= endIndex || !words.get(mid + 1).startsWith(prefix)) { return mid; } else { startIndex = mid; } } else { int comparison = words.get(mid).compareTo(prefix); if (comparison < 0) { startIndex = mid; } else { endIndex = mid; } } } return -1; } SimpleDictionary(InputStream wordListStream); @VisibleForTesting  SimpleDictionary(ArrayList<String> words, long randomSeed); @Override boolean isWord(String word); @Override String getAnyWordStartingWith(String prefix); @Override String getGoodWordStartingWith(String prefix);  }