Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON PLEASE Write a function named gerrymanderingRatios that examines an input file of voting data to calculate whether voting districts have been created in a

PYTHON PLEASEimage text in transcribed

Write a function named gerrymanderingRatios that examines an input file of voting data to calculate whether voting districts have been created in a way that gives an unfair advantage to a particular political party Districting is the process by which a US state is divided into voting regions called districts. During an election, the candidate who earns the largest number of votes in a district (the "plurality" of votes) is elected as that district's representative. Gerrymandering is a malicious process of trying to draw district boundaries in such a way as to give one party more representatives than their fair share. For example, if you spread the voters from a given party too thinly across many districts, they will not have enough votes to win a majority in any district. Similarly, if you too tightly pack the voters from a political party all into a small number of districts, all votes over the needed plurality (e.g. 50% in a two-party system) are wasted In the figure below, the square area of Democrat (D) and Republican (R) voters are divided into 5-person districts. The districting at left leads to 4/5 of the districts won by Democrats, while the districting at right is the opposite, awarding 4/5 of the districts to Republicans These both seem unfair given the nearly equal total count of voters from the two parties DR D D R RDR R R DIR RID D DID DIRD DIR R DIR D RID DR R DR RR DIRIRIDID D D DIRID DIR R DR gerrymandered toward D (left) and toward R (right) For this problem let us define the "Gerrymandering ratio" as a measure of a given political party's performance in a given districting arrangement. It is computed by dividing the % of districts that party won by that party's % of the total vote. A ratio of 1.0 would be perfect representation. If the ratio is far above 1.0 for a given party, it implies that the districting was gerrymandered to favor that party Similarly, if the ratio is far below 1.0, that party might be disadvantaged Gerrymandering Ratio for a Party (Percent of districts where that Party won a majority of the vote) (Percent of total votes won by that Party.) Your function accepts as its parameter a string representing a filename of an input file representing voting data. The box above at right shows an example contents of such a file. Each line of the input file will contains information about one voting district. First will be a single word token representing that district's name, followed by each individual vote from that district, each separated by a single space The votes will be single uppercase characte indicating the political party of interest, such as D or R. District1DRDD D District2 R D R R DISTRICT3 R RR RR District4 D DR RDR D district5 D DRDR Your job is to calculate the gerrymandering ratio for every political party that is represented in the data set. You should return this information as a Map from each political party name (a string) to that party's gerrymandering ratio (a real number). For example, if the input file make up 12/26 (46.1%) of the total voters, but Democrats won 3/5 (60%) of the districts by majority vote. This means that the gerrymandering ratio for "D" party is 60 / 46.1, or roughly 1.3. You can perform a similar calculation for the other party in the data set, R, representing the Republicans, which would reveal a ratio of roughly 0.74. Based on this information, your function would return the following map m for the fle above on the righ pa 0 nct ode should determine that the D votes (Demo ("D":1.3, "R":8.742857) Write a function named gerrymanderingRatios that examines an input file of voting data to calculate whether voting districts have been created in a way that gives an unfair advantage to a particular political party Districting is the process by which a US state is divided into voting regions called districts. During an election, the candidate who earns the largest number of votes in a district (the "plurality" of votes) is elected as that district's representative. Gerrymandering is a malicious process of trying to draw district boundaries in such a way as to give one party more representatives than their fair share. For example, if you spread the voters from a given party too thinly across many districts, they will not have enough votes to win a majority in any district. Similarly, if you too tightly pack the voters from a political party all into a small number of districts, all votes over the needed plurality (e.g. 50% in a two-party system) are wasted In the figure below, the square area of Democrat (D) and Republican (R) voters are divided into 5-person districts. The districting at left leads to 4/5 of the districts won by Democrats, while the districting at right is the opposite, awarding 4/5 of the districts to Republicans These both seem unfair given the nearly equal total count of voters from the two parties DR D D R RDR R R DIR RID D DID DIRD DIR R DIR D RID DR R DR RR DIRIRIDID D D DIRID DIR R DR gerrymandered toward D (left) and toward R (right) For this problem let us define the "Gerrymandering ratio" as a measure of a given political party's performance in a given districting arrangement. It is computed by dividing the % of districts that party won by that party's % of the total vote. A ratio of 1.0 would be perfect representation. If the ratio is far above 1.0 for a given party, it implies that the districting was gerrymandered to favor that party Similarly, if the ratio is far below 1.0, that party might be disadvantaged Gerrymandering Ratio for a Party (Percent of districts where that Party won a majority of the vote) (Percent of total votes won by that Party.) Your function accepts as its parameter a string representing a filename of an input file representing voting data. The box above at right shows an example contents of such a file. Each line of the input file will contains information about one voting district. First will be a single word token representing that district's name, followed by each individual vote from that district, each separated by a single space The votes will be single uppercase characte indicating the political party of interest, such as D or R. District1DRDD D District2 R D R R DISTRICT3 R RR RR District4 D DR RDR D district5 D DRDR Your job is to calculate the gerrymandering ratio for every political party that is represented in the data set. You should return this information as a Map from each political party name (a string) to that party's gerrymandering ratio (a real number). For example, if the input file make up 12/26 (46.1%) of the total voters, but Democrats won 3/5 (60%) of the districts by majority vote. This means that the gerrymandering ratio for "D" party is 60 / 46.1, or roughly 1.3. You can perform a similar calculation for the other party in the data set, R, representing the Republicans, which would reveal a ratio of roughly 0.74. Based on this information, your function would return the following map m for the fle above on the righ pa 0 nct ode should determine that the D votes (Demo ("D":1.3, "R":8.742857)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Probabilistic Databases

Authors: Dan Suciu, Dan Olteanu, Christopher Re, Christoph Koch

1st Edition

3031007514, 978-3031007514

More Books

Students also viewed these Databases questions

Question

2 Learn about the Bank of Canada

Answered: 1 week ago