473,288 Members | 1,745 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,288 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 1822
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.