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!
3 1822
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: -
>>> def vote(votesDict, name):
-
... votes = votesDict.get(name, 0)
-
... votes += 1
-
... votesDict[name] = votes
-
...
-
>>> dd = {}
-
>>> vote(dd, 'barton')
-
>>> def votes(votesDict, name):
-
... votes = votesDict.get(name, 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
-
>>>
Here's one way: -
>>> def vote(votesDict, name):
-
... votes = votesDict.get(name, 0)
-
... votes += 1
-
... votesDict[name] = votes
-
...
-
>>> dd = {}
-
>>> vote(dd, 'barton')
-
>>> def votes(votesDict, name):
-
... votes = votesDict.get(name, 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
-
>>>
That last line could be a tiny bit more efficient and a lot less readable like this: - yield "%s got %d vote%s" %(name, count, ('s', '')[count == 1])
That last line could be a tiny bit more efficient and a lot less readable like this: - 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: -
>>> 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(winners[:-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
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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....
|
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...
|
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...
|
by: abdlah |
last post by:
I have these tables in a database:
Agent(AgentPhone*, AgentName)
Constituency(ConstituencyNo*,ConstituencyName, RegionCode, ConstituencyDistrict)
Party(PartyCode*, PartyName)...
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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)...
| |