Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Andrew Friedman
/
Fairly Draw Election Districts
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
562daf7d
authored
Mar 14, 2017
by
Garrett Johnson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merged
parent
9e402b37
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
22 deletions
algorithms/algorithms_new.py
algorithms/algorithms_new.py
View file @
562daf7d
...
...
@@ -38,28 +38,28 @@ def calculate_efficiency_gap(state):
Returns: the efficiency gap of district plan (a interger)
'''
total_votes
=
0
wasted_dvotes
=
0
wasted_rvotes
=
0
districts
=
{}
for
x
in
range
(
len
(
state
)):
for
y
in
range
(
len
(
state
)):
if
state
[
x
][
y
][
"district"
]
not
in
districts
:
districts
[
state
[
x
][
y
][
"district"
]]
=
{
"Rvotes"
:
0
,
"Dvotes"
:
0
}
districts
[
state
[
x
][
y
][
"district"
]][
"Rvotes"
]
+=
state
[
x
][
y
][
"R"
]
districts
[
state
[
x
][
y
][
"district"
]][
"Dvotes"
]
+=
state
[
x
][
y
][
"D"
]
total_votes
+=
state
[
x
][
y
][
"R"
]
+
state
[
x
][
y
][
"D"
]
for
d
in
districts
.
keys
():
assert
districts
[
d
][
"Dvotes"
]
!=
districts
[
d
][
"Rvotes"
]
vict_threshold
=
(
districts
[
d
][
"Dvotes"
]
+
districts
[
d
][
"Rvotes"
])
//
2
if
districts
[
d
][
"Dvotes"
]
>
districts
[
d
][
"Rvotes"
]:
wasted_dvotes
+=
(
districts
[
d
][
"Dvotes"
]
-
vict_threshold
)
wasted_rvotes
+=
districts
[
d
][
"Rvotes"
]
elif
districts
[
d
][
"Dvotes"
]
<
districts
[
d
][
"Rvotes"
]:
wasted_rvotes
+=
(
districts
[
d
][
"Rvotes"
]
-
vict_threshold
)
wasted_dvotes
+=
districts
[
d
][
"Dvotes"
]
efficiency_gap
=
(
wasted_dvotes
-
wasted_rvotes
)
/
total_votes
return
efficiency_gap
total_votes
=
0
wasted_dvotes
=
0
wasted_rvotes
=
0
districts
=
{}
for
x
in
range
(
len
(
state
)):
for
y
in
range
(
len
(
state
)):
if
state
[
x
][
y
][
"district"
]
not
in
districts
:
districts
[
state
[
x
][
y
][
"district"
]]
=
{
"Rvotes"
:
0
,
"Dvotes"
:
0
}
districts
[
state
[
x
][
y
][
"district"
]][
"Rvotes"
]
+=
state
[
x
][
y
][
"R"
]
districts
[
state
[
x
][
y
][
"district"
]][
"Dvotes"
]
+=
state
[
x
][
y
][
"D"
]
total_votes
+=
state
[
x
][
y
][
"R"
]
+
state
[
x
][
y
][
"D"
]
for
d
in
districts
.
keys
():
assert
districts
[
d
][
"Dvotes"
]
!=
districts
[
d
][
"Rvotes"
]
vict_threshold
=
(
districts
[
d
][
"Dvotes"
]
+
districts
[
d
][
"Rvotes"
])
//
2
if
districts
[
d
][
"Dvotes"
]
>
districts
[
d
][
"Rvotes"
]:
wasted_dvotes
+=
(
districts
[
d
][
"Dvotes"
]
-
vict_threshold
)
wasted_rvotes
+=
districts
[
d
][
"Rvotes"
]
elif
districts
[
d
][
"Dvotes"
]
<
districts
[
d
][
"Rvotes"
]:
wasted_rvotes
+=
(
districts
[
d
][
"Rvotes"
]
-
vict_threshold
)
wasted_dvotes
+=
districts
[
d
][
"Dvotes"
]
efficiency_gap
=
(
wasted_dvotes
-
wasted_rvotes
)
/
total_votes
return
efficiency_gap
def
calculate_efficiency_gap_from_list
(
total_votes
,
rep_votes
,
dem_votes
,
tracts
,
tract
):
'''
...
...
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