Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lockwooda committed Apr 19, 2019
1 parent 3ca05ca commit 08be1cd
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 0 deletions.
5 changes: 5 additions & 0 deletions data/east_england.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Conservative,1690813,50
Labour,1012359,7
Liberal Democrat,224064,1
UKIP,77793,0
Green,58684,0
5 changes: 5 additions & 0 deletions data/east_midlands.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Conservative,1195983,31
Labour,952295,15
Liberal Democrat,101612,0
UKIP,56358,0
Green,34355,0
5 changes: 5 additions & 0 deletions data/london.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Labour,2087010,49
Conservative,1268800,21
Liberal Democrat,336680,3
Green,67579,0
UKIP,49369,0
5 changes: 5 additions & 0 deletions data/north_east_england.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Labour,709838,26
Conservative,440613,3
Liberal Democrat,58409,0
UKIP,49348,0
Green,16080,0
5 changes: 5 additions & 0 deletions data/north_west_england.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Labour,1972682,54
Conservative,1301562,20
Liberal Democrat,193197,1
UKIP,68946,0
Green,39169,0
7 changes: 7 additions & 0 deletions data/northern_ireland.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DUP,292316,10
Sinn Fein,238915,7
SDLP,95419,0
UUP,83280,0
Alliance,64553,0
Green,7452,0
TUV,3282,0
6 changes: 6 additions & 0 deletions data/scotland.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Scottish National Party,977569,35
Conservative,757949,13
Labour,717007,7
Liberal Democrat,179061,4
Green,5886,0
UKIP,5302,0
6 changes: 6 additions & 0 deletions data/south_east_england.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Conservative,2529574,72
Labour,1326279,8
Liberal Democrat,487243,2
Green,145171,1
UKIP,103509,0
Speaker,11283,1
5 changes: 5 additions & 0 deletions data/south_west_england.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Conservative,1539618,47
Labour,870561,7
Liberal Democrat,446181,1
Green,66176,0
UKIP,32944,0
15 changes: 15 additions & 0 deletions data/uk.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Conservative,12344901,317
Labour,11390099,262
Liberal Democrat,2121801,12
Scottish National Party,977568,35
UKIP,594068,0
Green,512327,1
DUP,292316,10
Sinn Fein,238915,7
Plaid Cymru,164466,4
Independent,151471,1
SDLP,95419,0
UUP,83280,0
Alliance,64533,0
Speaker,34299,0
Yorkshire Party,20958,0
6 changes: 6 additions & 0 deletions data/wales.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Labour,771354,28
Conservative,528839,8
Plaid Cymru,164466,4
Liberal Democrat,71039,0
UKIP,31376,0
Green,5128,0
5 changes: 5 additions & 0 deletions data/west_midlands.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Conservative,1355468,35
Labour,1175075,24
Liberal Democrat,122287,0
UKIP,50106,0
Green,45208,0
5 changes: 5 additions & 0 deletions data/yorkshire_humber.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Labour,1275800,37
Conservative,1054099,17
Liberal Democrat,129637,0
UKIP,67801,0
Green,32743,0
67 changes: 67 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from party import Party
from region import Region

def main():
east_england = Region("East of England", "data/east_england.csv", 35, 23)
east_midlands = Region("East Midlands", "data/east_midlands.csv", 28, 18)
london = Region("London", "data/london.csv", 41, 32)
north_east_england = Region("North East England", "data/north_east_england.csv", 19, 10)
north_west_england = Region("North West England", "data/north_west_england.csv", 47, 28)
northern_ireland = Region("Northern Ireland", "data/northern_ireland.csv", 11, 7)
scotland = Region("Scotland", "data/scotland.csv", 37, 22)
south_east_england = Region("South East England", "data/south_east_england.csv", 50, 34)
south_west_england = Region("South West England", "data/south_west_england.csv", 34, 21)
wales = Region("Wales", "data/wales.csv", 28, 12)
west_midlands = Region("West Midlands", "data/west_midlands.csv", 37, 22)
yorkshire_humber = Region("Yorkshire and the Humber", "data/yorkshire_humber.csv", 33, 21)

