Collect and Query votes using a Dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DDCane
    New Member
    • Oct 2007
    • 16

    #1

    Collect and Query votes using a Dictionary

    i need to make a dictonary that works like so:

    when u type:

    vote(dict,perso n): it should vote for that person in question by modifying the dictionary and if the person does not exist it creates the person and places a vote for them.

    votes(dict,pers on): it shows how many votes that person in question has and zero if a nmae is tyoed that isnt in the dictionary.

    result(dict): shows who has the most votes.

    hope yall understand. Thanks!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by DDCane
    i need to make a dictonary that works like so:

    when u type:

    vote(dict,perso n): it should vote for that person in question by modifying the dictionary and if the person does not exist it creates the person and places a vote for them.

    votes(dict,pers on): it shows how many votes that person in question has and zero if a nmae is tyoed that isnt in the dictionary.

    result(dict): shows who has the most votes.

    hope yall understand. Thanks!
    Here's one way:[CODE=python]
    >>> def vote(votesDict, name):
    ... votes = votesDict.get(n ame, 0)
    ... votes += 1
    ... votesDict[name] = votes
    ...
    >>> dd = {}
    >>> vote(dd, 'barton')
    >>> def votes(votesDict , name):
    ... votes = votesDict.get(n ame, 0)
    ... return votes
    ...
    >>> votes(dd, 'barton')
    1
    >>> vote(dd, 'barton')
    >>> vote(dd, 'bruce')
    >>> def result(dd):
    ... for (name, count) in dd.items():
    ... yield "%s got %d %s" %(name, count, ('votes', 'vote')[count == 1])
    ...
    >>> for res in result(dd):
    ... print res
    ...
    bruce got 1 vote
    barton got 2 votes
    >>> [/CODE]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Here's one way:[CODE=python]
      >>> def vote(votesDict, name):
      ... votes = votesDict.get(n ame, 0)
      ... votes += 1
      ... votesDict[name] = votes
      ...
      >>> dd = {}
      >>> vote(dd, 'barton')
      >>> def votes(votesDict , name):
      ... votes = votesDict.get(n ame, 0)
      ... return votes
      ...
      >>> votes(dd, 'barton')
      1
      >>> vote(dd, 'barton')
      >>> vote(dd, 'bruce')
      >>> def result(dd):
      ... for (name, count) in dd.items():
      ... yield "%s got %d %s" %(name, count, ('votes', 'vote')[count == 1])
      ...
      >>> for res in result(dd):
      ... print res
      ...
      bruce got 1 vote
      barton got 2 votes
      >>> [/CODE]
      That last line could be a tiny bit more efficient and a lot less readable like this:[CODE=python]yield "%s got %d vote%s" %(name, count, ('s', '')[count == 1])[/CODE]

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        That last line could be a tiny bit more efficient and a lot less readable like this:[CODE=python]yield "%s got %d vote%s" %(name, count, ('s', '')[count == 1])[/CODE]
        After reviewing your question, the last one is not quite as simple as the others.
        I took into account that there might be a tie or ties:[CODE=python]
        >>> vote(dd, 'bruce')
        >>> def result(dd):
        ... winners = [] # handle ties
        ... nVotes = 0
        ... for (name, count) in dd.items():
        ... if count == nVotes: # a tie
        ... winners.append( name)
        ... elif count > nVotes:
        ... winners = [name]
        ... nVotes = count
        ... if len(winners) > 1:
        ... res = (len(winners), ", ".join(winn ers[:-1]), winners[-1], nVotes, ('s', '')[nVotes == 1])
        ... return "There was a %d way tie between %s and %s. Each had %d vote%s" %res
        ... return "%s won with %d vote%s." %(winners[0], count, ('s', '')[count == 1])
        ...
        >>> print result(dd)
        There was a 2 way tie between bruce and barton. Each had 2 votes
        >>> [/CODE]

        Comment

        Working...