Could someone help me with this program I am struggling with
this assignment. It requires Python programing language also could
please add some comments to let me understand what is the program
doing? Thank you
def get_file_contents(filename): This function opens a file for reading, reads the contents into a string, and returns that string. If the file does not exist, the function should return None. def get_votes (string): This function takes a string as its parameter and returns a list of list of strings as the return value: • Lines are separated by the newline character. Your program can use the split method to separate the string into a list of lines. Empty lines should be ignored. • Non-empty lines are comma-separated. Your program can use the split method to separate a line into a list of strings. Each list of strings represents the rankings of the candidates for one voter. The list of those lists represents all of the rankings by the voters. assert get_votes(a, b, cnc, b, a na,c,b") == [ ["a", "b", "c"], ["C", "b", "a"] 1 assert get_votes("a, b\nb, a na,b") == [["a", "b"], ["6", "a"],["a", "b"]] In the first assert above, there were three voters who each ranked the candidates (a, b, and c). 1. The first voter ranked a above b above c. 2. The second voter ranked c above b above a. 3. The third voter ranked a above c above b. def borda_scores (votes): One way to elect a candidate using Ranked Choice Voting is called the "Borda Count" method. It assigns points to given rankings. The candidate with the most points wins. If there are N candidates, being ranked first is worth N points, being ranked second is worth N-1 points, etc. This function should take a list of rankings as its input parameter, and it should return a dictionary. The returned dictionary has candidates as keys, and for each key, the associated value is the total "Borda count" (i.e., points) for that candidate. . Your code may assume that every voter ranked all of the candidates. votesi = [["a", "b", "s"], ["c", "b", "a"], ["a", "c", "b"]] votes2 - [["a", "b"], ["6", "a" ["a", votes3 - [["a", "b", ["6", "c", "a"],["", "a", "b"]] assert borda_scores(votes1) -- "a": 7, "b": 5, "c":6) assert borda_scores(votes2) == {"a": 5, "b":4} assert borda_scores (votes3) == 1".": 6, "b": 6, "c":6}
def plurality(votes): Another way to elect a candidate using Ranked Choice Voting is called the "Plurality" method. It assigns one point for being ranked first by a voter. The candidate with the most points wins. This function should take a list of rankings as its input parameter, and it should return a dictionary. The returned dictionary has candidates as keys, and for each key, the associated value is the total number of first-place rankings for that candidate. assert plurality(votes1) == {"a": 2, "c": 1} assert plurality(votes2) == {"a": 2, "b": 1} assert plurality (votes3) == {"a": 1, "b": 1, "c": 1} def pairwise(votes): Some very sophisticated ways of choosing a winner in Ranked Choice Voting depend on viewing the rankings as a bunch one-to-one contests. So, if Alice, Bob, and Carol are all candidates, then we can view this as three different races: Alice vs. Bob, Alice vs Carol, and Bob vs. Carol. This function should take a list of rankings as its input parameter, and it should return a dictionary: The keys will be 2-value tuples of the form (candidatex, candidateY), where candidate here means the name of a candidate. • The value associated with (candidatex, candidatey) will be the count of the number of times that candidatex was ranked above candidateY. . There are no keys of the form (2,2). E.g., there is no key ("Alice", "Alice") because it doesn't make sense to think of Alice being ranked above or below herself. assert pairwise (votes1) == { ("a", "b"): 2, ("a", ""): 2, ("b", "c"): 1, ("C", "6"): 2, ("C", "a"): 1, ("b", "a"): 1, } assert pairwise (votes2) == {("a", "b"): 2, ("b", "a"): 1} assert pairwise (votes 3) == { ("a", "b"): 2, ("a", "c"): 1, ("b", "c"): 2, ("b", "a"): 1, ("c", "a"): 2, ("C", "b"): 1,
def condorcet_winner(votes): Another way to elect a candidate using Ranked Choice Voting is called the "Condorcet" method. It looks to see if there is a candidate that "beat" every other candidate in head-to-head matches. For instance, in a race between Alice, Bob, and Carol, if Alice was ranked above Bob more often than Bob was ranked above Alice, and Alice was ranked above Carol more often than Carol was ranked above Alice, then Alice beat all the other candidates head-to-head, and would be the Condorcet winner. This function should take a list of rankings as its input parameter, and it should return the Condorcet winner, if there is one. If no candidate qualifies as a Condorcet winner, then the function should return None. assert condorcet_winner(votes1) == "a" assert condorcet_winner(votes2) == "" assert condorcet_winner(votes3) == None def winners(scores): This function takes a single dictionary as its parameter. The dictionary maps candidates to their scores. It should return a list of the candidate(s) with the highest score. The return value should be sorted in ascending order using the sort method on lists. assert winners({"a": 1, "6": 2, "c": 3}) == ["c"] assert winners({"a": 1, "6": 1, "c": 1}) == ["a", "b", "c"]
Could someone help me with this program I am struggling with this assignment. It requires Python programing language als
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Could someone help me with this program I am struggling with this assignment. It requires Python programing language als
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!