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

Question:

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().

Asked By: Ahmad Morwat

||

Answers:

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))
Answered By: Ahmad Morwat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.