diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..3d1adcf Binary files /dev/null and b/.DS_Store differ diff --git a/lesson_1/.DS_Store b/lesson_1/.DS_Store new file mode 100644 index 0000000..dbe9b46 Binary files /dev/null and b/lesson_1/.DS_Store differ diff --git a/lesson_1/task1.py b/lesson_1/task1.py new file mode 100644 index 0000000..a84684f --- /dev/null +++ b/lesson_1/task1.py @@ -0,0 +1,5 @@ +num_arr = input().split(" ") +num_sum = 0 +for i in num_arr: + num_sum += float(i) +print(num_sum) diff --git a/lesson_1/task2.py b/lesson_1/task2.py new file mode 100644 index 0000000..1e9148c --- /dev/null +++ b/lesson_1/task2.py @@ -0,0 +1,5 @@ +current_str = input() +while(True): + replace_arr = input().split("->") + current_str = current_str.replace(replace_arr[0], replace_arr[1]) + print(current_str) diff --git a/lesson_1/task3.py b/lesson_1/task3.py new file mode 100644 index 0000000..b1d6d1b --- /dev/null +++ b/lesson_1/task3.py @@ -0,0 +1,5 @@ +import math + +num_arr = input().split(" ") +for i in num_arr: + print(int(math.floor(abs(float(i)))), end=" ") diff --git a/lesson_1/task4.py b/lesson_1/task4.py new file mode 100644 index 0000000..65d43aa --- /dev/null +++ b/lesson_1/task4.py @@ -0,0 +1,5 @@ +num_arr = input().split(" ") +num_sum = 0 +for i in num_arr: + num_sum += float(i) ** 2 +print(num_sum) diff --git a/lesson_1/task5.py b/lesson_1/task5.py new file mode 100644 index 0000000..0f0b7a5 --- /dev/null +++ b/lesson_1/task5.py @@ -0,0 +1,4 @@ +unencoded_str = input().replace(" ", "") +key = int(input()) +for i in unencoded_str: + print(chr(96 + (ord(i) + key - 96) % 26), end="") diff --git a/lesson_2/dearchiver_lwz.py b/lesson_2/dearchiver_lwz.py new file mode 100644 index 0000000..8dda575 --- /dev/null +++ b/lesson_2/dearchiver_lwz.py @@ -0,0 +1,55 @@ +import math +import sys + +if not (2 <= len(sys.argv) <= 3): + print('Usage: python', sys.argv[0], '{encoded_file}.lwz (output_file)') + exit(-1) + +in_path = sys.argv[1] + +out_path = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1][:-4][:sys.argv[1][:-4].find('.')] + '_dec' \ + + sys.argv[1][:-4][sys.argv[1][:-4].find('.'):] + +dictionary = {i: (i,) for i in range(256)} + +current_bytes = [] + +with open(in_path, 'rb') as file_in: + with open(out_path, 'wb') as file_out: + bites = math.ceil(math.log2(len(dictionary))) + bytes = bites // 8 + if bytes % 8 == 0: + bytes += 1 + sym = int.from_bytes(file_in.read(bytes), byteorder='little') + file_out.write(bytearray(dictionary[sym])) + current_bytes.append(dictionary[sym][0]) + current_bytes.append(sym) + current_bytes = list(dictionary[sym]) + while(True): + bites = math.ceil(math.log2(len(dictionary))) + bytes1 = bites // 8 + bytes1 += 1 + sym = file_in.read(bytes1) + if not sym: + break + sym = int.from_bytes(sym, byteorder='little') + #print('byte array:', current_bytes) + #print('int symbol:', sym) + if sym in dictionary: + file_out.write(bytearray(dictionary[sym])) + current_bytes.append(dictionary[sym][0]) + dictionary[len(dictionary)] = tuple(current_bytes) + current_bytes = list(dictionary[sym]) + else: + current_bytes.append(current_bytes[0]) + file_out.write(bytearray(current_bytes)) + dictionary[len(dictionary)] = tuple(current_bytes) + #print('=====================') + +print('new entries in the dictionary:') +printable_key = [] +for i in range(256, len(dictionary)): + for j in range(len(dictionary[i])): + printable_key.append(chr(dictionary[i][j])) + print(i, '->', ''.join(printable_key)) + printable_key.clear() \ No newline at end of file