Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use python please, thank you You are required to write the following Python func

ID: 3823280 • Letter: U

Question

Use python please, thank you You are required to write the following Python functions. Make sure you understand where each function fits into the required to 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 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, 21], ""), parsePaper(", , ", 4) = parsePaper ("0, 0", 4) = ([], "blank"), parsePaper ("4, -8, 0", 4) = parsePaper("4, 7.8, 0", 4) = parsePaper 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-digit"), ([], "non-digits"), ([4, 0, 4, 4], ""), ([], "too long"), ([], "blank")). 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.01], [0.7, 0.2, 0.1]], normalisePapers will use normalisePaper.

Explanation / Answer


import project1
import os.path


#a non-existent file
nonfile = "nofile.txt"

def hackb(xs):
    if xs == [] or xs[-1][1] != "blank":
        return xs
    else:
        return xs[:-1]


def test_getCandidates():
    f = "candidates.txt"
    if os.path.exists(f):
        return [(project1.getCandidates(x), y) for (x, y) in
                [(f, ["Major Clanger", "Soup Dragon", "Froglet", "Iron Chicken", "The Cloud"])
                ,(nonfile, [])]]
    else:
        print("You need to download file", f, end = " ")
        return []

def test_parseVote():
    return [(project1.parseVote(x), y) for (x, y) in
            [("", 0), ("no", -1), ("27", 27)]]

def test_parsePaper():
    return [(project1.parsePaper(x, y), z) for (x, y, z) in
            [(",,, ",      4, ([],         "blank"))
            ,(" 3,4,5 ", 2, ([],         "too long"))
            ,("4,-4,4",    3, ([],         "non-digits"))
            ,("4,5, 16",   6, ([4, 5, 16], ""))
            ,("9876",      8, ([9876],     ""))]]

def test_getPapers():
    f = "smallfile.txt"
    if os.path.exists(f):
        nd = ([], "non-digits")
        b = ([], "blank")
        tl = ([], "too long")
        return [(hackb(project1.getPapers(x, 4)), y) for (x, y) in
                [(f, [([1,2,3,4], ""), b, ([0,23,0], ""), nd, nd, ([4,0,4,4], ""), tl])
                ,(nonfile, [])]]
    else:
        print("You need to download file", f, end = " ")
        return []

def test_normalisePaper():
    return [(project1.normalisePaper(x, y), z) for (x, y, z) in
            [([2,1,5], 3, [0.25, 0.125, 0.625])
            ,([8,2],   4, [0.8, 0.2,   0,    0])
            ,([7],     4, [1,    0,     0,    0])]]

def test_normalisePapers():
    return [(project1.normalisePapers(x, y), z) for (x, y, z) in
            [([[2,1,5], [8,2]], 3, [[0.25,0.125,0.625], [0.8,0.2,0]])]]

def test_countVotes():
    return [(project1.countVotes(cs, ps), x) for (cs, ps, x) in
            [(["A", "B", "C"], [[0,0,1], [0,1,0], [0,0,1]], [[2,   "C"], [1,   "B"], [0,   "A"]])
            ,(["A", "B", "C"], [[0.7, 0.2, 0.1]],           [[0.7, "A"], [0.2, "B"], [0.1, "C"]])]]

def test_printCount():
    return []

def test_main():
    return []


def msg(f, z):
    bs = [x == y for (x, y) in z]
    if bs == []:
        s = "untested"
    elif all(bs):
        s = "all " + str(len(bs)) + " test(s) correct"
    else:
        zs = [k for k in range(len(bs)) if not bs[k]]
        s = "These tests incorrect: " + str(zs)[1:-1]
    print("%20s" % (f + ":"), s)

msg("getCandidates",      test_getCandidates())
msg("parseVote",          test_parseVote())
msg("parsePaper",         test_parsePaper())
msg("getPapers",          test_getPapers())
msg("normalisePaper",     test_normalisePaper())
msg("normalisePapers",    test_normalisePapers())
msg("countVotes",         test_countVotes())
msg("printCount",         test_printCount())
msg("main",               test_main())
print()
input("Hit Enter to finish: ")