Transcribed Text
Program Description:
Recently gerrymandering has been popular topic in the news and the subject of an ongoing Supreme Court
case. Gerrymandering is creating voting districts in such a way that gives one party an unfair advantage over the
other Political parties do this to try to help themsel ves stay in power. Recently. research group came up with
math-ematical definition for what constitutes gerrymandering For this project, you will prompt the user for
a state name, and then determine whether the state's districts are gerrymandered, according the researchers
definition. and output graphical representation of the districts on DrawingPanel. The input data about states
districts and total voters comes from two input files provided on the course web site Even if you disagree with
this definition of gerrymandering, it is interesting to understand it better as the courts are currently debating it.
Your program should give an introduction and then prompt the user for a state name. It should then read
the district file searching for that state, case-insensitively (that is, you should find the state regardless of the
capitali -zation the user uses when typing in). If the state is found in the file. your program should print the
total wasted votes of democrats, the total wasted votes of republicans, the total voters in the state, who has
gained an advantage from gerrymandering the state, if is gerrymandered, and display information about the
state graphically
This program allows you to search through
data about congresssional voting districts
and determine whether a particular state is
errymandered.
which state do you want to look up? Arizona
Total Wasted Democratic votes: 327852
Total Wasted Republican votes: 369697
4738332 elgible voters
Ariguna
lof 3
Input Data and Files:
Your program reads data from twofiles
1.
El
stricts.txt:
congressional district date
Each line of districts. txt contains state name followed by district information in groups of three. The first
of the three is the district name the second the democratic votes in that district and the third the republican votes
in that district Depending on the size of the state. there will be different number of districts. For example:
Arkansas, 1. 63555, 1241 39, 2, 1034 77 123073, 3, 0, 151 630, 4, 87742, 110789
Alaska, AL, 4596, 42566
Rhode Island, 1, 87060, 58877 2, 105716, 63844
Notice that the data is separated with commas (",") instead of the spaces we have used so far. We are using
commas because some state names contain spaces and we want to preserve those spaces.
You can split string called ine on commas with the following call to split: line spl it (" ")
Note that the district names will sometimes be numbers and sometimes strings The vote counts will always be
integers.
Once the user types state name. search each line of districts txt tosee if it contains data for that state. If
the state name is found. output its data line to the console, then construct a DrawingPanel to graph the
data
(see
next page). Your code should not assume that the file is sorted alphabetically
If the state name is not found, output "not found" message and don't show any data No DrawingPanel should
appear
This program al lows you to search through
data about congresssional voting districts
and determine whether a particular state is
gerrymandered
Which state do you want to look up? mErLin
"mErLin" not found.
The data displayed above has different number of districts for each state. Your program should work properly
with any number of districts of data greater than 0. Since there is limit to the size of the rawingPanel, you
may not be able to see all decades worth of data at the default height, but your code should process as many
districts of data finds in the line.
2.
= eligible voters txt:
the number of eligible voters in each state
Ifthe state name found in dist ricts. txt, you should also read el igible voters. txt to find its total number
of eligible voters. The number of eligible voters should be printed to the console and also drawn on the Draw-
ingPanel. Every state name in districts. txt is alsoin eligible voters. txt, so you do not need to worry
about state having district data but no eligible voter data.
Each line of ligible _voters. txt contains astate name, followed by the eligible voter count for that state. For
example:
Alabama, 3606103
Alaska,519501
Arizona, 4738332
Arkansas, 148441
California,25278803
Though the two input files contain different data, the task of searching for: state name in districts txt is very
similar to the task of searching for state name in eligible voters. txt. Your code should take advantage of
this fact and should avoid redundancy You should write your code in such way that you stop searching file
once you find line that has the name you' 're searching for
You may not assume the input files will have 50 lines. Your program must work on any length input file.
2 of 3
Graphical Output:
The panel's overall size is 500x500 pixels. Its background is white. It has black line drawn horizontally 20 pixels from
the top and black line drawn vertically in the middle of the panel from top to bottom. The state name is drawn at (0 0).
The eligible voters datais drawn: of 0 and 120 pixels from the right hand edge of the panel.
Each district is represented by horizontal bar that stretches across the entire width of the panel These bars are 20
pixels
tall each. The first one starts 25 pixels from the top of the panel There are empty pixels between each bar.
The blue part of the bar represents the democratic votes in the district The red represents the republican votes in a district
The total votes for each party in each district are in the districts.txt file. The width of the democratic portion
should be:
democratic-vot / (democratic-votes + republican-votes) * width- of the-panel
Determining Gerrymandering
You can determine gerrymandering by counting up and comparing the wasted votes cast for each party We will define.
wasted vote as any vote not needed to win the election. That means all votes for the party that loses the district seat are
wasted as well as all votes for the winning party other than the half they need to win the majority
For example, imagine that there was state with the following districts:
Dem
GOP
Wasted Dem
Wasted GOP
District
2
7
2
2
District
4
5
4
District 10
7
1
7
Total:
16
19
7
9
Having calculated this data. we can sum up the wasted votes for each district We find that the democrats wasted votes
and
the
republicans
wasted
impossible
to
make
voting
districts
exactly
fair
and
we
shouldn't
expect
the
wasted
vote counts to be equal. However. researchers have discovered that almost impossible for the disadvantaged party to
recover the difference in wasted votes greater or equal to percent. Therefore. the researchers well as us for the
purposes of this assignment, will consider state gerrymandered when there is 7% or greater difference in the wasted
votes.
Implementation Guidelines:
Wesuggest you begin with the text output and file processing, then any "fixed" graphical output, and then the bars
Your program should work correctly regardless of the capitalization the user uses to type the state name. If the user types
'AlAbAmA" or alabama", you should find i even though the input files have as "Alahama"
Stylistic Guidelines:
You should have least these two constants If the constant values are changed, your output should adapt.
.
The width of the DrawingFanel as an integer (default of 50D)
e.g. If you change the width to 600, each bar should still be the width of the screen
.
The height of the DrawingPanel as an integer (default of 500)
We will be especially picky about redundancy. For full credit, your functions should obey these constraints
The mair function should not draw on DrawingPanel nor read lines of input from: file.
You may not calculate whether state is gerrymandered in the same function that draws on the DrawingPanel
or
reads from the file.
Your functions should be well- structured and avoid redundancy, and your main function should be concise summary of
the overall program. Avoid 'chaining, which when many functions call each other without ever returning to main.
For this assignment you are limited to the language features in lectures 1 23 Follow past sty listic guidelines about
inden-
tation line lengths. identifier names. and localizing variables, and commenting the beginning of your program, at
the
start
of each function, and on complex sections of code. You may not have global variables or nest functions in one another.
You may not use the list count or sum functions.
3 of 3
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
from DrawingPanel import *
def print_introduction():
print("This program allows you to search through")
print("data about congresssional voting districts")
print("and determine whether a particular state is")
print("gerrymandered.")
print("")
def get_district_data(state_name):
district_file = open("districts.txt")
while True:
district = district_file.readline()
if not district:
result = None
break
district = district.strip()
data = district.split(",")
state = data.pop(0).lower()
if state == state_name:
result = list()
while len(data) >= 3:
district_name = data.pop(0).lower()
democratic = int(data.pop(0))
republican = int(data.pop(0))
result.append((district_name, democratic, republican))
return result
return result...