Search code examples
pythonfor-loopparity

Checking for alternating parity


I am struggling to find a way to check if every other number in my list is of an alternating parity (i.e. even, odd, even, odd, etc.)

[1,2,3,4] # odd, even, odd, even (True, because they alternate)
[1,3,2,4] # odd, odd, even, even (False, because they don't alternate)

Anyone have any idea how I can check for this?


Solution

  • Here

    def alter(ls):
      for i in range(len(ls)-1):
          if ls[i]%2 == ls[i+1]%2:
             return False
      return True