Guessers (Conceptual)
PAGA Example: getGuess(String pattern, List<Character> guesses)
Suppose we have the dictionary: [alf, bed, bee, cat, dog, zoo, fish, cows]
We can rule out fish and cows since they are the wrong length.
Among the remaining words, e and o occur the most times (3 respectively).
| getGuess("---", []) |
pattern | --- |
guesses | [] |
potential words | [alf, bed, bee, cat, dog, zoo, fish, cows] |
return | e |
PAGA Example: getGuess(String pattern, List<Character> guesses)
Suppose we have the dictionary: [alf, bed, bee, cat, dog, zoo, fish, cows]
Here we can narrow it down to the words bed and bee.
| getGuess("b--", [b]) |
pattern | b-- |
guesses | [b] |
potential words | [alf, bed, bee, cat, dog, zoo, fish, cows] |
return | e |
Note that even though the PAGAFequencyGuesser would never select ‘b’ as its first guess, its still a valid argument for the guesses to start with b.
PAGA Example: getGuess(String pattern, List<Character> guesses)
Suppose we have the dictionary: [alf, bed, bee, cat, dog, zoo, fish, cows]
Here we can narrow it down to the words bed, bee, dog, and zoo.
| getGuess("---", [a]) |
pattern | --- |
guesses | [a] |
potential words | [alf, bed, bee, cat, dog, zoo, fish, cows] |
return | e |
PAGA Example: getGuess(String pattern, List<Character> guesses)
Suppose we have the dictionary: [alf, bed, bee, cat, dog, zoo, fish, cows]
Here we can narrow it down to just the word bed.
| getGuess("be-", [b, e]) |
pattern | be- |
guesses | [b, e] |
potential words | [alf, bed, bee, cat, dog, zoo, fish, cows] |
return | d |
There were two typos on this slide. Oops! Fixed.
PAGA Example: getGuess(String pattern, List<Character> guesses)
You don’t need to worry about degenerate cases.
| getGuess("be-", []) |
pattern | be- |
guesses | [] |
potential words | undefined |
return | undefined |
PAGA Example: getGuess(String pattern, List<Character> guesses)
You don’t need to worry about degenerate cases.
| getGuess("aaa", [a, b]) |
pattern | aaa |
guesses | [a, b] |
potential words | undefined |
return | undefined |
PAGA Example: getGuess(String pattern, List<Character> guesses)
You don’t need to worry about degenerate cases.
| getGuess("bed", [b, e, d]) |
pattern | bed |
guesses | [b, e, d] |
potential words | [alf, bed, bee, cat, dog, zoo, fish, cows] |
return | undefined |
Guessers (Coding)
Syntax Tip: getOrDefault
The getOrDefault method allows you to concisely handle situations where keys are missing.
if (!map.containsKey(k)){
map.put(k, 0);
}
map.put(k, map.get(k) + 1);
map.put(k, map.getOrDefault(k, 0));
Performance Tips
The tests in NaiveLetterFreqGuesserTest, PatternAwareLetterFreqGuesserTest, and PAGALetterFreqGuesserTest should run in milliseconds.
Performance tips:
Note: Make sure you have the latest tests. Your PatternAwareLetterFreqGuesserTest and PAGALetterFreqGuesserTest files should include a final test called testGetGuess_o__a_PatternLargeFile.
Performance Tip: String Concatenation vs. StringBuilder
Some students have solutions that involve adding thousands of strings to an existing string.
Note: It’s ok to do string concatenation in a loop if it’s only a few strings, such as building a pattern character by character.
List Removal
Calling remove on a list of strings is very slow.
Two ways to fix this:
listOfStrings.remove(word);
Set<String> setOfStrings = new TreeSet<>(listOfStrings);
Choosers
Tips coming soon
Coming soon