-1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
djennacs
  • 107
  • 1
  • 7
  • `for` `if` `x%2`... it all starts by trying something... – Julien Oct 02 '18 at 23:39
  • It's not an easy question...been trying my best. Can't seem to get past: `for i in range(len(lst)-1): if lst[i] % 2 == 0 and lst[i+1] % 2 != 0: return True` I seem to only be checking the first and second element and going nowhere. – djennacs Oct 02 '18 at 23:43
  • Then show what you've tried... – Julien Oct 02 '18 at 23:43
  • Put your code in the question itself please. – Julien Oct 02 '18 at 23:52
  • Iterate through your array and check if somewhere the parity of the current element is same as that of the next element. If so you don't have alternating parity. Only if you can go through the full array (except last element), parity is alternating. – Michael Butscher Oct 02 '18 at 23:55

4 Answers4

3

You can use the Python modulus operator % on adjacent values within an input list and check for equality while iterating.

If you choose this route and efficiency is a concern, I advise you use 3rd party libraries such as NumPy / numba so the iteration does not take place at Python level:

from numba import jit

@jit(nopython=True)
def check_alt_parity(L):
    for i in range(len(L)-1):
        if L[i] % 2 == L[i+1] % 2:
            return False
    return True

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

A = [1, 2, 3, 4] * 10000
B = [1, 3, 2, 4] * 10000

%timeit check_alt_parity(A)       # 780 µs
%timeit check_alt_parity_list(A)  # 9.09 ms
jpp
  • 159,742
  • 34
  • 281
  • 339
1

Try this function, l for list comprehension and getting bool for odd or even, then checking if all are true (the every second even index element is all True or all False, and same with every odd index element:

def oddeven_alter(l):
    l=[i%2 for i in l]
    return all([any([all(l[::2]),all(not i for i in l[::2])]),any([all(l[1::2]),all(not i for i in l[1::2])])])
)
print(oddeven_alter([1,2,3,4]))
print(oddeven_alter([1,3,2,4]))

Output:

True
False

Kind of complex looking tho

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

Here

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

You can iterate over all indices of your sequence and then compare it with the next one:

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

print(is_alternating_parity([1, 2, 3, 4]))  # True
print(is_alternating_parity([1, 3, 2, 4]))  # False
zwer
  • 24,943
  • 3
  • 48
  • 66