From 1907fc294e764c5065d327d80d5436f2753498f2 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Fri, 8 Jan 2021 11:21:50 +0000 Subject: [PATCH 1/5] Setting up GitHub Classroom Feedback From 1f293d6ad65bc1a718cabbe6e914ddec389a35cf Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sun, 10 Jan 2021 13:22:36 +0400 Subject: [PATCH 2/5] Task2 --- .github/Task2_encode_decode.py | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 .github/Task2_encode_decode.py diff --git a/.github/Task2_encode_decode.py b/.github/Task2_encode_decode.py new file mode 100644 index 0000000..6d785cd --- /dev/null +++ b/.github/Task2_encode_decode.py @@ -0,0 +1,60 @@ +# problem1 +# 1st version +def en_code(): + word = input("Please input a string: ") + bin_word = [] + for item in word: + bin_word.append(bin(ord(item))[2:]) + result = "".join(bin_word) + return result + + +print(en_code()) + + +# 2nd version +def en_code1(): + word = input("Please input a string: ") + return "".join([bin(ord(item))[2:] for item in word]) + + +print(en_code1()) +# problem 2 +# 1st version + + +def de_code_input(bin_type): + for item in bin_type: + if item not in ("0", "1"): + return False + return True + + +def de_code(): + stack_num = [] + word = [] + while True: + bin_type = input("Please input a binary code(it can contain only 0 or 1): ") + if de_code_input(bin_type) == True: + break + new_bin_type = [item for item in bin_type] + while len(new_bin_type) > 0: + while len(stack_num) < 8: + stack_num.append(new_bin_type.pop(0)) + word.append(chr(int("".join(stack_num), 2))) + stack_num.clear() + return "".join(word) + + +print(de_code()) +# 2nd version + + +def de_code2(): + while True: + bin_type = input("Please input a binary code(it can contain only 0 or 1): ") + if de_code_input(bin_type) == True: + break + return "".join([chr(int(item1, 2))for item1 in [bin_type[item:item+8] for item in range(0, len(bin_type), 8)]]) + +print(de_code2()) From 4c973353e3a44b155fd77890d50be7d849d87f90 Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sun, 10 Jan 2021 13:27:53 +0400 Subject: [PATCH 3/5] Task2 --- Task2_encode_decode.py | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Task2_encode_decode.py diff --git a/Task2_encode_decode.py b/Task2_encode_decode.py new file mode 100644 index 0000000..01c96e3 --- /dev/null +++ b/Task2_encode_decode.py @@ -0,0 +1,61 @@ +# problem1 +# 1st version +def en_code(): + word = input("Please input a string: ") + bin_word = [] + for item in word: + bin_word.append(bin(ord(item))[2:]) + result = "".join(bin_word) + return result + + +print(en_code()) + + +# 2nd version +def en_code1(): + word = input("Please input a string: ") + return "".join([bin(ord(item))[2:] for item in word]) + + +print(en_code1()) +# problem 2 +# 1st version + + +def de_code_input(bin_type): + for item in bin_type: + if item not in ("0", "1"): + return False + return True + + +def de_code(): + stack_num = [] + word = [] + while True: + bin_type = input("Please input a binary code(it can contain only 0 or 1): ") + if de_code_input(bin_type) == True: + break + new_bin_type = [item for item in bin_type] + while len(new_bin_type) > 0: + while len(stack_num) < 8: + stack_num.append(new_bin_type.pop(0)) + word.append(chr(int("".join(stack_num), 2))) + stack_num.clear() + return "".join(word) + + +print(de_code()) +# 2nd version + + +def de_code2(): + while True: + bin_type = input("Please input a binary code(it can contain only 0 or 1): ") + if de_code_input(bin_type) == True: + break + return "".join([chr(int(item1, 2))for item1 in [bin_type[item:item+8] for item in range(0, len(bin_type), 8)]]) + + +print(de_code2()) From 56ccb1318f4dcf20409b46e142c5be377de051a8 Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sun, 17 Jan 2021 14:00:45 +0400 Subject: [PATCH 4/5] Task2 changes --- .github/Task2_encode_decode.py | 60 ---------------------------------- Task2_encode_decode.py | 27 +++------------ result.py | 18 ++++++++++ 3 files changed, 22 insertions(+), 83 deletions(-) delete mode 100644 .github/Task2_encode_decode.py create mode 100644 result.py diff --git a/.github/Task2_encode_decode.py b/.github/Task2_encode_decode.py deleted file mode 100644 index 6d785cd..0000000 --- a/.github/Task2_encode_decode.py +++ /dev/null @@ -1,60 +0,0 @@ -# problem1 -# 1st version -def en_code(): - word = input("Please input a string: ") - bin_word = [] - for item in word: - bin_word.append(bin(ord(item))[2:]) - result = "".join(bin_word) - return result - - -print(en_code()) - - -# 2nd version -def en_code1(): - word = input("Please input a string: ") - return "".join([bin(ord(item))[2:] for item in word]) - - -print(en_code1()) -# problem 2 -# 1st version - - -def de_code_input(bin_type): - for item in bin_type: - if item not in ("0", "1"): - return False - return True - - -def de_code(): - stack_num = [] - word = [] - while True: - bin_type = input("Please input a binary code(it can contain only 0 or 1): ") - if de_code_input(bin_type) == True: - break - new_bin_type = [item for item in bin_type] - while len(new_bin_type) > 0: - while len(stack_num) < 8: - stack_num.append(new_bin_type.pop(0)) - word.append(chr(int("".join(stack_num), 2))) - stack_num.clear() - return "".join(word) - - -print(de_code()) -# 2nd version - - -def de_code2(): - while True: - bin_type = input("Please input a binary code(it can contain only 0 or 1): ") - if de_code_input(bin_type) == True: - break - return "".join([chr(int(item1, 2))for item1 in [bin_type[item:item+8] for item in range(0, len(bin_type), 8)]]) - -print(de_code2()) diff --git a/Task2_encode_decode.py b/Task2_encode_decode.py index 01c96e3..622715e 100644 --- a/Task2_encode_decode.py +++ b/Task2_encode_decode.py @@ -1,7 +1,6 @@ # problem1 # 1st version -def en_code(): - word = input("Please input a string: ") +def en_code(word): bin_word = [] for item in word: bin_word.append(bin(ord(item))[2:]) @@ -9,16 +8,10 @@ def en_code(): return result -print(en_code()) - - # 2nd version -def en_code1(): - word = input("Please input a string: ") +def en_code1(word): return "".join([bin(ord(item))[2:] for item in word]) - -print(en_code1()) # problem 2 # 1st version @@ -30,13 +23,9 @@ def de_code_input(bin_type): return True -def de_code(): +def de_code(bin_type): stack_num = [] word = [] - while True: - bin_type = input("Please input a binary code(it can contain only 0 or 1): ") - if de_code_input(bin_type) == True: - break new_bin_type = [item for item in bin_type] while len(new_bin_type) > 0: while len(stack_num) < 8: @@ -45,17 +34,9 @@ def de_code(): stack_num.clear() return "".join(word) - -print(de_code()) # 2nd version -def de_code2(): - while True: - bin_type = input("Please input a binary code(it can contain only 0 or 1): ") - if de_code_input(bin_type) == True: - break +def de_code2(bin_type): return "".join([chr(int(item1, 2))for item1 in [bin_type[item:item+8] for item in range(0, len(bin_type), 8)]]) - -print(de_code2()) diff --git a/result.py b/result.py new file mode 100644 index 0000000..00dc071 --- /dev/null +++ b/result.py @@ -0,0 +1,18 @@ +from Task2_encode_decode import * + + +def rep_dec_enc(): + print("Choose what you want to do:Encode or Decode? ") + answer = input("Type 1 or 2: ") + if answer == "1": + word = input("Please input a string: ") + print(f"The answer is: {en_code1(word)}") # or en_code + else: + while True: + bin_type = input("Please input a binary code(it can contain only 0 or 1): ") + if de_code_input(bin_type) == True: + break + print(f"The answer is: {de_code2(bin_type)}") # or de_code + + +rep_dec_enc() \ No newline at end of file From 4e1523a09db93539aef66c6569259d45e7534c6a Mon Sep 17 00:00:00 2001 From: Sonasah96 Date: Sat, 23 Jan 2021 19:09:12 +0400 Subject: [PATCH 5/5] Task2 changes after checking for me --- Task2_encode_decode.py | 9 +++++---- result.py | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Task2_encode_decode.py b/Task2_encode_decode.py index 622715e..102c9b5 100644 --- a/Task2_encode_decode.py +++ b/Task2_encode_decode.py @@ -3,20 +3,22 @@ def en_code(word): bin_word = [] for item in word: - bin_word.append(bin(ord(item))[2:]) + bin_word.append("{:08b}".format(ord(item))) result = "".join(bin_word) return result # 2nd version def en_code1(word): - return "".join([bin(ord(item))[2:] for item in word]) + return "".join("{:08b}".format(ord(item)) for item in word) # problem 2 # 1st version def de_code_input(bin_type): + if len(bin_type) % 8 != 0: + return False for item in bin_type: if item not in ("0", "1"): return False @@ -34,9 +36,8 @@ def de_code(bin_type): stack_num.clear() return "".join(word) -# 2nd version - +# 2nd version def de_code2(bin_type): return "".join([chr(int(item1, 2))for item1 in [bin_type[item:item+8] for item in range(0, len(bin_type), 8)]]) diff --git a/result.py b/result.py index 00dc071..67f4e36 100644 --- a/result.py +++ b/result.py @@ -15,4 +15,5 @@ def rep_dec_enc(): print(f"The answer is: {de_code2(bin_type)}") # or de_code -rep_dec_enc() \ No newline at end of file +if __name__ == '__main__': + rep_dec_enc() \ No newline at end of file