Question

I am trying to create a code and Being a beginner in Python, I am having some trouble. So the code should to this:

It generates a random number with randint and put in into the list l = [] and do this as long as range.

Function repetition(l) that should check all numbers and returns the number of try(the postion(index)) of the first duplicate element.

Function repeater(count) that repeats 1-365 the repetition function long enough (1-365) and enumerating with the help of an array, until a repeat occurs.

import random

l = []

for _ in range(365):
    n = random.randint(1, 365)
    if n not in l:
    l.append(n)

print(l)

I think thats the same only int the in a funktion form:

numb = 365
def repetition(numb):
    for i in range(1,numb+1):
        i = random.randint(1, numb+1)
        if i not in l:
        l.append(n)

#def repeater(count):

Is is it also possible to do it with a dictionary instead of a list?

Stuck on step checking the Element and creating the repeater function

Was it helpful?

Solution

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.

OTHER TIPS

I'm still not totally sure, but I think this does what you want:

def repetition(numb):
    l = [random.randint(1, numb)]
    while True:
        n = random.randint(1, numb)
        l.append(n)
        if n == l[0]:
            return l

That will create a list where the first and last values are the same with other random numbers in between:

l = repetition(numb)
print(l)
print("Took {0} random numbers to repeat {1}".format(len(l), l[0]))

Example for numb == 10:

[8, 10, 5, 3, 2, 5, 8]
Took 7 random numbers to repeat 8

I have added print statement to see what is happening.

import random
numb = 20
def repetition(numb):
    l=[] # create a list
    for i in range(1,numb+1):
        i = random.randint(1, numb+1)
        l.append(i) # add i to list
        print l,i
        if len(l) > 1 and i == l[0]: # if list is longer than one element and i == first element
            return len(l) # return length of the list
repetition(numb)
[19] 19
[19, 21] 21
[19, 21, 19] 19
Out[21]: 3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top