From ee16ee8d1aac448e6e7148fb6ef57d76e3663d1a Mon Sep 17 00:00:00 2001 From: Winston Jodjana <winj@cs.washington.edu> Date: Thu, 13 Oct 2022 22:41:46 -0700 Subject: [PATCH] Added getWordsAfter solution for solo project --- .../java/p2/wordsuggestor/NGramToNextChoicesMap.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/p2/wordsuggestor/NGramToNextChoicesMap.java b/src/main/java/p2/wordsuggestor/NGramToNextChoicesMap.java index 1e1a6c2..7f66f84 100644 --- a/src/main/java/p2/wordsuggestor/NGramToNextChoicesMap.java +++ b/src/main/java/p2/wordsuggestor/NGramToNextChoicesMap.java @@ -73,10 +73,16 @@ public class NGramToNextChoicesMap { Comparator<Item<String, Integer>> comp = new LargeValueFirstItemComparator<String, Integer>(); if (k < 0) { - InsertionSort.sort(afterNGrams, comp); + QuickSort.sort(afterNGrams, comp); } else { - // You must fix this line toward the end of the project - throw new NotYetImplementedException(); + Comparator<Item<String, Integer>> revComp = (o1, o2) -> comp.compare(o2, o1); + TopKSort.sort(afterNGrams, k, revComp); + int cutoff = Math.min(k, afterNGrams.length); + for (int i = 0; i < cutoff / 2; i++) { + Item<String, Integer> temp = afterNGrams[i]; + afterNGrams[i] = afterNGrams[cutoff - i - 1]; + afterNGrams[cutoff - i - 1] = temp; + } } String[] nextWords = new String[k < 0 ? afterNGrams.length : k]; -- GitLab