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

homework for the Tally #21

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
44 changes: 33 additions & 11 deletions src/Tallyer.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;

/**
* The Tallyer class provides functionality for reading ID and topic pairs from user input,
* and tallying the number of occurrences of each topic.
*/
public class Tallyer {

public class Tallyer{

/**
* The main method serves as the entry point for the program. It reads pairs of IDs and topics
Expand All @@ -30,12 +32,12 @@ public static void main(String[] args) {
}
input.close();

// Wave 1
// Wave 1: Tally the occurrences of each topic from the provided list of topics
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 occurrences of valid votes for each topic
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 @@ -50,17 +52,22 @@ public static void main(String[] args) {
* @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: Implement this method
// Wave 1: Count how many times each topic appears in the list of topics
Map<String, Integer> topicCounts = new HashMap<>();

// Loop through each topic and count occurrences
for (String topic : topics) {
topicCounts.put(topic, topicCounts.getOrDefault(topic, 0) + 1);
}

return null;
return topicCounts;
}

/**
* Tally the occurrences of valid votes for each topic from the provided lists of IDs and topics.
*
* The lists are of equal length and are aligned: the id at index zero cast a vote for
* the topic at endex 0, the id at index 1 cast a vote for the topic at index 1, etc.
* the topic at index 0, the id at index 1 cast a vote for the topic at index 1, etc.
* It 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.
*
Expand All @@ -69,9 +76,24 @@ public static Map<String, Integer> tallyTopics(List<String> topics) {
* @return a map containing topics as keys and their occurrence counts as values
*/
public static Map<String, Integer> tallyTopicsFiltered(List<String> ids, List<String> topics) {
// WAVE 2
// TODO: Implement this method
// Wave 2: Count valid votes for each topic, excluding invalid entries
Map<String, Integer> filteredTopicCounts = new HashMap<>();

// Ensure the lists are of the same length
if (ids.size() != topics.size()) {
throw new IllegalArgumentException("IDs and topics lists must have the same length.");
}

return null;
}
// Loop through each student-topic pair and count valid votes
for (int i = 0; i < ids.size(); i++) {
String topic = topics.get(i);

// Only count valid topics (non-empty and non-null)
if (topic != null && !topic.trim().isEmpty()) {
filteredTopicCounts.put(topic, filteredTopicCounts.getOrDefault(topic, 0) + 1);
}
}

return filteredTopicCounts;
}
}