|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我学python三周,教授就讲了一些基础的东西,昨天刚讲了如何def function,就给我们出了一个project. 下面是题干。
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 #[题干的意思就是ZF要选举干部,人民投票 (教授给了2个txt文档,一个是txt文档里面是5个干部的名字; 另外一个全是投票数据,我的难题就是处理这个投票数据,因为里面有各种格式, ,, 文字 空格之类的,我必须处理掉这些格式,下面教授给了9个函数来解决这些问题,可是我不太会用,我才开始学,真的搞不清楚。
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个函数
下面是教授给的9个必须用的函数。
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"], [55, "A"]].
8.def printCount(c):
printCount(c) displays the election count c, i.e. the result from countVotes. See the sample output for the required foat.
9.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.
|
|