J
There's a mistake in your function. completarBinarioYou're using extend to add zeros to your binary and complete it to a certain length. But you're adding the zeros at the end of the binary, when they should be at first.I mean, 2 makes it 10000, but I should turn it into 00010. To get this use binario.insert(0, '0') (insert in index 0 the character '0').In the code you pass, you also have this:listaMontones = convertir_montones(listaMontones)
listaMontones = []
Then you're accessing listaMontones[y][x] when you've assigned listaMontones with an empty list, so it'll give IndexError: list index out of rangeThe first for nesting sumas, should iterate between 0 and len(listaMontones[0])To make sure we don't get out of each binary's digit number.The second for nesting sumas, should iterate between 0 and len(listaMontones)to receive a variable number of numbers.You also need a bit-sum logic, because in decimal 1 + 1 = 2, but in binary 1 + 1 = 10, and in that case, if I don't get it wrong, you want the result to be 0, not 1, or 2.This translates returning 1 if both numbers are different (an exclusive XOR/or):def sumar_digito_binario(dig1, dig2):
return 1 if (dig1 and not dig2) or (not dig1 and dig2)
# Es equivalente a esto:
# return 1 if ((dig1 == 1) and (dig2 == 0)) or ((dig1 == 0) and (dig2 == 1)) else 0
According to that:dig1dig2XOR(dig1, dig2)000011101110In the end, you get this code, which I think does what you want (I changed only what I indicated above, the rest is your code):def decimalToBinario(n):
"""
Función auxiliar con la que se obtiene la representación binaria
de un número en base 2
"""
if (n <= 0):
return ['0']
binario = ''
while (n > 0):
resto = int(n % 2)
n = int( n / 2)
binario = str(resto) + binario
return list(binario)
def completarBinario(binario, longitudFinal):
"""
Función auxiliar con la que se completa la lista con valores nulos
hasta que alcanza la longitud especificada
"""
binario = list(binario)
if (len(binario) < longitudFinal):
while len(binario) < longitudFinal:
binario.insert(0, '0')
len(binario) + 1
return binario
def convertir_montones(listaMontones):
"""
Función auxiliar para convertir la lista de montones en una lista de
representaciones binarias de la lista de montones
"""
for i in range(len(listaMontones)):
listaMontones[i] = completarBinario(decimalToBinario(listaMontones[i]), 5)
i = i + 1
return list(listaMontones)
def sumar_digito_binario(dig1, dig2):
return 1 if (dig1 and not dig2) or (not dig1 and dig2) else 0
def sumas(listaMontones):
listaMontones = convertir_montones(listaMontones)
sumColumna = []
for x in range(len(listaMontones[0])):
sumColumna.append(0)
for y in range(len(listaMontones)):
= sumar_digito_binario(sumColumna[x], int(listaMontones[y][x]))
return sumColumna
And these results:print(sumas([2, 3])) # Muestra [0, 0, 0, 0, 1]
print(sumas([5, 0, 2, 7, 3])) # Muestra [0, 0, 0, 1, 1]
Shall we check that?bin(2) = 10
bin(3) = 11
suma = 01
bin(5) = 101
bin(0) = 000
bin(2) = 010
bin(7) = 111
bin(3) = 011
suma = 001