The hardest CS50x problem (for me it was at least), from pset3, year 2023.
Based on ballots input, determine the winner of an election: every voter gets one vote, and the candidate with the most votes wins.
References:
https://en.wikipedia.org/wiki/Ranked_pairs
https://cs50.harvard.edu/x/2024/psets/3/tideman/
My approach was to use Depth First Search with stack implementation using dynamic memory allocation. I'm still a learner and on a long way to write robust C code, so don't consider it to be correct.
PS: i used cs50.h library to define typedef char *string, int i = get_int("Prompt: ") (get_int is scanf("%i", &i) implementation), string s = get_string("Prompt: "), but this was only to make sure to pass the check50.
./tideman Alice Bob Charlie
Number of voters: 5
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Alice
Rank 2: Charlie
Rank 3: Bob
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Bob
Rank 2: Charlie
Rank 3: Alice
Rank 1: Charlie
Rank 2: Alice
Rank 3: Bob
Winner: Alice
Visualiser:
https://tideman.netlify.app/
PS: data.txt can be passed to the CLI using ./tideman Alice Bob Charlie < data.txt and I provided CLI program tideman - executable file compiled with clang18.1 on linux.