Problem overview
The method countVote gets votes, a dictionary from names to how many votes each person has, and the name on a new vote. It counts the new vote and returns votes. The person called name gets one more vote. A name with no votes yet isn't in the dictionary: it starts from 0, so after this vote it has 1. The dictionary may be empty, and the order of the entries doesn't matter.
Examples:
{"Ana": 3, "Ben": 5}with"Ana"gives{"Ana": 4, "Ben": 5}.- The same dictionary with
"Cara"gives{"Ana": 3, "Ben": 5, "Cara": 1}, since she starts from 0. {}with"Max"gives{"Max": 1}.
Approach: read with a default, add one, store
There are two cases, a known name and a new one, but they follow the same steps if the new name is treated as having 0 votes. votes.get(name, 0) gives the current count for a known name and 0 for a new one, without an error. Add 1 to that and store the result under name. For a known name, the store replaces the old count. For a new name, it adds the key.
Reading with votes[name] would fail for a new name with a KeyError, and that is exactly the case the default avoids.
Step by step
- Read the current count into
countwithvotes.get(name, 0). - Store
count + 1under the keyname:votes[name] = count + 1. - Return
votes. Only the count ofnamechanged, and every other entry is as it was.
The solution in Python
class Solution:
def countVote(self, votes: Dict[str, int], name: str) -> Dict[str, int]:
count = votes.get(name, 0)
votes[name] = count + 1
return votes
On the three examples it returns:
{'Ana': 4, 'Ben': 5}
{'Ana': 3, 'Ben': 5, 'Cara': 1}
{'Max': 1}
The two lines of the body can be written as one, votes[name] = votes.get(name, 0) + 1, which does the same thing. Splitting them gives the old count a name. The dictionary passed in is changed in place and returned.
Edge cases and common mistakes
- Using brackets to read.
votes[name] += 1works for Ana but raisesKeyError: 'Cara'for a name that has no votes yet. - Using
getwithout a default.votes.get(name)givesNonefor a new name, andNone + 1fails with aTypeError. - Storing 1 for everyone. Writing
votes[name] = 1would reset Ana's 3 votes to 1. - Forgetting to store. Computing
count + 1and not assigning it leaves the dictionary unchanged.
Another way: a counting dictionary
Python's standard library has defaultdict, which gives 0 automatically for a missing key when created with int. It is handy in programs that count a lot, but the lesson passes a plain dictionary, so get is the tool here.
from collections import defaultdict
dd = defaultdict(int)
dd["Max"] += 1
print(dict(dd))
{'Max': 1}
Testing
Call the method three times in a row on an empty dictionary with the name "Max". The results are {'Max': 1}, {'Max': 2} and {'Max': 3}: the first vote creates the entry with 1, and each later one raises it by one. A vote for another name afterwards, such as "Zoe", gives {'Max': 3, 'Zoe': 1}, and Max's count is untouched.
Because the same dictionary is changed and returned each time, the three results above come from one dictionary growing, not from three separate ones. Copy the dictionary first (with dict(votes)) if you want to keep an earlier state.
