473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Collect and Query votes using a Dictionary

16
i need to make a dictonary that works like so:

when u type:

vote(dict,person): 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,person): 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!
Oct 25 '07 #1
3 1826
bartonc
6,596 Expert 4TB
i need to make a dictonary that works like so:

when u type:

vote(dict,person): 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,person): 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:
Expand|Select|Wrap|Line Numbers
  1. >>> def vote(votesDict, name):
  2. ...     votes = votesDict.get(name, 0)
  3. ...     votes += 1
  4. ...     votesDict[name] = votes
  5. ...     
  6. >>> dd = {}
  7. >>> vote(dd, 'barton')
  8. >>> def votes(votesDict, name):
  9. ...     votes = votesDict.get(name, 0)
  10. ...     return votes
  11. ... 
  12. >>> votes(dd, 'barton')
  13. 1
  14. >>> vote(dd, 'barton')
  15. >>> vote(dd, 'bruce')
  16. >>> def result(dd):
  17. ...     for (name, count) in dd.items():
  18. ...         yield "%s got %d %s" %(name, count, ('votes', 'vote')[count == 1])
  19. ...         
  20. >>> for res in result(dd):
  21. ...     print res
  22. ...     
  23. bruce got 1 vote
  24. barton got 2 votes
  25. >>> 
Oct 25 '07 #2
bartonc
6,596 Expert 4TB
Here's one way:
Expand|Select|Wrap|Line Numbers
  1. >>> def vote(votesDict, name):
  2. ...     votes = votesDict.get(name, 0)
  3. ...     votes += 1
  4. ...     votesDict[name] = votes
  5. ...     
  6. >>> dd = {}
  7. >>> vote(dd, 'barton')
  8. >>> def votes(votesDict, name):
  9. ...     votes = votesDict.get(name, 0)
  10. ...     return votes
  11. ... 
  12. >>> votes(dd, 'barton')
  13. 1
  14. >>> vote(dd, 'barton')
  15. >>> vote(dd, 'bruce')
  16. >>> def result(dd):
  17. ...     for (name, count) in dd.items():
  18. ...         yield "%s got %d %s" %(name, count, ('votes', 'vote')[count == 1])
  19. ...         
  20. >>> for res in result(dd):
  21. ...     print res
  22. ...     
  23. bruce got 1 vote
  24. barton got 2 votes
  25. >>> 
That last line could be a tiny bit more efficient and a lot less readable like this:
Expand|Select|Wrap|Line Numbers
  1. yield "%s got %d vote%s" %(name, count, ('s', '')[count == 1])
Oct 25 '07 #3
bartonc
6,596 Expert 4TB
That last line could be a tiny bit more efficient and a lot less readable like this:
Expand|Select|Wrap|Line Numbers
  1. yield "%s got %d vote%s" %(name, count, ('s', '')[count == 1])
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:
Expand|Select|Wrap|Line Numbers
  1. >>> vote(dd, 'bruce')
  2. >>> def result(dd):
  3. ...     winners = []  # handle ties
  4. ...     nVotes = 0
  5. ...     for (name, count) in dd.items():
  6. ...         if count == nVotes: # a tie
  7. ...             winners.append(name)
  8. ...         elif count > nVotes:
  9. ...             winners = [name]
  10. ...             nVotes = count
  11. ...     if len(winners) > 1:
  12. ...         res = (len(winners), ", ".join(winners[:-1]), winners[-1], nVotes, ('s', '')[nVotes == 1])
  13. ...         return "There was a %d way tie between %s and %s. Each had %d vote%s" %res
  14. ...     return "%s won with %d vote%s." %(winners[0], count, ('s', '')[count == 1])
  15. ... 
  16. >>> print result(dd)
  17. There was a 2 way tie between bruce and barton. Each had 2 votes
  18. >>> 
Oct 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: MM | last post by:
Hi, I don't know PHP..i use dreamweaver and now i realise it is far from enough. This code selects from a previous page where the users entered some votes and select the 7 products they vote the...
5
by: andreas.muller | last post by:
Hello everyone, I'm trying to solve this problem but can't seem to figure out how to start. I would like to create a rating system where people can vote (1-5 stars) on randomly displayed items....
3
by: Billy Boone | last post by:
Having trouble getting the right sql query. I have several tables: students (information about the students) - id, Name, Grade, 123456, John Public, 09 654321, Mary Smieth, 09 candidates...
21
by: Tarscher | last post by:
Hi all, I have events containing attendees (events has many attendees). The attendee table tells whether a user will attend the event or not. I want to build a query that returns all the...
1
by: abdlah | last post by:
I have these tables in a database: Agent(AgentPhone*, AgentName) Constituency(ConstituencyNo*,ConstituencyName, RegionCode, ConstituencyDistrict) Party(PartyCode*, PartyName)...
3
by: oravm | last post by:
Hi, I re-write a query and used bulk collect to improve the performance of the batch process. The query below has NO compile error but when execute query there is error 'ORA-01403: no data...
1
by: HSXWillH | last post by:
I hope my question here is clear. I have a table GovVotes that contains the following fields: Year/State/ElectionType/Candidate/Party/PopularVotes. Via net-searching, I found a code for a query...
3
by: shapper | last post by:
Hello, I have three tables: Polls (PollId, Question), Options (OptionID, Answer) and Votes (VoteID, OptionID) I then created two Wrapper Classes: PostPaper with the following properties:...
11
beacon
by: beacon | last post by:
Hi everybody, I created a database that links one table from an ODBC data source. I saved my password and UID to the data source so neither myself nor anyone else would have to login each time...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.