Last active
September 10, 2016 12:04
-
-
Save mayurbhangale/ad8c50db78cea9580c027f1bca697ee3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.google.engedu.ghost; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import java.util.ArrayList; | |
| import java.util.Collections; | |
| import java.util.Comparator; | |
| import java.util.Random; | |
| public class SimpleDictionary implements GhostDictionary { | |
| private ArrayList<String> words; | |
| public SimpleDictionary(InputStream wordListStream) throws IOException { | |
| BufferedReader in = new BufferedReader(new InputStreamReader(wordListStream)); | |
| words = new ArrayList<>(); | |
| String line = null; | |
| while((line = in.readLine()) != null) { | |
| String word = line.trim(); | |
| if (word.length() >= MIN_WORD_LENGTH) | |
| words.add(line.trim()); | |
| } | |
| } | |
| @Override | |
| public boolean isWord(String word) { | |
| return words.contains(word); | |
| } | |
| @Override | |
| public String getAnyWordStartingWith(String prefix) { | |
| if(prefix == null || prefix.trim().equals("")){ | |
| Random random = new Random(); | |
| return words.get(random.nextInt(words.size())); | |
| } | |
| else{ | |
| Comparator<String> comparator = new Comparator<String>() { | |
| @Override | |
| public int compare(String lhs, String rhs) { | |
| return lhs.substring(0, Math.min(rhs.length(),lhs.length())).compareToIgnoreCase(rhs); | |
| } | |
| }; | |
| int index = Collections.binarySearch(words,prefix,comparator); | |
| if(index >= 0){ | |
| return words.get(index); | |
| } | |
| else{ | |
| return null; | |
| } | |
| } | |
| } | |
| @Override | |
| public String getGoodWordStartingWith(String prefix) { | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment