Skip to content

Commit

Permalink
Merge pull request EGCETSII#30 from joszamama/G2-feature/Imperiali
Browse files Browse the repository at this point in the history
G2 feature/imperiali
  • Loading branch information
beallasai authored Dec 20, 2021
2 parents 06f0dbb + 180bdd6 commit 8095199
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 7 deletions.
31 changes: 31 additions & 0 deletions decide/postproc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,34 @@ def testDHont6(self): #Escaños elevados

values = response.json()
self.assertEqual(values, expected_result)

def test_imperiali(self):
data={
'type':'IMPERIALI',
'options':[
{'option':'A', 'number':1, 'votes':391000},
{'option':'B', 'number':2, 'votes':311000},
{'option':'C', 'number':2, 'votes':184000},
{'option':'D', 'number':4, 'votes':73000},
{'option':'E', 'number':5, 'votes':27000},
{'option':'F', 'number':6, 'votes':12000},
{'option':'G', 'number':7, 'votes':2000},
], 'escanyosTotales':21
}

expected_result=[
{'option':'A', 'number':1, 'votes':391000, 'postproc':0},
{'option':'B', 'number':2, 'votes':311000, 'postproc':0},
{'option':'C', 'number':2, 'votes':184000, 'postproc':0},
{'option':'D', 'number':4, 'votes':73000, 'postproc':0},
{'option':'E', 'number':5, 'votes':27000, 'postproc':0},
{'option':'F', 'number':6, 'votes':12000, 'postproc':0},
{'option':'G', 'number':7, 'votes':2000, 'postproc':0},
]

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

values = response.json()
self.assertEqual(values, expected_result)

56 changes: 52 additions & 4 deletions decide/postproc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework.views import APIView
from rest_framework.response import Response
import math
import numpy as np
#import numpy as np


class PostProcView(APIView):
Expand Down Expand Up @@ -122,6 +122,52 @@ def HuntingtonHill(self,options,numEscanyos):
return Response(options)


def imperiali(self, options,numEscanyos):
votosTotales = 0

for i in options:
votosTotales= votosTotales+ i['votes']

if votosTotales>0 and numEscanyos>0:

q=round(votosTotales/(numEscanyos+2),0)

escanyosAsigandos=0
for i in options:
votos= i['votes']
escanyos=math.floor(votos/q)
i.update({'postproc': escanyos})
escanyosAsigandos=escanyosAsigandos+i['postproc']

#Mientras queden escaños libre

while(escanyosAsigandos<numEscanyos):
#Se almacenan los votos residuo
for i in options:
i.update({'votosResiduos': i['votes']- (q*i['postproc'])})


#se ordena según los votos residuos
ordenadoMayorMenor= options.sort(key = lambda i :-i['votosResiduos'])


#se añade un escaño más al que tenga mayor residuo
votoMayorResiduo= ordenadoMayorMenor[0]
votoMayorResiduo.update({'postproc': votoMayorResiduo['postproc']+1})

#se elimina la nueva clave para que no afecte a futuras iteraciones
for i in options:
i.pop('votosResiduos')

options.sort(key = lambda i :-i['postproc'])

return Response(options)

else:
for i in options:
i.update({'postproc': 0})
return Response(options)

def dHont(self, options, numEscanyos):

#Añadimos un campo para el contador de escaños asignados a cada opción
Expand Down Expand Up @@ -176,7 +222,7 @@ def sortByVotes(e):

def post(self, request):
"""
* type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON | BIPARTITANSHIP
* type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON | BIPARTITANSHIP| IMPERIALI
* options: [
{
option: str,
Expand All @@ -194,7 +240,6 @@ def post(self, request):
if t == 'IDENTITY':
return self.identity(opts)


elif t=='HUNTINGTONHILL':
return self.HuntingtonHill(options=opts, numEscanyos=numEscanyos)

Expand All @@ -206,5 +251,8 @@ def post(self, request):

elif t == 'BIPARTISHANSHIP':
return self.bipartishanship(options=opts, numEscanyos=numEscanyos)

elif t=='IMPERIALI':
return self.imperiali(opts,numEscanos)

return Response({})
return Response({})
3 changes: 2 additions & 1 deletion decide/voting/migrations/0004_voting_tipo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='voting',
name='tipo',
field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL'), ('HAMILTON','HAMILTON'), ('BIPARTITANSHIP', 'BIPARTITANSHIP')], default='IDENTITY', max_length=20, verbose_name='Count method'),

field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL'), ('HAMILTON','HAMILTON'), ('BIPARTITANSHIP', 'BIPARTITANSHIP'),("IMPERIALI", "IMPERIALI")], default='IDENTITY', max_length=20, verbose_name='Count method'),
),
]
3 changes: 1 addition & 2 deletions decide/voting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class Voting(models.Model):
start_date = models.DateTimeField(blank=True, null=True)
end_date = models.DateTimeField(blank=True, null=True)


tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT"), ('HAMILTON', 'HAMILTON'),("BIPARTISHANSHIP", "BIPARTISHANSHIP")]
tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT"), ('HAMILTON', 'HAMILTON'),("BIPARTISHANSHIP", "BIPARTISHANSHIP"),("IMPERIALI", "IMPERIALI")]
tipo = models.CharField(choices=tipo_votacion, max_length=20, default="IDENTITY", verbose_name='Count method')


Expand Down

0 comments on commit 8095199

Please sign in to comment.