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

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Elections
#1
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
Quote
#2
could you post the file that contains the "votes" (numbers). Would like to try it on my machine
Quote
#3
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
Quote
#4
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()    


    


Quote
#5
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?
Quote
#6
(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

Quote
#7
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
Quote
#8
goodluck to you too mate.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)