Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encode(message):
- parity = 1
- while len(message)+parity+1 > 2**parity:
- parity += 1
- for i in range(0,parity):
- message = message[:2**i-1]+"_"+message[2**i-1:]
- check = 0
- while check < parity:
- dist = 2**check
- pos = dist-1
- total = 0
- while pos < len(message):
- for i in range(pos,pos+dist):
- try:
- if message[i] != "_":
- total += int(message[i])
- except IndexError:
- break
- pos += 2*dist
- if total &1 == 1:
- message = message[:dist-1] +"1" +message[dist:]
- else:
- message = message[:dist-1] +"0" +message[dist:]
- check += 1
- return message
- def decode(message):
- parity = 1
- while len(message)+parity+1 > 2**parity:
- parity += 1
- check = 0
- wrong = 0
- while check < parity:
- dist = 2**check
- pos = dist-1
- total = 0
- while pos <= len(message):
- for i in range(pos,pos+dist):
- try:
- if message[i] != "_":
- total += int(message[i])
- except IndexError:
- break
- pos += 2*dist
- if total &1 != 0:
- wrong += dist
- check += 1
- if wrong != 0:
- if message[wrong-1] == "0":
- message = message[:wrong-1] +"1" +message[wrong:]
- else:
- message = message[:wrong-1] +"0" +message[wrong:]
- print("error found, corrected")
- return message
- print(encode("100101"))
- print(decode("0111001101"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement