I don't fully understand what the question is, but I wrote this from what I understood:
import random
TRIES = 365
def generate_random_numbers(n):
"""Generates a list of n random numbers"""
random_numbers = []
for _ in range(n):
random_number = random.randint(1, n)
random_numbers.append(random_number)
return random_numbers
def repetition(random_numbers):
"""Given a list of random numbers, it will return the index of the first repeated element"""
for index, random_number in enumerate(random_numbers):
if random_number in random_numbers[:index]:
# You can print the list up to but not including the first repeated element
# using list slicing: print '{}'.format(random_numbers[:index])
return index
def repeater(n):
indices_of_first_duplicates = []
for i in range(n):
random_numbers = generate_random_numbers(n)
indices_of_first_duplicates.append(repetition(random_numbers))
return indices_of_first_duplicates
repeater_result = repeater(TRIES)
print '{}'.format(repetition(repeater_result))
In the code above, what genearte_random_numbers
and repetition
do is documented. repeater
generates n
list of random numbers. For each list, it finds the index of the first repetition and stores all those indices in a list and returns that list. Finally, from what I understood/guessed, you then want to see in the list return repeater
, what is the index of the first duplicate (i.e. After how many tries is the same random number generate twice at the same index.)
I might be entirely on the wrong track so I do apologise if I've misunderstood your problem.