regions = []
regions.append(east_england)
regions.append(east_midlands)
regions.append(london)
regions.append(north_east_england)
regions.append(north_west_england)
regions.append(northern_ireland)
regions.append(scotland)
regions.append(south_east_england)
regions.append(south_west_england)
regions.append(wales)
regions.append(west_midlands)
regions.append(yorkshire_humber)

computedRegions = []
for x in regions:
computed = x.convertToAMS()
computedRegions.append(computed)
print(x.getName())
for y in computed:
print(y[0] + "\t" + "Constituency MPs: " + str(y[1]) + " List MPs: " + str(y[2]))
print()
computeUKWide(computedRegions)

def computeUKWide(computedValues):
isEmptyList = False
print("United Kingdom")
while(not isEmptyList):
conMP = 0
listMP = 0
party = computedValues[0][0][0]
for x in computedValues:
values = list(filter(lambda y: y[0] == party, x))
if(len(values) > 0):
conMP += values[0][1]
listMP += values[0][2]
print(party + "\t" + "Constituency MPs: " + str(conMP) + " List MPs: " + str(listMP))

newComputedValues = []
for x in computedValues:
values = list(filter(lambda y: y[0] != party, x))
if(len(values) > 0):
newComputedValues.append(values)
if len(newComputedValues) > 0:
computedValues = newComputedValues
else:
isEmptyList = True

if __name__ == "__main__":
main()
29 changes: 29 additions & 0 deletions party.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Party:
conMP = 0
listMP = 0

def __init__(self, name, vote, conOld):
self.name = name
self.vote = vote
self.conOld = conOld

def getName(self):
return self.name

def getVote(self):
return self.vote

def getConOld(self):
return self.conOld

def getConstituencyMP(self):
return self.conMP

def getListMP(self):
return self.listMP

def setConstituencyMP(self, conMP):
self.conMP = conMP

def addListMP(self):
self.listMP += 1
71 changes: 71 additions & 0 deletions region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from party import Party
import csv
import math
import copy

class Region:

def __init__(self, name, file, conMPs, listMPs):
self.name = name
self.file = file
self.conMPs = conMPs
self.listMPs = listMPs
self.parties = []
self.readFile()

def getName(self):
return self.name

def addParty(self, party):
self.parties.append(party)

def readFile(self):
with open(self.file) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
lineCount = 0
for row in reader:
self.addParty(Party(row[0], int(row[1]), int(row[2])))

def convertToAMS(self):
allVotes = 0
leftOverMPs = self.conMPs
newConMPs = []

for x in range(0, len(self.parties)):
allVotes += self.parties[x].getVote()
newConMPs.append(math.floor(self.parties[x].getConOld() / float(self.conMPs + self.listMPs) * self.conMPs))
leftOverMPs -= newConMPs[x]

votes = list(map(lambda x: x.getVote(), self.parties))
if(leftOverMPs > 0):
newConMPs = self.dHondtConMethod(leftOverMPs, newConMPs, votes)

newListMPs = self.dHondtListMethod(self.listMPs, copy.copy(newConMPs), votes)
newMPs = [sum(x) for x in zip(newConMPs, newListMPs)]
return list(zip(map(lambda x: x.getName(), self.parties), newConMPs, newListMPs))

def dHondtConMethod(self, seatsLeft, seats, votes):
while (seatsLeft > 0):
maxValue = -1
seatToAdd = 0
for x in range(0, len (seats)):
if(seats[x] > 0 and (votes[x] / (seats[x] + 1)) > (votes[seatToAdd] / (seats[seatToAdd] + 1))):
seatToAdd = x
seats[seatToAdd] += 1
seatsLeft -= 1
return seats

def dHondtListMethod(self, seatsLeft, seats, votes):
newListMPs = [0] * len(votes)
# Whilst there are seats to assign, check values
while (seatsLeft > 0):
maxValue = -1
seatToAdd = 0
# For each set for values, compare the D'Hondt value to the current maximum
for x in range(0, len (seats)):
if(votes[x] / (seats[x] + 1)) > (votes[seatToAdd] / (seats[seatToAdd] + 1)):
seatToAdd = x
newListMPs[seatToAdd] += 1
seats[seatToAdd] += 1
seatsLeft -= 1
return newListMPs

0 comments on commit 08be1cd

Please sign in to comment.