diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..2b7e46d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.formatting.provider": "yapf" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..ad7a967 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,21 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Run program (.py)", + "command": "python3", + "args": [ + "${file}" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + } + ] +} \ No newline at end of file diff --git a/hw1/task_1.py b/hw1/task_1.py new file mode 100644 index 0000000..a6be8ad --- /dev/null +++ b/hw1/task_1.py @@ -0,0 +1,2 @@ +if __name__ == "__main__": + print(sum(map(float, input().split()))) \ No newline at end of file diff --git a/hw1/task_2.py b/hw1/task_2.py new file mode 100644 index 0000000..be22bcb --- /dev/null +++ b/hw1/task_2.py @@ -0,0 +1,34 @@ +def input_data(): + print('Enter main text:') + text = input() + print( + 'Enter substitution rules (e. g. A->B, C->D, ...), enter "end" to stop:' + ) + data = input() + substitution_rules = {} + while data != 'end': + pair = data.split('->') + substitution_rules[pair[0]] = pair[1] + data = input() + return text, substitution_rules + + +def print_data(text, substitution_rules): + print('Input data:') + print(text) + for i in substitution_rules.keys(): + print(i, substitution_rules[i]) + + +def process_data(text, substitution_rules): + for i in substitution_rules: + text = text.replace(i, substitution_rules[i]) + return text + + +if __name__ == "__main__": + # text, substitution_rules = input_data() + text = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG' + substitution_rules = {'A': '4', 'I': '1', 'E': '3'} + text = process_data(text, substitution_rules) + print(text) \ No newline at end of file diff --git a/hw1/task_3.py b/hw1/task_3.py new file mode 100644 index 0000000..4c624b3 --- /dev/null +++ b/hw1/task_3.py @@ -0,0 +1,8 @@ +def f1(n): + return abs(int(float(n))) + + +if __name__ == "__main__": + # text = '0.23 0 -12 5 -7.11 2' + text = input() + print(*map(f1, text.split())) \ No newline at end of file diff --git a/hw1/task_4.py b/hw1/task_4.py new file mode 100644 index 0000000..7a9a10b --- /dev/null +++ b/hw1/task_4.py @@ -0,0 +1,8 @@ +def f1(n): + return float(n)**2 + + +if __name__ == "__main__": + # text = '2 -5 -12 0.33 7 2' + text = input() + print(sum(map(f1, text.split()))) \ No newline at end of file diff --git a/hw1/task_5.py b/hw1/task_5.py new file mode 100644 index 0000000..d05cb3f --- /dev/null +++ b/hw1/task_5.py @@ -0,0 +1,13 @@ +def cesar_scheme(text, num): + a_num = ord('a') + alph_num = ord('z') - a_num + 1 + s = '' + for i in text: + i_num = ord(i) + s += chr(((i_num - a_num + num) % alph_num) + a_num) + return s + + +if __name__ == "__main__": + text = input() + print(cesar_scheme(text, 7)) \ No newline at end of file diff --git a/hw2/decoder.py b/hw2/decoder.py new file mode 100644 index 0000000..8ad3801 --- /dev/null +++ b/hw2/decoder.py @@ -0,0 +1,68 @@ +import math +import sys + + +def index_to_bytes(value, bits): + n = bits // 8 + if bits % 8 != 0: + n += 1 + return value.to_bytes(n, 'little') + + +def num_of_bytes_to_read(dict_len): + bits = math.ceil( + math.log2(dict_len) + ) # кол-во бит для представления числа len(dictionary) в binary формате (чтобы уместилось) + bytes = bits // 8 # кол-во байт (получаем из кол-ва бит) + if bits % 8 != 0: + bytes += 1 + return bytes + + +def bytes_to_index(bytes_str): + index = int.from_bytes(bytes_str, + 'little') # переводим байт/байты в индекс + return index + + +if __name__ == '__main__': + if len(sys.argv) == 1 or len(sys.argv) >= 4: + print('Usage: python', sys.argv[0], 'input_file') + print( + 'На входе должно быть от 1 или 2 аргумента (не считая сам исполняемый файл .py)!' + ) + exit(-1) + + with open(sys.argv[1], 'rb') as file: + data = file.read() + dictionary = {i: (i, ) for i in range(256)} + sequence = list() + + outpath = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1] + '.dec' + with open(outpath, 'wb') as file: + i = 0 + while i < len(data): + n = len(dictionary) if i != 1 else len( + dictionary) + 1 # чтобы считать два байта + bits = math.ceil(math.log2(n)) + Bytes = math.ceil( + bits / 8 + ) # кол-во байт для считывания (в первый раз - 1, затем - 2 и т.д.) + ind = bytes_to_index( + data[i:i + + Bytes]) # индекс соответствующей байт. последовательности + if ind == len(dictionary): + temp = [] + temp.append(sequence[0]) + dec_out = sequence + temp + sequence.append(dec_out[0]) + else: + dec_out = list(dictionary[ind]) + sequence.append(dec_out[0]) + file.write(bytes(dec_out)) + if i == 0: + i += Bytes + continue + i += Bytes + dictionary[len(dictionary)] = tuple(sequence) + sequence = dec_out diff --git a/hw2/decoder_wrong.py b/hw2/decoder_wrong.py new file mode 100644 index 0000000..3625f1f --- /dev/null +++ b/hw2/decoder_wrong.py @@ -0,0 +1,61 @@ +import math +import sys + + +def index_to_bytes(value, bits): + n = bits // 8 + if bits % 8 != 0: + n += 1 + return value.to_bytes(n, 'little') + + +def num_of_bytes_to_read(dict_len): + bits = math.ceil( + math.log2(dict_len) + ) # кол-во бит для представления числа len(dictionary) в binary формате (чтобы уместилось) + bytes = bits // 8 # кол-во байт (получаем из кол-ва бит) + if bits % 8 != 0: + bytes += 1 + return bytes + + +def bytes_to_index(bytes_str): + index = int.from_bytes(bytes_str, + 'little') # переводим байт/байты в индекс + return index + + +if __name__ == '__main__': + if len(sys.argv) == 1 or len(sys.argv) >= 4: + print('Usage: python', sys.argv[0], 'input_file') + print( + 'На входе должно быть от 1 или 2 аргумента (не считая сам исполняемый файл .py)!' + ) + exit(-1) + + with open(sys.argv[1], 'rb') as file: + data = file.read() + dictionary = {i: (i, ) for i in range(256)} + sequence = list() + nseq = list() + + outpath = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1] + '.dec' + with open(outpath, 'wb') as file: + i = 0 + while i < len(data): + n = len(dictionary) + bits = math.ceil(math.log2(n)) + Bytes = math.ceil( + bits / 8 + ) # кол-во байт для считывания (в первый раз - 1, затем - 2 и т.д.) + ind = bytes_to_index( + data[i:i + + Bytes]) # индекс соответствующей байт. последовательности + if ind == len(dictionary): + nseq = sequence + sequence[-1:] + else: + nseq = sequence + list(dictionary[ind]) + file.write(bytes(nseq)) + dictionary[len(dictionary)] = tuple(sequence + nseq[-1:]) + i += Bytes + seq = nseq.copy() diff --git a/hw2/main.py b/hw2/main.py new file mode 100644 index 0000000..cfbf83f --- /dev/null +++ b/hw2/main.py @@ -0,0 +1,44 @@ +import math +import sys + + +def index_to_bytes(value, bits): + n = bits // 8 + if bits % 8 != 0: + n += 1 + return value.to_bytes(n, 'little') + + +if __name__ == '__main__': + if len(sys.argv) == 1: + print('Usage: python', sys.argv[0], 'input_file') + exit(-1) + with open(sys.argv[1], 'rb') as file: + data = file.read() + dictionary = {(i,): i for i in range(256)} + sequence = list() + + outpath = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1] + '.lzw' + with open(outpath, 'wb') as file: + for sym in data: + sequence.append(sym) + key = tuple(sequence) + if key in dictionary: + continue + + n = math.ceil( + math.log2(len(dictionary))) # кол-во бит для представления числа len(dictionary) в binary формате + index_value = dictionary[key[:-1]] + enc_value = index_to_bytes(index_value, n) + file.write(enc_value) + + value = len(dictionary) + dictionary[key] = value + # print(value, '->', key) + sequence = sequence[-1:] + + n = math.ceil( + math.log2(len(dictionary))) # кол-во бит для представления числа len(dictionary) в 2-ом формате + index_value = dictionary[tuple(sequence)] + enc_value = index_to_bytes(index_value, n) + file.write(enc_value) diff --git a/hw2/main.py.lzw b/hw2/main.py.lzw new file mode 100644 index 0000000..b779e0f Binary files /dev/null and b/hw2/main.py.lzw differ diff --git a/hw2/main.py.lzw.dec b/hw2/main.py.lzw.dec new file mode 100644 index 0000000..cfbf83f --- /dev/null +++ b/hw2/main.py.lzw.dec @@ -0,0 +1,44 @@ +import math +import sys + + +def index_to_bytes(value, bits): + n = bits // 8 + if bits % 8 != 0: + n += 1 + return value.to_bytes(n, 'little') + + +if __name__ == '__main__': + if len(sys.argv) == 1: + print('Usage: python', sys.argv[0], 'input_file') + exit(-1) + with open(sys.argv[1], 'rb') as file: + data = file.read() + dictionary = {(i,): i for i in range(256)} + sequence = list() + + outpath = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1] + '.lzw' + with open(outpath, 'wb') as file: + for sym in data: + sequence.append(sym) + key = tuple(sequence) + if key in dictionary: + continue + + n = math.ceil( + math.log2(len(dictionary))) # кол-во бит для представления числа len(dictionary) в binary формате + index_value = dictionary[key[:-1]] + enc_value = index_to_bytes(index_value, n) + file.write(enc_value) + + value = len(dictionary) + dictionary[key] = value + # print(value, '->', key) + sequence = sequence[-1:] + + n = math.ceil( + math.log2(len(dictionary))) # кол-во бит для представления числа len(dictionary) в 2-ом формате + index_value = dictionary[tuple(sequence)] + enc_value = index_to_bytes(index_value, n) + file.write(enc_value) diff --git a/hw2/test.txt b/hw2/test.txt new file mode 100644 index 0000000..67b6cce --- /dev/null +++ b/hw2/test.txt @@ -0,0 +1 @@ +banana_bandana \ No newline at end of file diff --git a/hw2/test1.txt b/hw2/test1.txt new file mode 100644 index 0000000..026d905 --- /dev/null +++ b/hw2/test1.txt @@ -0,0 +1 @@ +ababababababababa \ No newline at end of file diff --git a/hw2/test1.txt.lzw b/hw2/test1.txt.lzw new file mode 100644 index 0000000..975d02b Binary files /dev/null and b/hw2/test1.txt.lzw differ