Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public String pattern2() {
- if (words.isEmpty()) {
- throw new IllegalStateException();
- }
- List<String> patterns = new ArrayList<>();
- for (String word : words) {
- patterns.add(wordPattern(word));
- }
- //System.out.println(patterns);
- //System.out.println(mostCommon(patterns));
- return mostCommon(patterns);
- }
- public int record2(char guess) {
- if (guessesLeft() < 1 || words.isEmpty()) {
- throw new IllegalStateException();
- } else if (guesses.contains(guess)) {
- throw new IllegalArgumentException();
- }
- int count = 0;
- guesses.add(guess);
- String pattern = pattern();
- Iterator<String> itr = words.iterator();
- while (itr.hasNext()) {
- if (!wordPattern(itr.next()).equals(pattern)) {
- itr.remove();
- }
- }
- for (int i = 0; i < pattern.length(); i++) {
- if (pattern.charAt(i) == guess) {
- count ++;
- }
- }
- if (count == 0) {
- guessesLeft -= 1;
- }
- return count;
- }
- private String mostCommon2(List<String> patterns) {
- int max = 0;
- Map<String, Integer> map = new TreeMap<>();
- for (String pattern : patterns) {
- if (!map.containsKey(pattern)) {
- map.put(pattern, 1);
- } else {
- map.put(pattern, map.get(pattern) + 1);
- }
- if (map.get(pattern) > max) {
- max = map.get(pattern);
- }
- }
- for (String pattern : map.keySet()) {
- if (map.get(pattern) == max) {
- return pattern;
- }
- }
- return null; // don't necessarily want to have to do this ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement