Compare items in a list in Python

2

I need to solve an exercise and I do not know how to finish it because I have a hard time making comparisons and other lists. The exercise asks me to build a program that uses the Egyptian multiplication method, giving me an example 125x41 . The Egyptian method consists of 2 columns:

Step 1:

Column 1: Put the powers of 2 (including 1) until you reach or approach the indicated number (41 in this case). We should stay like this [1, 2, 4, 8, 16, 32].

Column 2: It is multiplied by 2 the number indicated (125 in this case) the same number of times that the first column has been strengthened (the column of 41). We should stay like this [125, 250, 500, 1000, 2000, 4000].

Step 2:

Column 1: Now, taking the last number (32), we have to add it with the others until we get 41. Since 31 + 16 is greater than 41, we discard the 16 and continue with the other. We only add all the cases that give less than 41 until we reach the exact number.

Column 2: In this column we are only going to add specific results that have to do with the first Column. That is, for us to be 41 in column 1, we add only [1, 8, 32] and in column 2 we have to add those 3 numbers that are in the same position as the other column [125, 1000, 4000 ].

This will give us the final result.

So far I was able to do the step 1 , but I do not know how to do the step 2 , can anyone help me? is urgent, thank you very much :

lista1 = []

lista2 = []

n1 = int(input("Ingrese primer número: "))

n2 = int(input("Ingrese segundo número: "))

a = 1

cont1 = 0

cont2 = 0

while a < 2:

    lista1.append(a)

    a = a + 1

    while a <= n2:

        lista1.append(a)

        a = a * 2

z = len(lista1)


while cont1 < z:

    lista2.append(n1)

    n1 = n1 * 2

    cont1 = cont1 + 1
    
asked by Frannw_88 12.12.2018 в 00:35
source

2 answers

0

This would be the problem solved.

I had to create a function so that I convert the number to binary and know what values can be added to them. I could have put it in a cycle, but it's more formal like that.

# Permite determinar los valores 
# Ej: para 41
#   32, 16, 8, 4, 2, 1
#    1,  0, 1, 0, 0, 1      # En binario las posiciones que valen 1 se suman
#
# Nota: Devuelve tal cual muestra arriba. No la invierte porque 
# posteriormente buscaremos el valor del índice 1
def convertir_a_binario(decimal):
    lista = []
    while decimal > 1:
        resto = decimal % 2
        lista.append(resto)
        decimal = decimal // 2
    lista.append(decimal)
    return lista

lista1 = []
lista2 = []

n1 = int(input("Ingrese primer número: "))
n2 = int(input("Ingrese segundo número: "))

# Variables auziliares para evitar que se 
# sobreescriba su valor en las sig. operaciones
aux_num1 = n1
aux_num2 = n2

a = 1
cont1 = 0
cont2 = 0
while a < 2:
    lista1.append(a)
    a = a + 1
    while a <= n2:
        lista1.append(a)
        a = a * 2
z = len(lista1)

while cont1 < z:
    lista2.append(n1)
    n1 = n1 * 2
    cont1 = cont1 + 1

# Esta lista contiene en cada posición el resto de la
# división del número dado
valores = convertir_a_binario(aux_num2)
suma = 0
for i in range(len(lista2)):
    suma = suma + lista2[i] * valores[i]    # Si valores[i] es 0 a la variable suma no se le agrega nada

print("El resultado de {} * {} es: {}".format(aux_num1, aux_num2, suma))

This instruction will add only the values of the list that when multiplied by 1, in case the values are 0 the result will not affect the sum (simple math)

suma = suma + lista2[i] * valores[i]    
    
answered by 12.12.2018 в 15:51
0

Thanks for the help Andy, it helped me a lot. Although I did a small edition and it stayed like this:

def convertir_a_binario(decimal):

    lista = []

    while decimal > 1:

        resto = decimal % 2

        lista.append(resto)

        decimal = decimal // 2

    lista.append(decimal)

    return lista

n1 = int(input("Ingrese primer número: "))

n2 = int(input("Ingrese segundo número: "))

listan2 = convertir_a_binario(n2)

listan1 = []

while len(listan1) < len(listan2):

    listan1.append(n1)

    n1 = n1*2

resultado = 0

for posicion in range (len(listan2)):

    if listan2[posicion] == 1:

        resultado = resultado + listan1[posicion]

print (resultado)
    
answered by 13.12.2018 в 03:48