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 1829
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: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |