-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammingEncoding.py
More file actions
67 lines (52 loc) · 1.52 KB
/
HammingEncoding.py
File metadata and controls
67 lines (52 loc) · 1.52 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
56
57
58
59
60
61
62
63
64
65
66
67
def listToString(s):
# Initialize an empty string
listStr = ""
# Traverse in the string
for ele in s:
listStr += ele
# Return string
return listStr
def isBinary(num):
# Checking if the input is binary
for i in str(num):
# If digit is 1 or 0
if i in '10':
binary = True
else:
binary = False
break
return binary
def hammingEncode(binary):
if not isBinary(binary):
return "Error: Input must be binary!"
else:
return checkEncoding(binary)
def checkEncoding(checkNum):
numList = []
parity = 1
y = 0
location = 2
checkList = list(checkNum)
while (len(checkList) + y + 1) > 2 ** y:
y += 1
# Assigning placeholder values
for i in range(len(checkList)):
if i + 1 == parity:
checkList.insert(parity - 1, 'y')
parity *= 2
numList.append(i)
for x in numList:
codeList = []
for p in range(x, len(checkList), location):
status = checkList[p:int(p + (location / 2))]
for n in status:
codeList.append(n)
location *= 2
# Counting occurance of 1, checking if even and assigning it 1 or 0
numCounter = codeList.count('1')
if numCounter % 2 == 0:
checkList[x] = "0"
else:
checkList[x] = "1"
return listToString(checkList)
print(hammingEncode("1100"))