Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use python please thank you You are to write the following Python functions. M sure you understand where each function fits into the required system

Use python please thank you image text in transcribed
You are to write the following Python functions. M sure you understand where each function fits into the required system described above. def getCandidates(f): getCandidates(f) returns a list containing the candidates' names from the 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"]. 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. 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) = ([]m "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. 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 ("small file.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. def normalisePaper (p, n): sum (p) > 0 normalise Paper (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] def normalise Papers (ps, n): for every p on ps, sum (p) > 0 normalise Papers (ps, n) returns pos with each paper normalised, in an election with n candidates. eg, normalisePapers ([[2], [7, 2, 1]], 3) = [[1.0, 0.0, 0.0], [0.7, 0.2, 0.1]]. normalisePapers will use normalisePaper

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Using Entity Relationship Diagrams

Authors: Sikha Saha Bagui, Richard Walsh Earp

3rd Edition

103201718X, 978-1032017181

More Books

Students also viewed these Databases questions