鱼C论坛

 找回密码
 立即注册
查看: 3290|回复: 16

project求助。

[复制链接]
回帖奖励 4 鱼币 回复本帖可获得 1 鱼币奖励! 每人限 1 次(中奖概率 10%)
发表于 2017-4-25 20:34:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-4-25 20:41:11 | 显示全部楼层
这是我目前的code

f = 'candidates.txt'
def getCandidates(f):
    a = open('candidates.txt','r')
    s = []
    for i in range(5):
        s.append(a.readline().strip('\n'))
    print(s)

   
def parseVote(s):
    newlist = []
    list = s.split(",")
    for each in list:
        if each == "" or each == " ":
            newlist.append(0)
        elif(int(each)>=0):
            newlist.append(int(each))
        else:
            newlist.append(int(-1))
    return newlist

def ParsePaper(s,n):
  thelist = []
    #with open("papers.txt","r") as x:
    #line = x.readlines()
  for line in lines:
    k = line.replace(",","").replace(" ","").replace(", ","")
    try:
      if int(k):
        thelist.append(line)
    except:
      pass
  return thelist

def getPapers(f,n):


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 20:42:49 | 显示全部楼层
condidates.txt

Major Clanger
Soup Dragon
Froglet
Iron Chicken
The Cloud
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 20:43:36 | 显示全部楼层
papers.txt的部分内容

60,17,14,11,29,26,21
15,,79
,78,48,60
64,84,,
Wales 5-1 Australia
  
32,,62,
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:28:14 | 显示全部楼层
能不能说明白点
你到底想让我们干什么
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 21:33:17 | 显示全部楼层
新手·ing 发表于 2017-4-25 21:28
能不能说明白点
你到底想让我们干什么

就是用这9个function.。。。 我把自己写的贴上去了。。我现在的问题就是我不会把第2,3,4个 function写出来。 他的逻辑是第四个call第三个,第三个call第二个。。 我不晓得这个该怎们写才能这样一个call一个。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 21:53:30 | 显示全部楼层
本帖最后由 ooxx7788 于 2017-4-25 21:59 编辑
Jc嘻嘻 发表于 2017-4-25 21:33
就是用这9个function.。。。 我把自己写的贴上去了。。我现在的问题就是我不会把第2,3,4个 function写 ...

5
def normalisePaper(p, n):
    result = [each / sum(p) for each in p] if len(p) >= n else [each / sum(p) for each in p] + [0 for _ in
                                                                                                range(n - len(p))]
    return [round(_, max([len(str(each)) for each in result])) for _ in result]

6
def normalisePapers(ps, n):
    return [normalisePaper(each, n) for each in ps]

小数结尾的精度不能实现。
浮点型不支持小数后面补全0满足精度。而你这个要的是不是字符型。解决不了。
而且你也没说,p大于n的时候怎么办。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-25 22:07:30 | 显示全部楼层
ooxx7788 发表于 2017-4-25 21:53
小数结尾的精度不能实现。
浮点型不支持小数后面补全0满足精度。而你这个要的是不是字符型。解决 ...

可以帮我看看第三个和第四个吗?
我第二个我测试了可以用,怎么用第三个call第二个 ,再用第四个call第三个哦
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-25 22:22:08 | 显示全部楼层
Jc嘻嘻 发表于 2017-4-25 22:07
可以帮我看看第三个和第四个吗?
我第二个我测试了可以用,怎么用第三个call第二个 ,再用第四个call第 ...

2写的就是错的,自己好好理解理解吧。作业全帮你做了,你也没进步。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-26 09:58:51 | 显示全部楼层
昨天也比较晚了,懒得给你说了,第二个函数的主要问题在于,返回的类型不对,题目的要求是返回数值型,你返回的却是列表,所以如果想在第3个函数里面调用,那还需要再处理。至于怎么改,还是留给你自己想。

对于你不懂的函数调用写个伪码给你看看。
def func1():
        pass

def func2():
        pass

在第3个函数调用前面两个函数就是:
def func3():
        func1()
        func2()
并没有什么不同。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-26 10:09:23 | 显示全部楼层
ooxx7788 发表于 2017-4-26 09:58
昨天也比较晚了,懒得给你说了,第二个函数的主要问题在于,返回的类型不对,题目的要求是返回数值型,你返 ...

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-26 10:17:01 | 显示全部楼层
ooxx7788 发表于 2017-4-26 09:58
昨天也比较晚了,懒得给你说了,第二个函数的主要问题在于,返回的类型不对,题目的要求是返回数值型,你返 ...

唉,其实也不是我想问, 我这学期课, matlab, java, R, 统计,python. 选错了课, 我java还有点基础,主要全是编程语言。 而且我就上了三周,这个教授就出这种题, 这种project在java里面至少6-8周的作业。他就教了我们一个def function。都是单个的函数, 从来没有给一个例子如何套用几个函数。  我也是没办法, 朋友推荐这论坛的。      挺无奈的, 选错了课。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-26 10:28:11 | 显示全部楼层
ooxx7788 发表于 2017-4-26 09:58
昨天也比较晚了,懒得给你说了,第二个函数的主要问题在于,返回的类型不对,题目的要求是返回数值型,你返 ...

  如果第二个方程是返回数值型, 但是第三个方程又需要list, 那如果我第二个不返list 怎么才能把数套入第三个呢。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-26 10:50:32 | 显示全部楼层

回帖奖励 +1 鱼币

Jc嘻嘻 发表于 2017-4-26 10:28
如果第二个方程是返回数值型, 但是第三个方程又需要list, 那如果我第二个不返list 怎么才能把数套入第 ...

parsePaper你肯定是要把列表部分先用split(",")切开,形成一个列表,这样列表中的每一个元素都是一个字符串型。首先根据parsePaper的第二个参数判断列表,把blank,non-digits,too long的先返回出来。
然后使用for循环调用parseVote函数,分别返回一个值。这个值再组成一个列表形成了parsePaper返回的列表部分。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-4-26 14:53:09 | 显示全部楼层
ooxx7788 发表于 2017-4-26 10:50
parsePaper你肯定是要把列表部分先用split(",")切开,形成一个列表,这样列表中的每一个元素都是一个字符 ...

def parseVote(s):
    x = x.strip()
    a = len(x)
    if a == 0:
        return("0")
    space = sum(c.isspace() for c in x)
    if space>0:
        return("-1")  # judging 1 5   .  a space in the middle
    if x.isdigit():
        if int(x)>0:
            return(x)
        else:
            retun("-1")
   

parseVote这样写是对的吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-26 14:57:03 | 显示全部楼层
Jc嘻嘻 发表于 2017-4-26 14:53
def parseVote(s):
    x = x.strip()
    a = len(x)

我的理解,按照你的题目的原意,return 0/-1/15就可以了,不需要转成字符型。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2017-4-26 15:14:43 | 显示全部楼层
ooxx7788 发表于 2017-4-26 14:57
我的理解,按照你的题目的原意,return 0/-1/15就可以了,不需要转成字符型。

那只是个例子吧,第三个要call这个函数,,所以s是不确定的。 可以为15 也可以为任意数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 18:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表