From ffee22721a6243609419dc9163b05161f2c31fa8 Mon Sep 17 00:00:00 2001 From: Adam Meslaurov Date: Fri, 24 Mar 2023 00:16:54 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D0=97=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +++ task_1.py | 2 ++ task_2.py | 34 ++++++++++++++++++++++++++++++++++ task_3.py | 8 ++++++++ task_4.py | 8 ++++++++ task_5.py | 13 +++++++++++++ 6 files changed, 68 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 task_1.py create mode 100644 task_2.py create mode 100644 task_3.py create mode 100644 task_4.py create mode 100644 task_5.py 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/task_1.py b/task_1.py new file mode 100644 index 0000000..a6be8ad --- /dev/null +++ b/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/task_2.py b/task_2.py new file mode 100644 index 0000000..be22bcb --- /dev/null +++ b/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/task_3.py b/task_3.py new file mode 100644 index 0000000..4c624b3 --- /dev/null +++ b/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/task_4.py b/task_4.py new file mode 100644 index 0000000..7a9a10b --- /dev/null +++ b/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/task_5.py b/task_5.py new file mode 100644 index 0000000..d05cb3f --- /dev/null +++ b/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 From ae4720429b7dedd1dfade29ae4f14cc94ec721be Mon Sep 17 00:00:00 2001 From: Adam Meslaurov Date: Mon, 17 Apr 2023 23:09:36 +0300 Subject: [PATCH 2/3] folders for every homework --- task_1.py => hw1/task_1.py | 0 task_2.py => hw1/task_2.py | 0 task_3.py => hw1/task_3.py | 0 task_4.py => hw1/task_4.py | 0 task_5.py => hw1/task_5.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename task_1.py => hw1/task_1.py (100%) rename task_2.py => hw1/task_2.py (100%) rename task_3.py => hw1/task_3.py (100%) rename task_4.py => hw1/task_4.py (100%) rename task_5.py => hw1/task_5.py (100%) diff --git a/task_1.py b/hw1/task_1.py similarity index 100% rename from task_1.py rename to hw1/task_1.py diff --git a/task_2.py b/hw1/task_2.py similarity index 100% rename from task_2.py rename to hw1/task_2.py diff --git a/task_3.py b/hw1/task_3.py similarity index 100% rename from task_3.py rename to hw1/task_3.py diff --git a/task_4.py b/hw1/task_4.py similarity index 100% rename from task_4.py rename to hw1/task_4.py diff --git a/task_5.py b/hw1/task_5.py similarity index 100% rename from task_5.py rename to hw1/task_5.py From b05a1c29c5863db730679465630110baa5f45787 Mon Sep 17 00:00:00 2001 From: Adam Meslaurov Date: Wed, 19 Apr 2023 18:59:49 +0300 Subject: [PATCH 3/3] HW #2 --- .vscode/tasks.json | 21 +++++++++++++ hw2/decoder.py | 68 +++++++++++++++++++++++++++++++++++++++++++ hw2/decoder_wrong.py | 61 ++++++++++++++++++++++++++++++++++++++ hw2/main.py | 44 ++++++++++++++++++++++++++++ hw2/main.py.lzw | Bin 0 -> 1379 bytes hw2/main.py.lzw.dec | 44 ++++++++++++++++++++++++++++ hw2/test.txt | 1 + hw2/test1.txt | 1 + hw2/test1.txt.lzw | Bin 0 -> 15 bytes 9 files changed, 240 insertions(+) create mode 100644 .vscode/tasks.json create mode 100644 hw2/decoder.py create mode 100644 hw2/decoder_wrong.py create mode 100644 hw2/main.py create mode 100644 hw2/main.py.lzw create mode 100644 hw2/main.py.lzw.dec create mode 100644 hw2/test.txt create mode 100644 hw2/test1.txt create mode 100644 hw2/test1.txt.lzw 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/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 0000000000000000000000000000000000000000..b779e0fcbac8e78bf4fab538bc3a578b9eda0a72 GIT binary patch literal 1379 zcmWkteN5G57(M5_rl`w6DiJZSDRNN+#UR8{Yx#fa`dWs1k*h$!#p{(1U8O8%*3`-s znYEg>O3N>jQWRX7IV}?`SIxOHDg5F=QIHm_mGWHsVg?3!9M;x@Kofh!Y!R=FbOvt@{)+27Y=xpxJwta5G2 z`FxZcA%YU)HTqqFsVw3$M@9ZcP=>o$%36$KA*Z<}7x(!&9_5^d89tfAyS<;{{dC;V z5+;qGGD{L=%ws;|D8wC%@J>!-)C>j8vx*|7{EQ>d{FxliQCwiPjaJy;Y%^!F+;zNPqRt#mR-Cee#damZrF_h2akN@z6jP0!be}EW)!31QS>jm754nLa za-L5+&75cEDlByrLj@05>#N47MjB(7>FhaH@o~r1)^^msRhwy%IcxF0aoRAzl|HHU zY>Ju1Um5tfbw!LCv+AwZQH&Cl{@*Wp>_8k#G1-1Bax}ntXWn3vJxt(Zcaz5BcJLH; zSzobx%;H*}1Y?#LK+OR^O*KRH{`aTx6%NxZ}ypm}7-mJ*(NvE6*m*{5}d*VwLnyz6f!xaU4}d-Zx9^lbakiJjV^y^i*y zAMMzoeb}vjY;<(knN>dN^4jNkH4dl`d(f%f`rJ_uK2fhV_hT(iYc;;s9y7eHty-n) zcBxadxZ3^n<5{zO<}VK6OPx>`I@IC$j`6);$jKbwIlZGc-^i7k=lgnum7dxq^)f?O z73Fo6^RfoLJqHKivN8r_U*h~Xu<0~iK0p&#F%9_B1jLnDo=mt7iEU| zni#J0FEwbQ(b5CL9B$WUZs8;j3K=2KZrp<%T;YmIp@R#xT$viDl&TcBvLh_Vze3ok z!5phi&b^|dP|D5PqBr%HCWPO)O}XJc^KRD%+G&sW>Lc}{OQ+SXFVv&McHmPT(NP`K zakblvlRAh~>eqgsozZvtL1*=7ct(HfCw1D(V(ir}9m2OdZ=~OJ!46-viV-0v+!P{+ zhITvFq@?Z*vqQcW)@e)_9|nhPriE$_4@', 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 0000000000000000000000000000000000000000..975d02be2a061a80c28ef7468ee05b533102a9e1 GIT binary patch literal 15 WcmYdHVqjooVq|1wVPs}ZWB>pRKLIoV literal 0 HcmV?d00001