Minnesota is holding elections on the day that this assignment is due! We can help them count results by writing a program. Assume that each district copiles votes into a CSV file, where each row contains one citizen's ballot data, and each column represents a different office up for election. The first row is header data indicating what office each column represents.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please answer in python

Minnesota is holding elections on the day that this
assignment is due! We can help them count results by
writing a program. Assume that each district copiles
votes into a CSV file, where each row contains one
citizen's ballot data, and each column represents a
different office up for election. The first row is header
data indicating what office each column represents.
Mayor
Jobu Tupaki
Evelyn Wang
Trevor Belmont
Leslie Knope
April Ludgate
●
●
County Sheriff
Shallan Davar
Liz Lemon
Liz Lemon
Liz Lemon
Ron Swanson
For example, the CSV file above would represent a
district with three offices up for election, and five citizens
who voted.
Governor
Buffy Summers
Leslie Knope
Gordon Freeman
and so on...
Monkey D. Luffy
Buffy Summers
The first voted for Jobu Tupaki for Mayor, Shallan
Davar for Sheriff, and Buffy Summers for
Governor.
The second voted for Evelyn Wang for Mayor, Liz
Lemon for Sheriff, and Leslie Knope for
Governor.
We're going to be creating a dictionary representing the
vote counts for a specific office. For example, in the
spreadsheet above, if I wanted a dictionary of vote counts
for the office of County Sheriff, the result would be:
{'Shallan Davar': 1, 'Liz Lemon': 3, 'Ron
Swanson': 1}
Whereas the vote counts for Governor would be:
{'Buffy Summers': 2, 'Leslie Knope': 1,
'Gordon Freeman': 1,
'Monkey D. Luffy': 1}
Write a function that count_votes (district,
office) that takes in two strings as parameters.
●
district should be the name of a file containing
all voting data for a given district, in the format
specified above. You can assume that said file
actually exists (no need for a try-except block).
• office should be the name of one of the column
titles present in the CSV file. You can assume
that the office passed in will match one of the
columns in the CSV file.
The function should return a dictionary in which each
key is a name present in the column corresponding to the
given office, and the value represents how many times
that name occurs within the column.
Transcribed Image Text:Minnesota is holding elections on the day that this assignment is due! We can help them count results by writing a program. Assume that each district copiles votes into a CSV file, where each row contains one citizen's ballot data, and each column represents a different office up for election. The first row is header data indicating what office each column represents. Mayor Jobu Tupaki Evelyn Wang Trevor Belmont Leslie Knope April Ludgate ● ● County Sheriff Shallan Davar Liz Lemon Liz Lemon Liz Lemon Ron Swanson For example, the CSV file above would represent a district with three offices up for election, and five citizens who voted. Governor Buffy Summers Leslie Knope Gordon Freeman and so on... Monkey D. Luffy Buffy Summers The first voted for Jobu Tupaki for Mayor, Shallan Davar for Sheriff, and Buffy Summers for Governor. The second voted for Evelyn Wang for Mayor, Liz Lemon for Sheriff, and Leslie Knope for Governor. We're going to be creating a dictionary representing the vote counts for a specific office. For example, in the spreadsheet above, if I wanted a dictionary of vote counts for the office of County Sheriff, the result would be: {'Shallan Davar': 1, 'Liz Lemon': 3, 'Ron Swanson': 1} Whereas the vote counts for Governor would be: {'Buffy Summers': 2, 'Leslie Knope': 1, 'Gordon Freeman': 1, 'Monkey D. Luffy': 1} Write a function that count_votes (district, office) that takes in two strings as parameters. ● district should be the name of a file containing all voting data for a given district, in the format specified above. You can assume that said file actually exists (no need for a try-except block). • office should be the name of one of the column titles present in the CSV file. You can assume that the office passed in will match one of the columns in the CSV file. The function should return a dictionary in which each key is a name present in the column corresponding to the given office, and the value represents how many times that name occurs within the column.
Note that it is possible to have someone write in the same
name for multiple different offices, so you do need to be
careful to only count occurrences of the name present in
the column representing the office you're counting votes
for.
You are permitted to import the csv module, but this is
not required.
Hints:
Be careful when the request is for the last column
in the file - if you're splitting by comma, then that
column will contain the '\n' character, and this
shouldn't be included in the candidate names, or
in the name of the office.
You can assume for the sake of simplicity that no
candidate or office will contain a comma in the
name.
Disclaimer: The data in the CSV files was randomly
generated from all names available on the ballot for a
given district according to
https://myballotmn.sos.state.mn.us/ (along with a few
fictional characters to simulate write-ins). Don't take any
given candidate randomly getting more "votes" than
another as an endorsement.
Examples (assumes that you have the sample files from
hw09files.zip downloaded to the same directory that
you're running Python in):
>>> count_votes('district_0z.csv', 'County
Sheriff')
{'Shallan Davar': 1, 'Liz Lemon': 3, 'Ron
Swanson': 1}
>>> count_votes('district_4b.csv',
'Mayor')
{'Shelly Carlson': 7, 'Donna Meagle': 2,
'Kevin Nese Shores': 5}
>>> count_votes('district_60b.csv',
'County Commissioner District 4')
{'Angela Conley': 50, 'Monkey D. Luffy':
1, 'Jobu Tupaki': 2, 'Leslie Knope': 1}
Transcribed Image Text:Note that it is possible to have someone write in the same name for multiple different offices, so you do need to be careful to only count occurrences of the name present in the column representing the office you're counting votes for. You are permitted to import the csv module, but this is not required. Hints: Be careful when the request is for the last column in the file - if you're splitting by comma, then that column will contain the '\n' character, and this shouldn't be included in the candidate names, or in the name of the office. You can assume for the sake of simplicity that no candidate or office will contain a comma in the name. Disclaimer: The data in the CSV files was randomly generated from all names available on the ballot for a given district according to https://myballotmn.sos.state.mn.us/ (along with a few fictional characters to simulate write-ins). Don't take any given candidate randomly getting more "votes" than another as an endorsement. Examples (assumes that you have the sample files from hw09files.zip downloaded to the same directory that you're running Python in): >>> count_votes('district_0z.csv', 'County Sheriff') {'Shallan Davar': 1, 'Liz Lemon': 3, 'Ron Swanson': 1} >>> count_votes('district_4b.csv', 'Mayor') {'Shelly Carlson': 7, 'Donna Meagle': 2, 'Kevin Nese Shores': 5} >>> count_votes('district_60b.csv', 'County Commissioner District 4') {'Angela Conley': 50, 'Monkey D. Luffy': 1, 'Jobu Tupaki': 2, 'Leslie Knope': 1}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Files and Directory
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education