Skip to content

Commit eef79a9

Browse files
committed
Codewars. Who won the election?
1 parent 0ca21f2 commit eef79a9

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ First column is the problem difficulty, in descending order, second links to the
8585
| Level | Remote Link | Local Link |
8686
| ----- | ------------------------------------------- | -------------------------------------------------------- |
8787
| 6kyu | [Convert string to camel case][cdw517abf8]] | [python](codewars/6-kyu-convert-string-to-camel-case.py) |
88+
| 6kyu | [Who won the election?][cdw554910d]] | [python](codewars/6-kyu-who-won-the-election.py) |
8889

8990
[cdw517abf8]: https://www.codewars.com/kata/517abf86da9663f1d2000003/train/python
91+
[cdw554910d]: https://www.codewars.com/kata/554910d77a3582bbe300009c/train/python
9092
[lc1]: https://leetcode.com/problems/two-sum/
9193
[lc3]: https://leetcode.com/problems/longest-substring-without-repeating-characters/
9294
[lc5]: https://leetcode.com/problems/longest-palindromic-substring/
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://www.codewars.com/kata/554910d77a3582bbe300009c/train/python
2+
3+
from collections import defaultdict
4+
5+
6+
def get_winner(ballots):
7+
# default dict with 0
8+
votes = defaultdict(int)
9+
current_winner = None
10+
for ballot in ballots:
11+
# Add the current vote to the candidate
12+
votes[ballot] += 1
13+
# If the candidate has more votes than the current winner, store it
14+
# as the current winner
15+
if votes[ballot] > votes[current_winner]:
16+
current_winner = ballot
17+
# Return the current winner if it has more than half the votes
18+
return current_winner if votes[current_winner] > len(ballots) / 2 else None

0 commit comments

Comments
 (0)