|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Jc嘻嘻 于 2017-4-24 14:15 编辑
题目,
三个同学评选班干部, 我把他们存在一个txt 文件中, a,b,c3个同学
另外的同学给他们打分, 我把分数存在另一个txt文件中, 比如 2,3,4 2是a的分数, 3b同学的分数 4c同学的分数
我的意思就是 现在有三个同学, 其他同学(不管多少个) 给他们三个按顺序打分。
1,2,3
4,1,1
5,6,2
3,4,5
,,1
0,1,2
删掉我
1,2,1
。。。
可以有无数个。
但是这其中有 ,, 有文字, 我需要删掉其中的文字 ,如果是,, 我需要把中间的数字改成0
比如,,1
我要改成0,0,1
然后我把每竖排加起来
然后用sort
比较大小 最后选出得票最多的那个人。
我才学这个,,好多搞不懂 这是我目前写的 求各位帮助
x = 'candidates.txt'
def getCandidates(x):
a = open('candidates.txt','r')
s = []
for i in range(5):
s.append(a.readline().strip('\n'))
print(s)
getCandidates(x)
y = 'papers.txt'
def getPapers(y): #我想先读这个分数文件,
a = open('papers.txt','r')
# for line in a:
# print(line[:-1])
x = a(i);
def parseVote(s): 然后我想def另一个函数, 这个函数用来判断 ,, 文字 还是怎样,可是我卡在这里了。。
for i in a:
if(len(x)==0):
a.remove(i)
a.close()
parseVote(y)
以下是我需要用到的function ...我理解这个东西 但是喊我写 感觉都是蒙的 求指导!
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. |
|