# Get the ranked preferences for each voter pairs = [] for i in range(num_voters): voter_preferences = [] print(f"\nEnter the ranked preferences for voter {i+1}:") for j in range(num_candidates): preference = input(f"Enter preference {j+1}: ") voter_preferences.append(preference) pairs.append(voter_preferences)
// Get the ranked preferences for each voter Voter voters[num_voters]; for (int i = 0; i < num_voters; i++) { printf("\nEnter the ranked preferences for voter %d:\n", i+1); for (int j = 0; j < num_candidates; j++) { printf("Enter preference %d: ", j+1); scanf("%s", voters[i].preferences[j]); } } Cs50 Tideman Solution
#define MAX_CANDIDATES 9 #define MAX_VOTERS 9 #define MAX_NAME_LENGTH 50 # Get the ranked preferences for each voter
// Eliminate the candidate(s) with the fewest votes int eliminated_candidates = 0; while (eliminated_candidates < num_candidates - 1) { // Find the candidate with the fewest votes int min_vote_index = -1; for (int i = 0; i < num_candidates; i++) { if (candidates[i].votes == min_votes) { min_vote_index = i; break; } } for (int i = 0
def tideman(candidates, pairs): # Count first-choice votes vote_counts = {candidate: 0 for candidate in candidates} for pair in pairs: vote_counts[pair[0]] += 1
# Update vote counts vote_counts = {candidate: 0 for candidate in candidates} for pair in pairs: if len(pair) > 0: vote_counts[pair[0]] += 1
// Structure to represent a candidate typedef struct { char name[MAX_NAME_LENGTH]; int votes; } Candidate;