@Test public void testGetGoodWordStartingWith() { ArrayList<String> words = new ArrayList<>(Arrays.asList(wordsArray)); SimpleDictionary dict = new SimpleDictionary(words, 0); assertEquals("life", dict.getGoodWordStartingWith("li")); assertEquals("lives", dict.getGoodWordStartingWith("l")); assertEquals("apple", dict.getGoodWordStartingWith("a")); assertEquals(0, dict.getGoodWordStartingWith("ca").length() % 2); assertEquals(1, dict.getGoodWordStartingWith("cat").length() % 2); }
@Override public String getGoodWordStartingWith(String prefix) { if (prefix.length() == 0) { return getAnyWordStartingWith(prefix); } int start = findFirstIndexStartingWith(prefix); int end = findLastIndexStartingWith(prefix); if (start == -1 || end == -1) { return null; } List<String> goodWords = new ArrayList<>(); for (int i = start; i <= end; i++) { if (isGoodWord(words.get(i), prefix)) { goodWords.add(words.get(i)); } } if (goodWords.size() > 0) { return goodWords.get(mRandom.nextInt(goodWords.size())); } else { return words.get(mRandom.nextInt(end - start) + start); } }
SimpleDictionary implements GhostDictionary { @Override public String getGoodWordStartingWith(String prefix) { if (prefix.length() == 0) { return getAnyWordStartingWith(prefix); } int start = findFirstIndexStartingWith(prefix); int end = findLastIndexStartingWith(prefix); if (start == -1 || end == -1) { return null; } List<String> goodWords = new ArrayList<>(); for (int i = start; i <= end; i++) { if (isGoodWord(words.get(i), prefix)) { goodWords.add(words.get(i)); } } if (goodWords.size() > 0) { return goodWords.get(mRandom.nextInt(goodWords.size())); } else { return words.get(mRandom.nextInt(end - start) + start); } } }
SimpleDictionary implements GhostDictionary { @Override public String getGoodWordStartingWith(String prefix) { if (prefix.length() == 0) { return getAnyWordStartingWith(prefix); } int start = findFirstIndexStartingWith(prefix); int end = findLastIndexStartingWith(prefix); if (start == -1 || end == -1) { return null; } List<String> goodWords = new ArrayList<>(); for (int i = start; i <= end; i++) { if (isGoodWord(words.get(i), prefix)) { goodWords.add(words.get(i)); } } if (goodWords.size() > 0) { return goodWords.get(mRandom.nextInt(goodWords.size())); } else { return words.get(mRandom.nextInt(end - start) + start); } } SimpleDictionary(InputStream wordListStream); @VisibleForTesting  SimpleDictionary(ArrayList<String> words, long randomSeed); }
SimpleDictionary implements GhostDictionary { @Override public String getGoodWordStartingWith(String prefix) { if (prefix.length() == 0) { return getAnyWordStartingWith(prefix); } int start = findFirstIndexStartingWith(prefix); int end = findLastIndexStartingWith(prefix); if (start == -1 || end == -1) { return null; } List<String> goodWords = new ArrayList<>(); for (int i = start; i <= end; i++) { if (isGoodWord(words.get(i), prefix)) { goodWords.add(words.get(i)); } } if (goodWords.size() > 0) { return goodWords.get(mRandom.nextInt(goodWords.size())); } else { return words.get(mRandom.nextInt(end - start) + start); } } 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 { @Override public String getGoodWordStartingWith(String prefix) { if (prefix.length() == 0) { return getAnyWordStartingWith(prefix); } int start = findFirstIndexStartingWith(prefix); int end = findLastIndexStartingWith(prefix); if (start == -1 || end == -1) { return null; } List<String> goodWords = new ArrayList<>(); for (int i = start; i <= end; i++) { if (isGoodWord(words.get(i), prefix)) { goodWords.add(words.get(i)); } } if (goodWords.size() > 0) { return goodWords.get(mRandom.nextInt(goodWords.size())); } else { return words.get(mRandom.nextInt(end - start) + start); } } 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);  }