-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmajority_vote.py
45 lines (34 loc) · 1.24 KB
/
majority_vote.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
"""Majority Vote.
Create a function that returns the majority vote in a list.
A majority vote is an element that occurs > N/2 times in a list
(where N is the length of the list).
Source:
https://edabit.com/challenge/pQavNkBbdmvSMmx5x
"""
from collections import Counter
def majority_vote(iterable):
"""Find the majority voted item in iterable.
Majority vote is an element that occurs > N/2 times.
N = length of iterable.
"""
cnt = Counter(iterable)
length = len(iterable)
majority = [item for item, count in cnt.items() if count > length/2]
if majority:
return majority[0]
return None
def main():
assert majority_vote(["A", "B", "B", "B", "A", "A"]) is None
assert majority_vote(["B", "B", "B"]) == "B"
assert majority_vote(["A", "B", "B"]) == "B"
assert majority_vote(["A", "A", "B"]) == "A"
assert majority_vote(["A", "A", "A", "B", "C", "A"]) == "A"
assert majority_vote(["B", "A", "B", "B", "C", "A", "B", "B"]) == "B"
assert majority_vote(["A", "B", "B", "A", "C", "C"]) is None
assert majority_vote(["A", "B"]) is None
assert majority_vote(["A"]) == "A"
assert majority_vote([]) is None
print("Passed.")
if __name__ == "__main__":
main()