parity.py 616 Bytes
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
# Author: Brice Colombier
# Laboratoire Hubert Curien
# 42000 Saint-Etienne - France
# Contact: b.colombier@univ-st-etienne.fr
# Project: CASCADE
# File: parity.py
# Date : 2016-10-12


def parity(vect_in_A, par_B):
"""Check if the vector parity is equal to a given parity

>>> parity([0, 1, 0, 1], 0)
0
>>> parity([1, 1, 0, 1], 1)
0
>>> parity([1, 1, 1, 1], 1)
1
>>> parity([0, 1, 0, 0], 0)
1

"""

par_A = 0
for i in vect_in_A:
par_A ^= i
return int(par_A) ^ int(par_B)

if __name__ == "__main__":
print(parity([0, 0, 0, 0], 1))