Skip to content

Commit 2a059c1

Browse files
authored
Merge pull request EGCETSII#27 from joszamama/G2-feature/hamilton
G2 feature/hamilton
2 parents ff69907 + a821248 commit 2a059c1

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

decide/postproc/tests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ def test_identity(self):
3636
{ 'option': 'Option 6', 'number': 6, 'votes': 1, 'postproc': 1 },
3737
{ 'option': 'Option 2', 'number': 2, 'votes': 0, 'postproc': 0 },
3838
]
39+
def test_hamilton(self):
40+
data = {
41+
'type': 'HAMILTON',
42+
'options': [
43+
{'option':'A','number':1,'votes': 100000},
44+
{'option':'B', 'number':2,'votes': 80000},
45+
{'option':'C', 'number':3,'votes': 30000},
46+
{'option':'D', 'number':4,'votes': 20000}
47+
], 'numEscanyos': 10
48+
49+
}
50+
51+
expected_result = [
52+
{'option':'A','number':1,'votes': 100000, 'postproc': 4},
53+
{'option':'B', 'number':2,'votes': 80000, 'postproc': 4},
54+
{'option':'C', 'number':3,'votes': 30000, 'postproc': 1},
55+
{'option':'D', 'number':4,'votes': 20000, 'postproc': 1}
56+
]
57+
58+
response = self.client.post('/postproc/', data, format='json')
59+
self.assertEqual(response.status_code, 200)
60+
61+
values = response.json()
62+
self.assertEqual(values, expected_result)
3963

4064
response = self.client.post('/postproc/', data, format='json')
4165
self.assertEqual(response.status_code, 200)

decide/postproc/views.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,38 @@ def identity(self, options):
1818

1919
out.sort(key=lambda x: -x['postproc'])
2020
return Response(out)
21+
22+
23+
def hamilton(self, options, numEscanyos):
24+
#Definimos votos totales y el numero de escanyos asignados
25+
votos = 0
26+
numEscanyosAsignados=0
27+
#Hacemos recuento de votos totales y le añadimos a cada opción otro valor llamado postproc en el que almacenaremos el numero de escanyos que le asignamos
28+
for option in options:
29+
option['postproc']=0
30+
votos += option['votes']
31+
#Creamos una lista vacia para introducir el resto de cada partido al anyadir los escanyos
32+
lista=[]
33+
if votos>0 and numEscanyos>0:
34+
participantes = len(options)
35+
#Recorremos las opciones y al atributo postproc que habiamos creado anteriormente le asignamos un numero de escanyos mediante
36+
#el siguiente calculo: (NumVotosPartido*NumEscanyos)//VotosTotales. El resultado sera una division exacta
37+
#A su vez rellenamos la lista vacia con un diccionario en el que ponemos el nombre de la opcion y el resto de la division
38+
#Tambien vamos incrementando el numero de escanyos asignados
39+
for option in options:
40+
option['postproc']=(option['votes']*numEscanyos)//votos
41+
lista.append({'number':option['number'],'votes':(option['votes']*numEscanyos)%votos})
42+
numEscanyosAsignados+=(option['votes']*numEscanyos)//votos
43+
44+
45+
lista.sort(key=lambda o: o['votes'],reverse=True)
46+
for option in lista:
47+
i = option['number']-1
48+
if(numEscanyosAsignados<numEscanyos):
49+
options[i].update({'postproc' : options[i]['postproc']+1})
50+
numEscanyosAsignados+=1
51+
return Response(options)
52+
2153

2254
def HuntingtonHill(self,options,numEscanyos):
2355

@@ -38,7 +70,10 @@ def HuntingtonHill(self,options,numEscanyos):
3870

3971
while(numEscanyosAsig != numEscanyos):
4072

73+
#si llegamos a aplicar rounding rule y no llegamos al numero igual de escanos,
74+
#reseteamos de nuevo el numero de escanos asig y empezamos de nuevo
4175
##si no se cumple la regla reseteamos cero
76+
4277
numEscanyosAsig = 0
4378

4479
for x in options:
@@ -70,6 +105,7 @@ def HuntingtonHill(self,options,numEscanyos):
70105
#round q up to U.
71106

72107
if(numEscanyosAsig < numEscanyos):
108+
73109
limit = lower
74110
lower = limit-rounding
75111
upper = limit+rounding
@@ -110,7 +146,7 @@ def dHont(self, options, numEscanyos):
110146

111147
def post(self, request):
112148
"""
113-
* type: IDENTITY | HUNTINGTONHILL | DHONT
149+
* type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON
114150
* options: [
115151
{
116152
option: str,
@@ -125,7 +161,6 @@ def post(self, request):
125161
opts = request.data.get('options', [])
126162
numEscanyos = request.data.get('numEscanyos', 0)
127163

128-
129164
if t == 'IDENTITY':
130165
return self.identity(opts)
131166

@@ -134,5 +169,8 @@ def post(self, request):
134169

135170
elif t == 'DHONT':
136171
return self.dHont(options=opts, numEscanyos=numEscanyos)
172+
173+
elif t== 'HAMILTON':
174+
return self.hamilton(options=opts, numEscanyos=numEscanyos)
137175

138176
return Response({})

decide/voting/migrations/0004_voting_tipo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class Migration(migrations.Migration):
1313
migrations.AddField(
1414
model_name='voting',
1515
name='tipo',
16-
field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL')], default='IDENTITY', max_length=20, verbose_name='Count method'),
16+
field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL'), ('HAMILTON','HAMILTON')], default='IDENTITY', max_length=20, verbose_name='Count method'),
1717
),
1818
]

decide/voting/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class Voting(models.Model):
3636
start_date = models.DateTimeField(blank=True, null=True)
3737
end_date = models.DateTimeField(blank=True, null=True)
3838

39-
tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT")]
39+
40+
tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT"), ('HAMILTON', 'HAMILTON')]
4041
tipo = models.CharField(choices=tipo_votacion, max_length=20, default="IDENTITY", verbose_name='Count method')
4142

4243

0 commit comments

Comments
 (0)