2
Hi, I’m having a problem with a Python function, which involves a tuple stack. When performing this assembly function calling the tuple tuple votes:
votacoes = (
(0, 15729, 220408, 1297, 0, 3040, 993, 0, 1354, 1046, 0, 3284, 99652, 19327, 0),
(0, 19000, 23173, 255, 0, 532, 201, 0, 306, 232, 0, 1980, 22307, 3890, 0),
(0, 23731, 244971, 1959, 0, 2710, 1465, 0, 1094, 1114, 0, 4264, 159476, 20488, 0),
(0, 1956, 47716, 282, 0, 0, 175, 0, 165, 247, 0, 417, 19728, 1732, 0),
(0, 5384, 52325, 403, 0, 770, 543, 0, 428, 0, 0, 1454, 38317, 4609, 0),
(0, 14138, 113419, 662, 0, 2535, 600, 0, 591, 557, 0, 2014, 66199, 13034, 0),
(0, 18967, 31260, 237, 0, 649, 216, 0, 168, 207, 0, 1810, 25010, 4225, 0),
(0, 17255, 99745, 2076, 0, 3285, 0, 0, 1069, 700, 0, 3160, 46082, 16347, 0),
(0, 3299, 53450, 251, 0, 520, 199, 0, 178, 191, 0, 755, 26263, 3114, 0),
(0, 12351, 148762, 977, 0, 3029, 633, 0, 595, 453, 0, 2502, 51518, 0, 0),
(0, 111661, 560365, 4135, 0, 16913, 2410, 0, 5897, 4270, 0, 14419, 322034, 66874, 0),
(0, 7910, 26257, 176, 0, 333, 162, 0, 151, 135, 0, 1031, 19963, 2753, 0),
(0, 61832, 488402, 2413, 0, 9072, 3386, 0, 1551, 1525, 0, 9640, 318113, 51002, 0),
(0, 21347, 118028, 1454, 0, 2220, 692, 0, 832, 726, 0, 3413, 61194, 13712, 0),
(0, 82159, 156444, 1682, 0, 6282, 1133, 0, 1595, 847, 0, 0, 112764, 29667, 0),
(0, 6648, 76961, 384, 0, 926, 0, 0, 213, 331, 0, 1473, 35327, 5928, 0),
(0, 3656, 71840, 304, 0, 617, 254, 0, 147, 574, 0, 675, 34825, 2784, 0),
(0, 5810, 123184, 696, 0, 1229, 465, 0, 266, 626, 0, 1456, 54107, 5786, 0),
(0, 2288, 53518, 314, 0, 756, 293, 0, 219, 271, 0, 669, 23189, 3965, 0),
(0, 5096, 87597, 2560, 0, 2385, 2992, 0, 617, 538, 0, 1967, 20360, 5568, 0),
(0, 803, 6306, 101, 0, 192, 83, 0, 48, 50, 0, 132, 7205, 602, 0),
(0, 127, 8938, 87, 0, 0, 0, 0, 64, 47, 0,52, 2714, 165, 0)
)
def assembleia(t):
lst_votos = list (t)
lst = []
for e in t:
lst = lst + [0]
n = (16, 3, 19, 3, 4, 9, 3, 9, 4, 10, 47, 2, 39, 9, 18, 6, 5, 9, 5, 6, 2, 2)
lst_mand = list (n)
lstm = []
for i in n:
lstm = lstm + [0]
for z in n:
while z > 0:
maior = lst_votos[0]
for i in range(0, len(lst_votos)):
if lst_votos[i] > maior:
maior = lst_votos[i]
for j in range(0, len(lst_votos)):
if maior == lst_votos[j]:
p = j
lst[p] = lst[p] + 1
lst_votos = lst_votos[:p] + [(lst_votos[p] * lst[p]) / (lst[p] +1)] + lst_votos[p+1:]
z = (z - 1)
return tuple(lst)
The mistake that gives is:
line 61, in <module>
lst_votos = lst_votos[:p] + [(lst_votos[p] * lst[p]) / (lst[p] +1)] + lst_votos[p+1:]
builtins.TypeError: unsupported operand type(s) for /: 'tuple' and 'int'
I can understand why that mistake happened, but I can’t seem to get around it and make it work.... I can make it work if you do it one-on-one, now calling Thiuplo, broke... The idea is that later a Tuple is returned with the results of the distribution of votes (then I will have to add this return, but I haven’t put it in the code).
hmm, it’s kind of hard to answer your question. The mistake you’ve already noticed:
list_votos[p]
is a tuple andlst[p]
is a whole thenlist_votos[p] * lst[p]
is a tuple and tuples cannot be the numerator of a division. But I’m not quite sure what you want the program to actually do to suggest a fix.– hugomg
Thanks for the reply. What I want is for the program to make a calculation according to the D'Hondt method. Each number inside the list
n
is a number of seats to be distributed according to the vote that is present in the corresponding Pool withinvotacoes
.– Marcos Lopes
The output would be a tuple stack with the result of seating distribution. And I can make it work if I give each element individually, like this:
assembleia(16, (0, 15729, 220408, 1297, 0, 3040, 993, 0, 1354, 1046, 0, 3284, 99652, 19327, 0))
get the expected return(0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0)
– Marcos Lopes