You are required to write the following python functions


Overview

The Government of Nerdvana conducts its elections using an unusual system. Each constituency elects one member to the Nerdvanian Parliament, and each voter casts one ballot in which they can assign any non-negative number of votes to each candidate. Each ballot paper is normalised to a total value of 1 before being counted: for example, given four candidates, the ballot paper

5, 0, 8, 7

would be normalised to

0.25, 0.00, 0.40, 0.35

Any paper that does not fit the above description is declared informal and is disregarded in the count.

Once each paper has been normalised, the count operates via a straightforward first-past-the-post system: add up the normalised vote for each candidate, and whoever has the highest total wins the seat.

In this project you will write a Python program that reads in a file of Nerdvanian candidates, and a file of voters' completed ballot papers (example here), and that conducts the counting process for that election.

Tasks

You are required to write the following Python functions. Make sure you understand where each function fits into the system described above.

1. def getCandidates(f):
getCandidates(f) returns a list containing the candidates' names from the file f. The names will be one per line with no extraneous characters. Disregard any blank lines in f. If f doesn't exist, print an appropriate error message and return the empty list.
For example, getCandidates("candidates.txt") = ["Major Clanger", "Soup Dragon", "Froglet", "Iron Chicken", "The Cloud"].

2. def parseVote(s):
parseVote(s) returns the vote from s. Return 0 for an empty vote, and -1 if there are any non-digits (other than spaces). For example,
parseVote("") = parseVote(" ") = 0,
parseVote("-3") = parseVote("no") = parseVote("1 5") = -1,
parseVote("15") = parseVote(" 15 ") = 15.

3. def parsePaper(s, n):
parsePaper(s, n) returns the votes from the ballot paper s in an election with n candidates, plus an error message if appropriate. If s is formal, return the list of numbers found in s and the empty string; if s is informal, return an empty list of numbers and the appropriate string below.
For example:
parsePaper("14, , 2", 4) = ([14, 0, 2], ""),
parsePaper(", , ", 4) = parsePaper("0, 0", 4) = ([ ], "blank"),
parsePaper("4, -8, 0", 4) = parsePaper("4, 7.8, 0", 4) = parsePaper("pointless, 5, 5", 4) = ([ ], "non-digits"),
parsePaper("1,2,,4,5", 4) = ([ ], "too long").

parsePaper will use parseVote.

4. def getPapers(f, n):
getPapers(f, n) returns a list containing the ballot papers from the file f, in an election with n candidates. Treat each line of the file as a separate paper. If f doesn't exist, print an appropriate error message and return the empty list. For example:
getPapers("smallfile.txt", 4) = [([1, 2, 3, 4], ""), ([], "blank"), ([0, 23, 0], ""), ([], "non-digits"), ([], "non-digits"), ([4,0,4,4], ""), ([], "too long"), ([], "blank")].
smallfile.txt is available from the project web-site.

getPapers will use parsePaper.

5. def normalisePaper(p, n): # sum(p) > 0
normalisePaper(p, n) returns p with each vote scaled according to its total, and padded to contain n votes. For example:
normalisePaper([1,2,3,4], 4) = [0.1, 0.2, 0.3, 0.4], normalisePaper([2], 3) = [1.0, 0.0, 0.0], normalisePaper([0, 4, 496], 3) = [0.000, 0.008, 0.992]

6. def normalisePapers(ps, n): # for every p on ps, sum(p) > 0
normalisePapers(ps, n) returns ps with each paper normalised, in an election with n candidates.
e.g. normalisePapers([[2], [7, 2, 1]], 3) = [[1.0, 0.0, 0.0], [0.7, 0.2, 0.1]].

normalisePapers will use normalisePaper.

7. def countVotes(cs, ps): # ps have been normalised for an election with len(cs) candidates
countVotes(cs, ps) returns a list of lists containing the counts for the candidates cs from the ballot papers ps, in descending order of total number of votes. For example, countVotes(["A", "B", "C"], [[0.5, 0.5, 0], [0.05, 0.3, 0.65]]) = [[0.8, "B"], [0.65, "C"], [0.55, "A"]].

8. def printCount(c):
printCount(c) displays the election count c, i.e. the result from countVotes.

9. def main():
main() prompts the user for the names of the necessary files, then conducts the election.

main will use getCandidates, getPapers, normalisePapers, countVotes, and printCount.

Request for Solution File

Ask an Expert for Answer!!
Python Programming: You are required to write the following python functions
Reference No:- TGS02880858

Expected delivery within 24 Hours