dimanche 31 octobre 2021

How to write a recursive function that will return an index from large array [closed]

I have an array of the size 5000. From that 5000 items first I have to choose random 100 items and from that 100 items, I have to choose 50 random items. Similarly, I have to select half number of items until I get only 1 item.

  def hamster(data):
    rand_100 =[] #list of 100 random indices
    rand_50=[]   #list of 50 random indices
    rand_25=[]   #list of 25 random indices
    rand_12=[]   #list of 12 random indices
    rand_6=[]    #list of 6 random indices
    rand_3=[]    #list of 3 random indices
    rand_1=[]    #list of 1 random index 
    for i in range(100):
        rand = randint(0,5000)
        rand_100.append(rand)
    for i in range(50):
        rand = randint(0,len(rand_100)-1)
        rand_50.append(rand_100[rand])
        
    for i in range(25):
         rand = randint(0,len(rand_50)-1)
         rand_25.append(rand_50[rand])
    for i in range(12):
         rand = randint(0,len(rand_25)-1)
         rand_12.append(rand_25[rand])
    for i in range(6):
         rand = randint(0,len(rand_12)-1)
         rand_6.append(rand_12[rand])
    for i in range(3):
         rand = randint(0,len(rand_6)-1)
         rand_3.append(rand_6[rand])
    for i in range(1):
         rand = randint(0,len(rand_3)-1)
         rand_1.append(rand_3[rand])
    return data[rand_1[0]]



Aucun commentaire:

Enregistrer un commentaire