For instance, let's say I have a random array [5,2,1,3,6,4]
and I want to access it at least 6 times (i.e. 6 == len(array)) (the acess order doesn't matter, nor does it matter that I've accessed them all). How can I randomly access the whole array without popping any of the values ?
In other words, I am trying to write a function find_new_index
which takes an index i
such that:
arr = [5,2,1,3,6,4]
i=0
x = 0
while i < 6:
access = arr[i]
x+=1
i = i + find_new_index(i)
assert(x >= 6)
The idea is that this function works for any random array of size N which has random values from 1 to N.
PS : The easy way to write find_index
is to generate a random number within it. However, I want to know if it's possible to write this function without loops or any function calls.
This differs from other questions, since I don't want to use the Random Module.