algorithm in python, list with 100 numbers without repeating and get the sum of odd and even

-1

They ask me the following:

  

Perform an algorithm that generates 100 random numbers from 1 to 200.   The system must save these random numbers in a range of 100   positions, in this arrangement you should not repeat any number generated.   After filling the arrangement with the 100 numbers, make the sum of the   odd and even numbers and according to the result, show the message   which was greater (the sum of the pairs or the sum of the odd ones), in case   that the result is equal, show the two results of the sums.

The theme is functions and arrangements, so I started like this:

from random import randrange
from time import sleep

numeros = []
validacion = []
longitudx = 10
longitudy = 10

def lista():
    numeros.append([])

def imprimir_numeros():
    longitud = len(numeros)

    for i in range(longitud):
        print(numeros[i])

def validacion(numero):
    for i in range(longitudx):
        for j in range (longitudy):
            if numeros[i][j] == numero :
                return False
                break
    return numero

numeros = randrange(200)

def llenar_numeros():
    x = 0
    while x < longitudx:
         y = 0
         while y < longitudy:
             numero = validacion(randrange(200))
             if numero:
                 numeros[x][y] = numero
                 y += 1

        imprimir_numeros()
        print(" ")
        print("  ")
        x += 1

def sumatoria():
    for numero in numeros:
        if numero // 2 == 0:
            suma_pares = numero
        print(" La suma de los numeros pares es " + str(suma_pares) )

        elif numero // 2 !=0:
            suma_impares = numero
            print(" La suma de los impares es " + str(suma_impares))

llenar_numeros()
sumatoria()

At the time of running it gives me several errors, I need help to fill the list with the numbers and then I identify the odd and even and then make the sum of them.

    
asked by Lina Judith Pacheco 14.10.2017 в 16:47
source

4 answers

0

This way you could generate the list of numbers without repeating it, maybe it is not the most optimal but it can help you get an idea.

def generate_random_list_int():
    numbers = []

    while True:
       number = random.randrange(1, 201)
       if number not in numbers:
          numbers.append(number)
          if len(numbers) == 100:
              break
    return numbers

Removing odd and even numbers is also simple:

numbers = generate_random_list_int()

pares = sum(filter(lambda x: x % 2 == 0, numbers))
impares = sum(filter(lambda x: x % 2 != 0, numbers))

This is also not the best way to get the sum, but again you get an idea. The operator mod or % is used which returns the remainder of a division, if it gives 0, it is an even number, otherwise it is an odd number, and the function sum that is integrated is used with python to add the numbers within a list.

Any questions comment.

    
answered by 14.10.2017 в 17:25
0
  

Perform an algorithm that generates 100 random numbers from 1 to 200.   The system must save these random numbers in a range of 100   positions, in this arrangement you should not repeat any number generated.   After filling the arrangement with the 100 numbers, make the sum of   the odd and even numbers and according to the result, show the   message which was greater (the sum of the pairs or the sum of the   odd), in case the result is the same, show the two   results of the sums.

You can use a set, instead of a list. A set is defined as a disordered collection of unique elements. Therefore never in a set will any element be repeated.

Using your add method, we can fill it with unique elements without checking that the element is in the array.

from random import randrange

numeros = set()


while len(numeros) < 100:
  numero = randrange(200)
  numeros.add(numero)

print(numeros)

suma_pares = sum([n for n in numeros if n % 2 == 0])
suma_impares = sum([n for n in numeros if not n % 2 == 0])

mayor = 'pares' if suma_pares > suma_impares else \
        'impares' if suma_impares > suma_pares else \
        None

if mayor:
  print('La suma mayor es la de los números: ', mayor)
else:
  print('Los pares e impares suman lo mismo')

  print('Suma pares: ', suma_pares)
  print('Suma impares: ', suma_impares)

Working example

    
answered by 14.10.2017 в 18:25
0

The sample function of random allows you to generate a population without repetitions.

import random
population = random.sample(range(1, 200), 100)
sum_even_numbers = sum(x for x in population if x % 2 == 0)
sum_odd_numbers = sum(x for x in population if x % 2 != 0)
if sum_even_numbers > sum_odd_numbers:
    print('Suma pares mayor:', sum_even_numbers)
elif sum_even_numbers < sum_odd_numbers:
    print('Suma impares mayor:', sum_odd_numbers)
else:
    print('Sumas iguales')
    print('Impares:', sum_odd_numbers)
    print('Pares:', sum_even_numbers)

Greetings

    
answered by 05.02.2018 в 15:21
0

This is a very simple and efficient way, simply when you are generating random elements from 1 to 200 you will analyze if it is not in the list, if you are not adding it to a list of odd or even pairs, and when they have been 100 elements in total, (which is added to a separate list), evaluates the maximum sum and prints.

from sys import stdin
import random
def main():
    lista=[]
    pares=[]
    impares=[]
    while len(lista)<100:
        numero=random.randint(1,200)
        if numero not in lista:
            lista.append(numero)
            if numero%2==0:
                pares.append(numero)
            else:
                impares.append(numero)

    resultado=(max(sum(pares),sum(impares)))
    if resultado==pares: print('la suma de pares es la mayor')
    else: print('la suma de impares es la mayor')
main()
    
answered by 22.06.2018 в 21:09