Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs1-25sp
lecture-code
Commits
2727c49c
Commit
2727c49c
authored
2 months ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
Add new file
parent
1ca63891
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
08/voting.py
+73
-0
08/voting.py
with
73 additions
and
0 deletions
+73
-0
08/voting.py
0 → 100644
View file @
2727c49c
import
math
def
process_votes
()
->
tuple
[
dict
[
str
,
list
[
str
]],
str
|
None
]:
"""
Takes in votes from the voters, and processes the winner.
Returns:
dict[str, list[str]]: A dictionary where the keys are the possible
options someone could vote for. The value associated with each option
is a list of people who voted for that option.
"""
vote_tracker
:
dict
[
str
,
list
[
str
]]
=
{}
done
:
bool
=
False
while
...:
name
:
str
=
input
(
"What is your name? "
)
choice
:
str
=
input
(
"Hi there "
+
name
+
". Which ice cream flavor is your favorite? "
)
...
more_votes
:
str
=
input
(
"Your vote has been registered. Are there more votes to process? Y/N "
)
if
more_votes
==
"N"
:
done
=
True
#Determine the winning option
max_votes
=
...
winning_option
=
...
for
option
in
vote_tracker
:
num_votes
=
...
if
...:
max_votes
=
...
winning_option
=
...
return
(
vote_tracker
,
winning_option
)
# results = process_votes()
# print(votes[0])
# print(votes[1])
def
highest_vote_count
(
vote_tracker
:
dict
[
str
,
list
[
str
]])
->
str
|
None
:
"""
Identifies the person who voted the most times across all options.
Args:
vote_tracker (dict[str, list[str]]): A dictionary where the keys are
the possible options someone could vote for. The value associated
with each option is a list of people who voted for that option.
Returns:
str: The name of the person who appears most frequently across all
voting lists. If there's a tie, one of the top voters is returned
arbitrarily.
"""
vote_counter
:
dict
[
str
,
int
]
=
{}
for
voters
in
vote_tracker
.
values
():
for
person
in
voters
:
if
person
in
vote_counter
:
vote_counter
[
person
]
+=
1
else
:
vote_counter
[
person
]
=
1
max_votes
=
-
math
.
inf
top_voter
=
None
for
person
in
vote_counter
:
if
vote_counter
[
person
]
>
max_votes
:
max_votes
=
vote_counter
[
person
]
top_voter
=
person
return
top_voter
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help