-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparity.py
More file actions
55 lines (41 loc) · 1.23 KB
/
parity.py
File metadata and controls
55 lines (41 loc) · 1.23 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def parity_index(bits):
bit_index = 2
parity_location = [0]
while bit_index <= len(bits):
parity_location.append(bit_index - 1)
bit_index = bit_index * 2
return parity_location
def parity_range(bits, interator):
result = []
next_bit = interator - 1
cicle = interator
for index, bit in enumerate(bits):
if index == next_bit:
if index not in parity_index(bits):
result.append(index)
cicle -= 1
if cicle == 0:
next_bit += interator + 1
cicle = interator
else:
next_bit += 1
return result
def parity(bits, interator):
result = 0
for index in parity_range(bits, interator):
result += bits[index]
return 0 if result % 2 == 0 else 1
def insert_parity(bits):
for bit_index in parity_index(bits):
bits.insert(bit_index, 0)
return bits
def remove_parity(bits):
result = []
for index, bit in enumerate(bits):
if index not in parity_index(bits):
result.append(bit)
return result
def calculate_parity(bits):
for bit_index in parity_index(bits):
bits[bit_index] = parity(bits, bit_index + 1)
return bits