Last active
January 26, 2020 05:12
-
-
Save Kinjalrk2k/d4c66cf2b91efa8baeb1a701126f417b to your computer and use it in GitHub Desktop.
1D Parity Checker - Includes manual manipulation of the data to illustrate error cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# EVEN PARITY | |
import functools, random | |
# parity = functools.reduce(lambda a, b: a ^ b, list(map(int, list(str(bin(num))[2:])))) | |
# print(parity) | |
def parityGenerator(msg): | |
binary_li = list(map(int, list(msg))) | |
parity = functools.reduce(lambda a, b: a ^ b, binary_li) | |
return parity | |
def parityChecker(msg): | |
p = parityGenerator(msg) | |
if p == 0: | |
return True | |
else: | |
return False | |
data_send = int(input("Enter a number: ")) | |
binary_send = str(bin(data_send))[2:] | |
p = parityGenerator(binary_send) | |
print(f"Binary Message: {binary_send}\nParity: {p}") | |
sender = binary_send + str(p) | |
print(f"Transmitted message: {sender}") | |
# Manually manupulating the message during transmission to illustrate error | |
print("\n") | |
transmission = sender | |
transmission = list(map(int, list(transmission))) | |
error_pos = random.randint(0, 2*len(transmission)) # equate the probability of error/no-error | |
if error_pos < len(transmission): | |
transmission[error_pos] = 1 - transmission[error_pos] | |
print("Data was manipulated during transmission") | |
transmission = ''.join(list(map(str, transmission))) | |
print(f"In Transit: {transmission}") | |
print("\n") | |
receiver = transmission | |
binary_receive = receiver[:-1] | |
data_receive = int(binary_receive, 2) | |
print(f"Received message: {receiver}\nBinary message: {binary_receive}\nData Received: {data_receive}") | |
if parityChecker(receiver): | |
print("Tranmission is error-free!") | |
else: | |
print("There's an error in tranmission!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment