Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Elections nacerr Programmer named Tim Posts: 5 Threads: 1 Joined: Apr 2017 Reputation: 0 Likes received: 0 #1 Apr-21-2017, 07:35 AM (This post was last modified: Apr-21-2017, 08:31 AM by Kebap. Edited 1 time in total.) I'm working on a problem here and it's quite a challenge. I just need some clarifications on some of the functions. overview 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 . First task is to open the file contaning the candidates which I have done: ```def getCandidates(f):          try:         filename = open(f,'r')         line = filename.read()         line=[line.replace('\n', ",")]         print(line)              except IOError:         print("File not found.", []) ```Second task parseVote(s) returns the vote from s. Return 0 for an empty vote, and -1 if there are any non-digits (other than spaces) ```def parseVote(s):     if s == []:         print("0")     elif [ s != [0-9] ]:         print("-1")     else:         print(s)       //Not sure if this is really correct. ```I have a problem with the 3rd function that should return 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. def parsePaper(s,n): Could someone explain how to get about the 3rd function? This function uses function 2 The foorth function should just read the votes from a file and pass to the above function. Kebap wrote Apr-21-2017, 08:31 AM:Put code inside python box idontreallywolf Silly Frenchman Posts: 26 Threads: 0 Joined: Apr 2017 Reputation: -3 Likes received: 2 #2 Apr-21-2017, 08:35 AM could you post the file that contains the "votes" (numbers). Would like to try it on my machine nacerr Programmer named Tim Posts: 5 Threads: 1 Joined: Apr 2017 Reputation: 0 Likes received: 0 #3 Apr-21-2017, 09:19 AM (This post was last modified: Apr-21-2017, 09:59 AM by nacerr. Edited 2 times in total.) Hide/Show ``````Output:100,95,,9 ,,92,70 ,,68,57 100,95,11,48, 81,95,66,92,66 ,,20,,33 3, -3, 3 50,,,50 45,88,,62, 44,65,27,70,10 88,94,48,74,6 ,75,95, 88,,61,63,62 ,10,92,12 ,,,,21 ,86,89,, 27,,16,89 62,92,37,52, 0 14,29,55,73,20 18,,84,2,43 ,93 ,16,63, ,,,100 96,22,65,93,75,98,36 ,88,17,63,85 0,0 5,49,31,40,60 9.3 6,75,,55,8 19,79,21,74,20 95,30,50,,58 42,28,5,25,59,30,43 , 7, 9.3, , ,77,74 96,,75 73,35,67,7,80 48,92,40,60, 10,,74 9,88,,14,20 100,,15 ,,,31,91 75,90,44,,10 69,,50,53,41 69,,,, 89,78,48,25,17,49,83 76,93,28,66,47   18,45,56,41,93 ,84, ,96, ,64,47,81,12 43,30,53,72,61,68,98 ,77,,94, 59,78,27,,59 33,,57,, 77,54,78,38, 34,38,86,81, 72,,50, 42,12,,56,41 94,65,24,, ,,74 12, ,84,28,30,91 ,,,,1 36,83,77,75,24 ,39,11,,77 86,88,92,27,55 ,39, 74,14,50,100,44 50,58,18,77,7 ,42,,, ,27,92,,30 76,,3,95,80 88,26,26,100, 96,,80,18 ,83,,32,64 91,,43,18,88 90,19,3,69,63 ,27 15,,93,44, 45,96,28,5,11 27,85,83,59,74 12,24,76,4,59 33,, 3,19,6,87,90 ,,100 4,87,,, ,86,16,84 19,70,,35 ,,60, 94,80,10,21,37 ,,101, 53,19,68,38,80 6,83,45,68,81,52,54 50,25,36,76,39 75,26,,63 2,,10 20,,88,50, ,,,79,80 63,,18,2,          41,72,55,62,0 , 0, , 0 ,92,7 12,13,26,37,93,75,4 55,69,69,46,74,47 5,,54,, no ,88 19,47,,,86 ,83 ,24,,11 ,,12,33,20 ,92,, 89,95,89,92,89,39,69 67,59,51,53,16 82,32,34,5,53,19   ,,84,25,89 17, 52,83,,14, ,,62,31,79 ,99,77 ,19,10,77,16 51,50,,78 83,,54 10,33,10,32, 22,94,27,6,55,23 17,79,,97 ,100,78,93,94 70,10,30,54,8,50,41 68,9,41,80,89 ,15,,, 22,,73,79,16 ,,100 ,42,,97,85 67,6,77,91,54 ,83,17,82,15 75,,59,,13 9, 9, 1e1 ,31,, 74,55,,, 19,85,53,73, 45,49,46,,56 ,10,100,,61 84,49,21,59,75 11,,36,45 37,92,,,65 13,0,25,90,32 ,,70, 88,21,90,,97 ,,80,,41 18,18,,47 46, 20,32, ,35,30,90 73,,,, ,,,,77 77,46,53,8,82 ,,,,35 32,,1,100 72,,94 ,79, 35,0,55,11,89 97,,93,67,22 60,17,14,11,29,26,21 15,,79 ,78,48,60 64,84,, Wales 5-1 Australia    32,,62,     ,22,25 49,47,26,80,11 29,47,40, ,,32,69,81 50,56,,, 54,58,9,100,9 14,0,55,55,92 15,49,16, 85,48,35,38,88,25 24,,61 70,,16,92,82 52,33,43,12,88 56,,7,32 ,56,,23 ,50,71,, 7,13,86,9,55 56,62,60,, 56,,76,18, 26,83,5,81,85    66,,27,1,48 95,,68 63,64,83,51,10,13 38,66,12,58,64 63,85,70,4,5,50 3,87,84,37,19,37 14,,,49,10 ,52,,82 37,14,,97 23,79,40,77,93 17,39,,15,25 19,38,4,64,16 31,5,,11,80 21,11,38,36, 19,47,75,90,66 ,,,57, ,64,, 31,,79,40, ,29,, 23,1,22,33,49 42,,91 0, 0, , , 9,,82,52,36 63,,,45,26 38,,62,42,39 85,50,, ,79,34,,11 ,,1,, ,,,,31 36,95,100,50, ,,28 ,40,53,, 12,,80, ,85,37,40, -8 ,52,,,94 ,95,74,73,32 99,74,68,16,58 37,, ,77,28, 80,,90,, 8,, 76,48,,6,32 59, 28,21,62,87,76     ,38 96,84,71,,48 ,37,49 ,83,,53 7,,11,4,51 ,77,15,0,20 100,80,15,74,49,7 ,,5,,84 25,45,,86,34 ,70,53,18, ,,56,, ,86 ,,,,25    ,0 35,54,5,60,70 0,28,65,38,28 55,,44,46,31 ,32,19,,99 20,0,50,23,86 16,83,66,38,89 14,0,64,49,62 who are these candidates? 85,33,38, 24,12,57,94, 33,13,80,61,88 99,,24,37 13,96, ,,,,75 ,,,66, ,47, 82,91,52,60, 79,,18,55,10 33,,76, ,30,84 93,43,5,26,5 ,71 51,,25,40,41 ,,,,19 96,41,15,35,79 99,91,38,,100 ,,,35, 10,,59,7, 88, 99,30,,52,97 21,10,96,72,71,6 27,60,98,28,15,58 72,15,81,97,5,57,88 ,82,0,,17 85,,, 93,48,45,,``````Not sure how to upload a file, I can't find a link to do that. Just copied the whole file for u. Cheers. Note: Each line represents one person's vote for different candidates. Criteria for formal and informal vote: Formal votes: "3, 5, 0, 2" normalises to [0.3, 0.5, 0.0, 0.2]. "3, 5, 0" normalises to [0.375, 0.625, 0.000, 0.000]. "3, 5" normalises to [0.375, 0.625, 0.000, 0.000]. "3" normalises to [1.0, 0.0, 0.0, 0.0]. ",3, 5" normalises to [0.000, 0.375, 0.625, 0.000]. ",3,, 5" normalises to [0.000, 0.375, 0.000, 0.625]. " ,, , 3" normalises to [0.0, 0.0, 0.0, 1.0]. Informal votes: With n candidates, any paper containing more than n votes is informal. Any paper casting no votes is informal, e.g. "" "," "0,0,0,0" Any paper without proper separators (i.e. commas) is informal, e.g. "1 2". Negative votes are not allowed, e.g. "5, -6, 4" is informal. Floating point votes are not allowed, e.g. "5, 6.7, 4" is informal. Any paper containing anything other than whitespace, digits, and commas is inform nacerr Programmer named Tim Posts: 5 Threads: 1 Joined: Apr 2017 Reputation: 0 Likes received: 0 #4 Apr-21-2017, 01:40 PM ```def getCandidates(f):          try:         filename = open(f,'r')         line = filename.readlines()                  print(line)               except IOError:         print("File not found.", [])      def parseVote(s):     if s == []:         print("0")     elif [ s != [0-9] ]:         print("-1")     else:         print(s)    def parsePaper(s,n):     n=0     for line in open(s):         n += 1               def getPapers(f,n):     try:         filename = open(f,'r')         line = filename.read()         line=[line.replace('\n', ",")]         print( [line,n] )                   except IOError:         print("File not found.", f)         def main():     candidate=input("Enter candidate file: ")     getCandidates(candidate)          papers=input("Enter papers file:")     n=len(papers)     getPapers(papers,n)           main()          ``` PythonHatesMe Unladen Swallow Posts: 2 Threads: 0 Joined: Apr 2017 Reputation: 0 Likes received: 0 #5 Apr-25-2017, 06:24 AM I have the exact same problem and I'm with you it's hard stuff did you manage to figure out how to do part 2 the parseVotes section? nacerr Programmer named Tim Posts: 5 Threads: 1 Joined: Apr 2017 Reputation: 0 Likes received: 0 #6 Apr-25-2017, 06:27 AM (Apr-25-2017, 06:24 AM)PythonHatesMe Wrote: I have the exact same problem and I'm with you it's hard stuff did you manage to figure out how to do part 2 the parseVotes section? Yh, I got this. ``` def parseVote(s):     if s == "":         return 0     elif s.isdigit():         return int(s)     else:         return -1 ``` PythonHatesMe Unladen Swallow Posts: 2 Threads: 0 Joined: Apr 2017 Reputation: 0 Likes received: 0 #7 Apr-25-2017, 06:40 AM Yeah I tried something similar and sadly it didn't work good luck with it I don't think I have much chance of finishing nacerr Programmer named Tim Posts: 5 Threads: 1 Joined: Apr 2017 Reputation: 0 Likes received: 0 #8 Apr-25-2017, 06:43 AM goodluck to you too mate. « Next Oldest | Next Newest »

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)