zad 3 #29

Closed
s426285 wants to merge 5 commits from s426285/DALGLI0:zad3_426285 into master
Showing only changes of commit c93bc4b37e - Show all commits

12
hw3.py
View File

@ -1,5 +1,6 @@
from sys import argv from sys import argv
from fractions import gcd from fractions import gcd
from ast import literal_eval
class Polynomial(): class Polynomial():
def __init__(self, lst, mod): def __init__(self, lst, mod):
@ -112,7 +113,7 @@ def to_bin(x):
if len(data) < 8: if len(data) < 8:
data = str(0) + data data = str(0) + data
else: else:
data.replace(data[0], '') data = data[1:]
return data return data
def to_ascii_val(lst): def to_ascii_val(lst):
@ -120,6 +121,7 @@ def to_ascii_val(lst):
for i in range(len(lst)): for i in range(len(lst)):
if lst[i] == 1: if lst[i] == 1:
sum += 2**(7-i) sum += 2**(7-i)
return chr(sum) return chr(sum)
def data(): def data():
@ -157,16 +159,18 @@ def check(m):
def main(): def main():
global m global m
m = list(argv[1]) m = list(argv[2])
mode = argv[2] # flagi -e -d (encode, decode) mode = argv[1] # flagi -e -d (encode, decode)
if mode == '-e': if mode == '-e':
res = fcs(m) res = fcs(m)
fcs_ch1 = to_ascii_val(res[:8]) fcs_ch1 = to_ascii_val(res[:8])
fcs_ch2 = to_ascii_val(res[8:]) fcs_ch2 = to_ascii_val(res[8:])
m.append(fcs_ch1) m.append(fcs_ch1)
m.append(fcs_ch2) m.append(fcs_ch2)
print(''.join(m)) print(m)
elif mode == '-d': elif mode == '-d':
to_check = literal_eval(argv[3])
m += to_check
print(check(m)) print(check(m))
if __name__ == '__main__': if __name__ == '__main__':