|
楼主 |
发表于 2017-4-25 17:53:49
|
显示全部楼层
我之前问的问题都是片段,我整体说一下把。
这个的原因是 无论是小数还是负数, 格式都是不对的, 意思就是一旦又小数或者负数出现就当成不是数字来处理。
我贴一下题干哈
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. Example ballot papers and their interpretations are provided.
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 (example here), and a file of voters' completed ballot papers (example here), and that conducts the counting process for that election (results from the examples here).
然后他给除了下面的9个方程喊我们用。
第一个我会,
第二个你已经告诉我了,我测试了也可以用,但是这个放出2,3,4 是一个组合, call 第四个方程,第2,3个应该工作,我就懵了 , 例子我看得懂但是我确实不晓得咋写。 我问教授,他喊我们自己解决。 真的搞不懂。 然后第5,6个方程也是一个组合, 第6个包含第5个。 我现在的难题就是,我不太会这个方程套用,以前喊我们做作业都是一个一个简单的小方程。
你看了过后可以帮我理下思路嘛,,我弄这个课比我其他课时间多多了, 可能太愚了,确实做不下去。 那个教授说就算过程错了, main函数里面必须包括 getCandidates, getPapers, normalisePapers, countVotes, and printCount. 这5个函数。 不然扣分。
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"].
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) = ([ ], "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("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.
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]
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.
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"]].
def printCount(c):
printCount(c) displays the election count c, i.e. the result from countVotes. See the sample output for the required format.
def main():
main() prompts the user for the names of the necessary files, then conducts the election. See the sample output for the required output.
main will use getCandidates, getPapers, normalisePapers, countVotes, and printCount.
|
|