Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed wave 2 #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions src/Tallyer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.HashMap;

/**
* The Tallyer class provides functionality for reading ID and topic pairs from user input,
Expand Down Expand Up @@ -34,11 +35,13 @@ public static void main(String[] args) {
Map<String, Integer> topicCounts = tallyTopics(topics);
System.out.println("Here are how many times each topic appears (unfiltered):");
System.out.println(topicCounts);
System.out.println("test");

// Wave 2
Map<String, Integer> topicCountsFiltered = tallyTopicsFiltered(ids, topics);
System.out.println("Here are how many times each topic appears (filtered):");
System.out.println(topicCountsFiltered);

}

/**
Expand All @@ -52,12 +55,17 @@ public static void main(String[] args) {
*/
public static Map<String, Integer> tallyTopics(List<String> topics) {
// WAVE 1
// TODO: Remove the print statements and implement this method
Map<String, Integer> topicsCountMap = new HashMap<>();

for (String topic : topics) {
System.out.println("The topic is: " + topic);
if(!topicsCountMap.containsKey(topic)){
topicsCountMap.put(topic, 1);
} else {
topicsCountMap.put(topic, topicsCountMap.get(topic) + 1);
}
}

return null;
return topicsCountMap;
}

/**
Expand All @@ -72,8 +80,43 @@ public static Map<String, Integer> tallyTopics(List<String> topics) {
*/
public static Map<String, Integer> tallyTopicsFiltered(List<String> ids, List<String> topics) {
// WAVE 2
// TODO: Implement this method

return null;
// list for valid topics
List<String> validTopics = new ArrayList<>();
// map of topic count
Map<String, Integer> topicsCountMap = new HashMap<>();

// count ids
Map<String, Integer> idsCountMap = new HashMap<>();
for (String id : ids){
if(!idsCountMap.containsKey(id)){
idsCountMap.put(id, 1);
}
else {
idsCountMap.put(id, idsCountMap.get(id) + 1);
}
}

// create list of valid topics
for (int i = 0; i < ids.size(); i++) {
if(idsCountMap.get(ids.get(i)) == 2){
validTopics.add(topics.get(i));
}
}

// count topics

for (String topic : validTopics) {
if(!topicsCountMap.containsKey(topic)){
topicsCountMap.put(topic, 1);
}
else {
topicsCountMap.put(topic, topicsCountMap.get(topic) + 1);
}



}
return topicsCountMap;
}
}