-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinear_block_code.py
More file actions
89 lines (79 loc) · 2.29 KB
/
linear_block_code.py
File metadata and controls
89 lines (79 loc) · 2.29 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def check_wrong_dataword(dataword):
if len(dataword) != 4:
return 1
for char in dataword:
if char != '0' and char != '1':
return 2
return 0
def input_datawords(number_of_words):
datawords = []
for i in range(number_of_words):
result = 3
while result != 0:
dataword = input('enter the dataword: ')
result = check_wrong_dataword(dataword)
if result == 1:
print('all datawords should be of length 4')
elif result == 2:
print('a dataword should be made up of only 0s and 1s')
datawords.append(dataword)
print()
return datawords
def generate_codewords(datawords):
codewords = []
for dataword in datawords:
count1 = dataword.count('1')
if count1 % 2 == 0:
dataword += '0'
else:
dataword += '1'
codewords.append(dataword)
return codewords
def get_valid_codewords_list():
valid_codewords_list = []
with open('list.txt') as file:
for i in range(16):
string = file.readline()
valid_codewords_list.append(string[:-1])
file.close()
return valid_codewords_list
def xor(bit1, bit2):
if bit1 == '0' and bit2 == '0':
return str(0)
elif bit1 == '1' and bit2 == '1':
return str(0)
else:
return str(1)
def xor_of_codewords(code1, code2):
xor_result = ''
for i in range(len(code1)):
xor_result += xor(code1[i], code2[i])
return xor_result
def main():
print('4B/5B Linear Block Coding\n\n')
datawords = input_datawords(int(input('enter number of words: ')))
codewords = generate_codewords(datawords)
valid_codewords_list = get_valid_codewords_list()
for code in codewords:
print(str(code), end=' ')
if code in valid_codewords_list:
print('valid')
else:
print('invalid')
print('\nenter indices of the two words to find xor (index from 1 to '+str(len(codewords))+'):')
i = 100
j = 100
while i not in range(1, len(codewords)+1):
i = int(input('first word: '))
print()
while j not in range(1, len(codewords)+1) or j == i:
j = int(input('second word: '))
print()
xor_result = xor_of_codewords(codewords[i-1], codewords[j-1])
print(codewords[i-1]+' xor '+codewords[j-1]+' = '+xor_result)
if xor_result in valid_codewords_list:
print('it is also a valid codeword')
else:
print('xor has generated an invalid codeword')
if __name__ == '__main__':
main()