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 1 and Wave 2 for Response Tally #6

Open
wants to merge 2 commits 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
60 changes: 31 additions & 29 deletions src/Tallyer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
Expand All @@ -9,71 +10,72 @@
*/
public class Tallyer {

/**
* The main method serves as the entry point for the program. It reads pairs of IDs and topics
* from standard input, stores them in lists, and then calculates the number of occurrences
* of each topic. The IDs and topics are guaranteed to not include internal whitespace.
*
* @param args command-line arguments (not used in this implementation)
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

List<String> ids = new ArrayList<>();
List<String> topics = new ArrayList<>();

// Reading input for IDs and topics
// Assumes file is well formed into pairs
while (input.hasNext()) {
ids.add(input.next());
topics.add(input.next());
ids.add(input.next()); // add ID
topics.add(input.next()); // add topic
}
input.close();

// Wave 1
// Wave 1: Tally the topics without filtering
Map<String, Integer> topicCounts = tallyTopics(topics);
System.out.println("Here are how many times each topic appears (unfiltered):");
System.out.println(topicCounts);

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

/**
* Tally the occurrences of each topic from the provided lists of IDs and topics.
* This method takes two lists, one of IDs and one of topics, and returns a map
* where each topic is associated with the number of times it appears in the input.
* Wave 1: Tally the occurrences of each topic from the provided list of topics.
*
* @param ids a list of strings representing IDs associated with each topic
* @param topics a list of strings representing the topics to be tallied
* @return a map containing topics as keys and their occurrence counts as values
*/
public static Map<String, Integer> tallyTopics(List<String> topics) {
// WAVE 1
// TODO: Remove the print statements and implement this method
Map<String, Integer> topicCountMap = new HashMap<>();

// Loop through each topic and count occurrences
for (String topic : topics) {
System.out.println("The topic is: " + topic);
topicCountMap.put(topic, topicCountMap.getOrDefault(topic, 0) + 1);
}

return null;
return topicCountMap;
}

/**
* Tally the occurrences of each topic from the provided lists of IDs and topics.
* This method takes two lists, one of IDs and one of topics, and returns a map
* where each topic is associated with the number of times it appears in the input.
* However, any user who did not enter exactly 2 topics should not have their votes counted.
* Wave 2: Tally the occurrences of each topic, but only count users who provided exactly 2 topics.
*
* @param ids a list of strings representing IDs associated with each topic
* @param topics a list of strings representing the topics to be tallied
* @return a map containing topics as keys and their occurrence counts as values
* @return a map containing topics as keys and their occurrence counts as values, filtered by valid users
*/
public static Map<String, Integer> tallyTopicsFiltered(List<String> ids, List<String> topics) {
// WAVE 2
// TODO: Implement this method
Map<String, Integer> topicCountMap = new HashMap<>();
Map<String, Integer> studentTopicCount = new HashMap<>();

return null;
}
// Count how many topics each student has entered
for (String id : ids) {
studentTopicCount.put(id, studentTopicCount.getOrDefault(id, 0) + 1);
}

// Tally only the topics from students who entered exactly 2 topics
for (int i = 0; i < ids.size(); i++) {
String studentId = ids.get(i);
if (studentTopicCount.get(studentId) == 2) {
String topic = topics.get(i);
topicCountMap.put(topic, topicCountMap.getOrDefault(topic, 0) + 1);
}
}

return topicCountMap;
}
}