Skip to content

Commit 141fe19

Browse files
Create majority_vote.py
1 parent de0dc99 commit 141fe19

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

majority_vote.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Thu Nov 16 21:04:20 2017
4+
5+
@author: Prince Adeyemi
6+
"""
7+
8+
import random
9+
10+
11+
def majority_vote(votes):
12+
"""Calculate and select majority vote from a list of votes"""
13+
14+
vote_counts = {}
15+
for vote in votes:
16+
if vote in vote_counts:
17+
vote_counts[vote] += 1
18+
else:
19+
vote_counts[vote] = 1
20+
21+
#Choose a winner
22+
winner = []
23+
max_count = max(vote_counts.values())
24+
#loop over vote and count
25+
for vote, count in vote_counts.items():
26+
if count == max_count:ype
27+
winner.append(vote)
28+
29+
return random.choice(winner)

0 commit comments

Comments
 (0)