Ordering Distinct Words The distinctWordsByNatural and distinctWordsByCount meth
ID: 3809415 • Letter: O
Question
Ordering Distinct Words
The distinctWordsByNatural and distinctWordsByCount methods have you read the words from an input string, keep only the distinct words, and return an array with these words ordered by two policies. The distinctWordsByNatural method has you order the array via “natural” ordering (i.e. the String method’s compareTo method). The distinctWordsByCount uses natural ordering as a second ordering, but first orders by how many times each word appears.
To obtain credit, you MUST use a Set in both methods to avoid duplication and order outputs, as well as a Map in distinctWordsByCount to count each word’s occurrences. You have been supplied a fully implemented WordCount class to assist. Use Set/Map concrete classes that are efficient for the task.
Completed WordCount Class
public class WordCount implements Comparable<WordCount> {
final private String word;
final private int count;
/**
* Initialize the word count
*
* @param word word
* @param count word's count
*/
public WordCount(String word, int count) {
this.word = word;
this.count = count;
}
/**
* Get the word
*
* @return word
*/
public String getWord() {
return word;
}
/**
* Get the count
*
* @return count
*/
public int getCount() {
return count;
}
@Override
public String toString() {
return String.format("%s (%d)", word, count);
}
@Override
public int compareTo(WordCount o) {
if (count == o.count) {
return word.compareTo(o.word);
} else {
return Integer.compare(count, o.count);
}
}
}
Two Method I need help.
/**
* Takes a string of words
* and returns an array
* of distinct words sorted
* naturally
*
* @param words input string
* @return distinct words in input, sorted naturally (ascending)
*/
public static String[] distinctWordsByNatural(String words) {
// 1. Create a set that supports sorted keys
// 2. Construct a scanner with the input string
// e.g. Scanner s = new Scanner(str);
// 3. Use the scanner to read all the words
// and add them to the set
// 4. Iterate over the set, copy elements
// to an array
// 5. Return the array
}
/**
* Takes a string of words
* and returns an array of
* distinct words sorted
* by the number of times
* they appear
*
* @param words input string
* @return distinct words in input, sorted by number of appearances (ascending)
*/
public static String[] distinctWordsByCount(String words) {
// Suggested algorithm:
// 1. Create a map (doesn't need to sort) from
// words to integers (this will count how
// many of each)
// 2. Construct a scanner with the input string
// e.g. Scanner s = new Scanner(str);
// 3. Use the scanner to read all the words,
// use the map to update each word's counter
// (first time a word is hit, map to 1;
// otherwise increment what was there)
// 4. Create a set that supports sorted keys,
// this time of WordCount
// 5. For all the elements of the map, create
// a WordCount object and add to the set
// 6. Iterate over the elements of the set,
// add them to an array
// 7. Return the array
return new String[] {};
}
Some test cases to help test both methds:
//read as _(testDestinctNatural(Input, Desired Result);
public void testDistinctNatural() {
_testDistinctNatural("", new String[] {});
_testDistinctNatural("a", new String[] {"a"});
_testDistinctNatural("b", new String[] {"b"});
_testDistinctNatural("abc", new String[] {"abc"});
_testDistinctNatural("a a", new String[] {"a"});
_testDistinctNatural("c c c c c c c c", new String[] {"c"});
_testDistinctNatural("a b c", new String[] {"a", "b", "c"});
_testDistinctNatural("a c b", new String[] {"a", "b", "c"});
_testDistinctNatural("a b c b a", new String[] {"a", "b", "c"});
public void testDistinctCount() {
_testDistinctCount("", new String[] {});
_testDistinctCount("a", new String[] {"a"});
_testDistinctCount("b", new String[] {"b"});
_testDistinctCount("abc", new String[] {"abc"});
_testDistinctCount("a a", new String[] {"a"});
_testDistinctCount("c c c c c c c c", new String[] {"c"});
_testDistinctCount("a b c", new String[] {"a", "b", "c"});
_testDistinctCount("a c b", new String[] {"a", "b", "c"});
_testDistinctCount("a b c b a", new String[] {"c", "a", "b"});
_testDistinctCount("c c c b b a b b c c c a b c b a", new String[] {"a", "b", "c"});
Thank you.
Explanation / Answer
HI, Please find my implementation of both methods.
Please let me know in case of any issue.
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeSet;
public class WordCountTest {
/**
* Takes a string of words
* and returns an array
* of distinct words sorted
* naturally
*
* @param words input string
* @return distinct words in input, sorted naturally (ascending)
*/
public static String[] distinctWordsByNatural(String words) {
// Suggested algorithm:
// 1. Create a set that supports sorted keys
TreeSet<String> sordSet = new TreeSet<>();
// 2. Construct a scanner with the input string
// e.g. Scanner s = new Scanner(str);
Scanner scan = new Scanner(words);
scan.useDelimiter(" ");
// 3. Use the scanner to read all the words
// and add them to the set
while(scan.hasNext())
sordSet.add(scan.next());
// 4. Iterate over the set, copy elements
// to an array
String[] wordArr = new String[sordSet.size()];
int i=0;
for(String word : sordSet)
wordArr[i++] = word;
// 5. Return the array
return wordArr;
}
/**
* Takes a string of words
* and returns an array of
* distinct words sorted
* by the number of times
* they appear
*
* @param words input string
* @return distinct words in input, sorted by number of appearances (ascending)
*/
public static String[] distinctWordsByCount(String words) {
// Suggested algorithm:
// 1. Create a map (doesn't need to sort) from
// words to integers (this will count how
// many of each)
HashMap<String, Integer> map = new HashMap<>();
// 2. Construct a scanner with the input string
// e.g. Scanner s = new Scanner(str);
Scanner scan = new Scanner(words);
scan.useDelimiter(" ");
// 3. Use the scanner to read all the words,
// use the map to update each word's counter
// (first time a word is hit, map to 1;
// otherwise increment what was there)
while(scan.hasNext()){
String word = scan.next();
if(map.containsKey(word))
map.put(word, map.get(word)+1);
else
map.put(word, 1);
}
// 4. Create a set that supports sorted keys,
// this time of WordCount
TreeSet<WordCount> set = new TreeSet<>();
// 5. For all the elements of the map, create
// a WordCount object and add to the set
for(Entry<String, Integer> entry : map.entrySet()){
set.add(new WordCount(entry.getKey(), entry.getValue()));
}
// 6. Iterate over the elements of the set,
// add them to an array
String[] wordArr = new String[set.size()];
int i=0;
for(WordCount word : set)
wordArr[i++] = word.getWord();
// 7. Return the array
return wordArr;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.