Skip to content

Commit

Permalink
Merge pull request EGCETSII#27 from joszamama/G2-feature/hamilton
Browse files Browse the repository at this point in the history
G2 feature/hamilton
  • Loading branch information
beallasai authored Dec 20, 2021
2 parents ff69907 + a821248 commit 2a059c1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
24 changes: 24 additions & 0 deletions decide/postproc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@ def test_identity(self):
{ 'option': 'Option 6', 'number': 6, 'votes': 1, 'postproc': 1 },
{ 'option': 'Option 2', 'number': 2, 'votes': 0, 'postproc': 0 },
]
def test_hamilton(self):
data = {
'type': 'HAMILTON',
'options': [
{'option':'A','number':1,'votes': 100000},
{'option':'B', 'number':2,'votes': 80000},
{'option':'C', 'number':3,'votes': 30000},
{'option':'D', 'number':4,'votes': 20000}
], 'numEscanyos': 10

}

expected_result = [
{'option':'A','number':1,'votes': 100000, 'postproc': 4},
{'option':'B', 'number':2,'votes': 80000, 'postproc': 4},
{'option':'C', 'number':3,'votes': 30000, 'postproc': 1},
{'option':'D', 'number':4,'votes': 20000, 'postproc': 1}
]

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

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

response = self.client.post('/postproc/', data, format='json')
self.assertEqual(response.status_code, 200)
Expand Down
42 changes: 40 additions & 2 deletions decide/postproc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ def identity(self, options):

out.sort(key=lambda x: -x['postproc'])
return Response(out)


def hamilton(self, options, numEscanyos):
#Definimos votos totales y el numero de escanyos asignados
votos = 0
numEscanyosAsignados=0
#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
for option in options:
option['postproc']=0
votos += option['votes']
#Creamos una lista vacia para introducir el resto de cada partido al anyadir los escanyos
lista=[]
if votos>0 and numEscanyos>0:
participantes = len(options)
#Recorremos las opciones y al atributo postproc que habiamos creado anteriormente le asignamos un numero de escanyos mediante
#el siguiente calculo: (NumVotosPartido*NumEscanyos)//VotosTotales. El resultado sera una division exacta
#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
#Tambien vamos incrementando el numero de escanyos asignados
for option in options:
option['postproc']=(option['votes']*numEscanyos)//votos
lista.append({'number':option['number'],'votes':(option['votes']*numEscanyos)%votos})
numEscanyosAsignados+=(option['votes']*numEscanyos)//votos


lista.sort(key=lambda o: o['votes'],reverse=True)
for option in lista:
i = option['number']-1
if(numEscanyosAsignados<numEscanyos):
options[i].update({'postproc' : options[i]['postproc']+1})
numEscanyosAsignados+=1
return Response(options)


def HuntingtonHill(self,options,numEscanyos):

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

while(numEscanyosAsig != numEscanyos):

#si llegamos a aplicar rounding rule y no llegamos al numero igual de escanos,
#reseteamos de nuevo el numero de escanos asig y empezamos de nuevo
##si no se cumple la regla reseteamos cero

numEscanyosAsig = 0

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

if(numEscanyosAsig < numEscanyos):

limit = lower
lower = limit-rounding
upper = limit+rounding
Expand Down Expand Up @@ -110,7 +146,7 @@ def dHont(self, options, numEscanyos):

def post(self, request):
"""
* type: IDENTITY | HUNTINGTONHILL | DHONT
* type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON
* options: [
{
option: str,
Expand All @@ -125,7 +161,6 @@ def post(self, request):
opts = request.data.get('options', [])
numEscanyos = request.data.get('numEscanyos', 0)


if t == 'IDENTITY':
return self.identity(opts)

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

elif t == 'DHONT':
return self.dHont(options=opts, numEscanyos=numEscanyos)

elif t== 'HAMILTON':
return self.hamilton(options=opts, numEscanyos=numEscanyos)

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

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


Expand Down

0 comments on commit 2a059c1

Please sign in to comment.