Search code examples
pythonprobabilityphysics

Trying to compare each element location, and if the location are the same return false, otherwise return true


I have tried to write here a code that takes a random permutation from random_list and compares each element's location. I do get a False and True result but not sure that my code goes through the list and compares every single element. Any suggestion will be helpful.

  def not_correct_order(n):
    randomList = random_List(n)
    counter = 0
    while (counter <n):
        if(randomList[counter] == counter):
                return False
                counter = counter+1
        return True

tried a.any() and a.all() and a i thought using np.isclose().


Solution

  • Thanks to Tim Roberts, he helped me to figure out my answer. by using a.any() I could compare my list to its position and if any element was not in the correct position it gives back a false.

    def not_correct_order(n):
      randomList = random_List(n)
    
      return any(i==j for i,j in enumerate(randomList))