Program that creates two vectors with 10 random elements between 1 and 100

-1

Make a program that creates two vectors with 10 random elements between 1 and 100. Generate a third vector of 20 elements, whose values should be composed of the interleaved elements of the two other vectors. Print the three vectors.

I would like to know if the code I made is correct and if possible show me another way to thank you.

import random
lista1 = []
lista2 = []
lista3 = []

while len(lista1) < 10:
    numero = random.randint(1,100)
    if numero not in lista1:
        lista1.append(numero)
        lista3.append(numero)


while len(lista2)  < 10:
    numero = random.randint(1,100)
    if numero not in lista2:
        lista2.append(numero)
        lista3.append(numero)

lista1.sort()        
lista2.sort()
lista3.sort()

print(lista1)
print(lista2)
print(lista3)
    
asked by anonymous 08.08.2016 / 01:25

1 answer

2

If you use the sort method, we will not know if the elements are interleaved. An example: lista1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] e lista2 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20] . In this case the lista3 would be [1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9, 19, 10, 20] .

I suggest dividing the logic into two parts: first assemble the lists, without drawing lots and without the concern of having different elements. Then use the possible indexes, with range(10) to go including the elements in lista3 in a loop. First of all lista3.append(lista1[0]) lista3.append(lista2[0]) and so on.

    
08.08.2016 / 01:37