From 00c22a34ddc2918a1d71ff00e36c341428067c05 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Sun, 16 May 2021 13:18:42 +0300 Subject: [PATCH 01/22] Added censor function --- strings/censor.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 strings/censor.py diff --git a/strings/censor.py b/strings/censor.py new file mode 100644 index 000000000000..e69de29bb2d1 From 419434471455e53287360d75fdd85fd949d24066 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Mon, 17 May 2021 17:35:32 +0300 Subject: [PATCH 02/22] Added censor code --- strings/censor.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/strings/censor.py b/strings/censor.py index e69de29bb2d1..cbdd948a9033 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -0,0 +1,11 @@ +txt = input("Please enter the text from which you would like to censor a certain word: \n") +word = input("Please enter the word you would like to be censored from the text: \n") +txt_lst = txt.split() +for word1 in txt_lst: + if word1 == word: + txt_lst[txt_lst.index(word1)] = "*" * len(word1) +txt = "" +for word1 in txt_lst: + txt += word1 + " " +txt = txt[:-1] +print(txt) \ No newline at end of file From e7d4102bbb5a90ea144f9a5880a92722dc5cd20e Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Mon, 17 May 2021 17:40:48 +0300 Subject: [PATCH 03/22] Added comments to the code --- strings/censor.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/strings/censor.py b/strings/censor.py index cbdd948a9033..5c629cf79a18 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -1,11 +1,15 @@ txt = input("Please enter the text from which you would like to censor a certain word: \n") word = input("Please enter the word you would like to be censored from the text: \n") txt_lst = txt.split() + +#here we loop through a list which consists of words from the text we should censor for word1 in txt_lst: if word1 == word: txt_lst[txt_lst.index(word1)] = "*" * len(word1) txt = "" + +#here we combine all of the split words in txt_lst into one string for word1 in txt_lst: txt += word1 + " " txt = txt[:-1] -print(txt) \ No newline at end of file +print(txt) From 93bd4acf6b7eeb2a856319837c3f854f0a574a11 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Sat, 22 May 2021 13:34:28 +0300 Subject: [PATCH 04/22] modified censor function --- strings/censor.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/strings/censor.py b/strings/censor.py index 5c629cf79a18..607dcd9a53d7 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -1,15 +1,14 @@ -txt = input("Please enter the text from which you would like to censor a certain word: \n") -word = input("Please enter the word you would like to be censored from the text: \n") -txt_lst = txt.split() +def censor(txt, word): + txt_lst = txt.split() + #here we loop through a list which consists of words from the text we should censor + for word1 in txt_lst: + if word1 == word: + txt_lst[txt_lst.index(word1)] = "*" * len(word1) + txt = "" + #here we combine all of the split words in txt_lst into one string + for word1 in txt_lst: + txt += word1 + " " + txt = txt[:-1] + print(txt) -#here we loop through a list which consists of words from the text we should censor -for word1 in txt_lst: - if word1 == word: - txt_lst[txt_lst.index(word1)] = "*" * len(word1) -txt = "" - -#here we combine all of the split words in txt_lst into one string -for word1 in txt_lst: - txt += word1 + " " -txt = txt[:-1] -print(txt) +censor("one two three", "one") \ No newline at end of file From b7142782ae3376c5e66f2adf025d8e4a57e3f0d7 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Sun, 1 Aug 2021 23:05:45 +0300 Subject: [PATCH 05/22] added decrypt function --- strings/decrypt.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 strings/decrypt.py diff --git a/strings/decrypt.py b/strings/decrypt.py new file mode 100644 index 000000000000..478226af5f17 --- /dev/null +++ b/strings/decrypt.py @@ -0,0 +1,27 @@ +def decrypt(txt, symbol): + #here we split the string we need to decypher into a list + txt_lst = txt.split() + res_str = "" + #we create a for loop which loops through the newly created list + for word1 in txt_lst: + #here we convert the current word we got through the loop into a list of charachters + word1_lst = list(word1) + #we loop through each charachter in the newly created list of charachters form the word we are looping throuh + for char1 in word1_lst: + #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it + if char1 == symbol: + #we find the charachters index from the word + indx = word1.index(char1) + #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. + word1_lst[indx] = "" + res_wrd = "" + #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string + for char1 in word1_lst: + res_wrd += char1 + #here we add the newly created, decyphered word to a new string which will contain our full result + res_str += res_wrd + " " + res_str = res_str[:-1] + #and here we print out the final result + print(res_str) + +decrypt("o$ne t$wo three$", "$") \ No newline at end of file From c879c7f589a41cf9a669fda94a01c38afd04484b Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Wed, 4 Aug 2021 14:24:38 +0300 Subject: [PATCH 06/22] added cypher and decypher functions, deleted censor and decrypt functions --- strings/cypher_txt.py | 51 +++++++++++++++++++++++++++++++++++++++++ strings/decypher_txt.py | 49 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 strings/cypher_txt.py create mode 100644 strings/decypher_txt.py diff --git a/strings/cypher_txt.py b/strings/cypher_txt.py new file mode 100644 index 000000000000..45abc0896e94 --- /dev/null +++ b/strings/cypher_txt.py @@ -0,0 +1,51 @@ +def cypher_mesage(txt): + alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] + + info = [] + for char in txt: + if (char.isalpha() == False): + info.append(2) + elif char.isupper() == True: + info.append(1) + else: + info.append(0) + + indx_lst=[] + for char in txt: + if (char.isalpha() == False): + indx_lst+=char + else: + if char.isupper() == True: + indx_lst.append(alphabet_upper.index(char)) + else: + indx_lst.append(alphabet_lower.index(char)) + + res_indx=[] + for char in indx_lst: + if type(char) == int: + if int(char) <= 2: + res_indx.append(26 + (int(char) - 3)) + else: + res_indx.append(int(char) - 3) + else: + res_indx+=char + + + res_str = "" + count = 0 + for char in res_indx: + if type(char) != int: + res_str += char + else: + if info[count] == 0: + res_str += str(alphabet_lower[int(char)]) + elif info[count] == 1: + res_str += str(alphabet_upper[int(char)]) + count+=1 + + print(res_str) + + + +code_mesage("xXyzabB!1234") \ No newline at end of file diff --git a/strings/decypher_txt.py b/strings/decypher_txt.py new file mode 100644 index 000000000000..509f9fc6963c --- /dev/null +++ b/strings/decypher_txt.py @@ -0,0 +1,49 @@ +def decode_mesage(txt): + alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] + + info = [] + for char in txt: + if (char.isalpha() == False): + info.append(2) + elif char.isupper() == True: + info.append(1) + else: + info.append(0) + + indx_lst=[] + for char in txt: + if (char.isalpha() == False): + indx_lst+=char + else: + if char.isupper() == True: + indx_lst.append(alphabet_upper.index(char)) + else: + indx_lst.append(alphabet_lower.index(char)) + + res_indx=[] + for char in indx_lst: + if type(char) == int: + if int(char) >= 23: + res_indx.append((int(char) - 23)) + else: + res_indx.append(int(char) + 3) + else: + res_indx+=char + + + res_str = "" + count = 0 + for char in res_indx: + if type(char) != int: + res_str += char + else: + if info[count] == 0: + res_str += str(alphabet_lower[int(char)]) + elif info[count] == 1: + res_str += str(alphabet_upper[int(char)]) + count+=1 + + print(res_str) + +print(decode_mesage("xyzab!1234")) \ No newline at end of file From e60dd6f96a3ec55e4ba148e316859a8d6b829658 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 4 Aug 2021 14:36:05 +0300 Subject: [PATCH 07/22] Deleted decrypt.py --- strings/decrypt.py | 27 --------------------------- 1 file changed, 27 deletions(-) delete mode 100644 strings/decrypt.py diff --git a/strings/decrypt.py b/strings/decrypt.py deleted file mode 100644 index 478226af5f17..000000000000 --- a/strings/decrypt.py +++ /dev/null @@ -1,27 +0,0 @@ -def decrypt(txt, symbol): - #here we split the string we need to decypher into a list - txt_lst = txt.split() - res_str = "" - #we create a for loop which loops through the newly created list - for word1 in txt_lst: - #here we convert the current word we got through the loop into a list of charachters - word1_lst = list(word1) - #we loop through each charachter in the newly created list of charachters form the word we are looping throuh - for char1 in word1_lst: - #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it - if char1 == symbol: - #we find the charachters index from the word - indx = word1.index(char1) - #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. - word1_lst[indx] = "" - res_wrd = "" - #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string - for char1 in word1_lst: - res_wrd += char1 - #here we add the newly created, decyphered word to a new string which will contain our full result - res_str += res_wrd + " " - res_str = res_str[:-1] - #and here we print out the final result - print(res_str) - -decrypt("o$ne t$wo three$", "$") \ No newline at end of file From b3e0cb8b88617b4e62f6993ce24b62231ef1817f Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 4 Aug 2021 14:36:45 +0300 Subject: [PATCH 08/22] Deleted censor.py --- strings/censor.py | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 strings/censor.py diff --git a/strings/censor.py b/strings/censor.py deleted file mode 100644 index 607dcd9a53d7..000000000000 --- a/strings/censor.py +++ /dev/null @@ -1,14 +0,0 @@ -def censor(txt, word): - txt_lst = txt.split() - #here we loop through a list which consists of words from the text we should censor - for word1 in txt_lst: - if word1 == word: - txt_lst[txt_lst.index(word1)] = "*" * len(word1) - txt = "" - #here we combine all of the split words in txt_lst into one string - for word1 in txt_lst: - txt += word1 + " " - txt = txt[:-1] - print(txt) - -censor("one two three", "one") \ No newline at end of file From 947f7d84c083e0ae941977c741d332e2b73d74c3 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Wed, 4 Aug 2021 14:44:20 +0300 Subject: [PATCH 09/22] edited the crypt and decrypt files --- strings/cypher_txt.py | 6 +----- strings/decypher_txt.py | 4 +--- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/strings/cypher_txt.py b/strings/cypher_txt.py index 45abc0896e94..338d2ec33f43 100644 --- a/strings/cypher_txt.py +++ b/strings/cypher_txt.py @@ -44,8 +44,4 @@ def cypher_mesage(txt): res_str += str(alphabet_upper[int(char)]) count+=1 - print(res_str) - - - -code_mesage("xXyzabB!1234") \ No newline at end of file + print(res_str) \ No newline at end of file diff --git a/strings/decypher_txt.py b/strings/decypher_txt.py index 509f9fc6963c..2f13ddd9a7d4 100644 --- a/strings/decypher_txt.py +++ b/strings/decypher_txt.py @@ -44,6 +44,4 @@ def decode_mesage(txt): res_str += str(alphabet_upper[int(char)]) count+=1 - print(res_str) - -print(decode_mesage("xyzab!1234")) \ No newline at end of file + print(res_str) \ No newline at end of file From daf21ad821d5462d47160c4efd6ca1ce572c772b Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 4 Aug 2021 14:55:21 +0300 Subject: [PATCH 10/22] Update cypher_txt.py --- strings/cypher_txt.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/strings/cypher_txt.py b/strings/cypher_txt.py index 45abc0896e94..5aaeb6d0994d 100644 --- a/strings/cypher_txt.py +++ b/strings/cypher_txt.py @@ -45,7 +45,3 @@ def cypher_mesage(txt): count+=1 print(res_str) - - - -code_mesage("xXyzabB!1234") \ No newline at end of file From d3d8b29926119eb2a8b6eee5ce8dbef7f16cd64b Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 4 Aug 2021 15:18:15 +0300 Subject: [PATCH 11/22] Remove the endline in cypher.py --- strings/cypher_txt.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/strings/cypher_txt.py b/strings/cypher_txt.py index 5aaeb6d0994d..3c23571f906c 100644 --- a/strings/cypher_txt.py +++ b/strings/cypher_txt.py @@ -43,5 +43,3 @@ def cypher_mesage(txt): elif info[count] == 1: res_str += str(alphabet_upper[int(char)]) count+=1 - - print(res_str) From 752c197dba6f158115372cbbe967fd8353ff026f Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 4 Aug 2021 15:19:33 +0300 Subject: [PATCH 12/22] Removed the print at the end of decypher.py --- strings/decypher_txt.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/strings/decypher_txt.py b/strings/decypher_txt.py index 509f9fc6963c..aa97ac834ac0 100644 --- a/strings/decypher_txt.py +++ b/strings/decypher_txt.py @@ -43,7 +43,3 @@ def decode_mesage(txt): elif info[count] == 1: res_str += str(alphabet_upper[int(char)]) count+=1 - - print(res_str) - -print(decode_mesage("xyzab!1234")) \ No newline at end of file From f72431b07dae6a172e84d6041547858c17a75f8b Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Wed, 18 Aug 2021 18:26:33 +0300 Subject: [PATCH 13/22] added 4 new algorithms --- strings/censor.py | 14 +++++++++++ strings/code_message.py | 51 +++++++++++++++++++++++++++++++++++++++ strings/decode_message.py | 49 +++++++++++++++++++++++++++++++++++++ strings/decrypt.py | 27 +++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 strings/censor.py create mode 100644 strings/code_message.py create mode 100644 strings/decode_message.py create mode 100644 strings/decrypt.py diff --git a/strings/censor.py b/strings/censor.py new file mode 100644 index 000000000000..2481dbc9b7a6 --- /dev/null +++ b/strings/censor.py @@ -0,0 +1,14 @@ +def censor(txt, word) -> str: + txt_lst = txt.split() + #here we loop through a list which consists of words from the text we should censor + for index, word1 in enumerate(txt_lst): + if word1 == word: + txt_lst[index] = "*" * len(word1) + txt = "" + #here we combine all of the split words in txt_lst into one string + for word1 in txt_lst: + txt += word1 + " " + txt = txt[:-1] + print(txt) + +censor("one two three", "one") \ No newline at end of file diff --git a/strings/code_message.py b/strings/code_message.py new file mode 100644 index 000000000000..340c922f9764 --- /dev/null +++ b/strings/code_message.py @@ -0,0 +1,51 @@ +def code_mesage(txt) -> str: + alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] + + info = [] + for char in txt: + if (char.isalpha() == False): + info.append(2) + elif char.isupper() == True: + info.append(1) + else: + info.append(0) + + indx_lst=[] + for char in txt: + if (char.isalpha() == False): + indx_lst+=char + else: + if char.isupper() == True: + indx_lst.append(alphabet_upper.index(char)) + else: + indx_lst.append(alphabet_lower.index(char)) + + res_indx=[] + for char in indx_lst: + if type(char) == int: + if int(char) <= 2: + res_indx.append(26 + (int(char) - 3)) + else: + res_indx.append(int(char) - 3) + else: + res_indx+=char + + + res_str = "" + count = 0 + for char in res_indx: + if type(char) != int: + res_str += char + else: + if info[count] == 0: + res_str += str(alphabet_lower[int(char)]) + elif info[count] == 1: + res_str += str(alphabet_upper[int(char)]) + count+=1 + + print(res_str) + + + +code_mesage("abcde!1234") \ No newline at end of file diff --git a/strings/decode_message.py b/strings/decode_message.py new file mode 100644 index 000000000000..c229dd6a2882 --- /dev/null +++ b/strings/decode_message.py @@ -0,0 +1,49 @@ +def decode_mesage(txt) -> str: + alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] + + info = [] + for char in txt: + if (char.isalpha() == False): + info.append(2) + elif char.isupper() == True: + info.append(1) + else: + info.append(0) + + indx_lst=[] + for char in txt: + if (char.isalpha() == False): + indx_lst+=char + else: + if char.isupper() == True: + indx_lst.append(alphabet_upper.index(char)) + else: + indx_lst.append(alphabet_lower.index(char)) + + res_indx=[] + for char in indx_lst: + if type(char) == int: + if int(char) >= 23: + res_indx.append((int(char) - 23)) + else: + res_indx.append(int(char) + 3) + else: + res_indx+=char + + + res_str = "" + count = 0 + for char in res_indx: + if type(char) != int: + res_str += char + else: + if info[count] == 0: + res_str += str(alphabet_lower[int(char)]) + elif info[count] == 1: + res_str += str(alphabet_upper[int(char)]) + count+=1 + + print(res_str) + +print(decode_mesage("xyzab!1234")) \ No newline at end of file diff --git a/strings/decrypt.py b/strings/decrypt.py new file mode 100644 index 000000000000..3cbacbce0284 --- /dev/null +++ b/strings/decrypt.py @@ -0,0 +1,27 @@ +def decrypt(txt, symbol) -> str: + #here we split the string we need to decypher into a list + txt_lst = txt.split() + res_str = "" + #we create a for loop which loops through the newly created list + for word1 in txt_lst: + #here we convert the current word we got through the loop into a list of charachters + word1_lst = list(word1) + #we loop through each charachter in the newly created list of charachters form the word we are looping throuh + for char1 in word1_lst: + #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it + if char1 == symbol: + #we find the charachters index from the word + indx = word1.index(char1) + #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. + word1_lst[indx] = "" + res_wrd = "" + #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string + for char1 in word1_lst: + res_wrd += char1 + #here we add the newly created, decyphered word to a new string which will contain our full result + res_str += res_wrd + " " + res_str = res_str[:-1] + #and here we print out the final result + print(res_str) + +decrypt("o$ne t$wo three$", "$") \ No newline at end of file From 4c891f484ed772a554d00fdaa1aac0443dfc4a31 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Wed, 25 Aug 2021 14:44:17 +0300 Subject: [PATCH 14/22] added tests to the four files --- strings/censor.py | 36 ++++++++++++++++++++++++------------ strings/code_message.py | 15 ++++++++++++--- strings/decode_message.py | 13 ++++++++++++- strings/decrypt.py | 13 ++++++++++++- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/strings/censor.py b/strings/censor.py index 2481dbc9b7a6..de3c0b076c8b 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -1,14 +1,26 @@ def censor(txt, word) -> str: - txt_lst = txt.split() - #here we loop through a list which consists of words from the text we should censor - for index, word1 in enumerate(txt_lst): - if word1 == word: - txt_lst[index] = "*" * len(word1) - txt = "" - #here we combine all of the split words in txt_lst into one string - for word1 in txt_lst: - txt += word1 + " " - txt = txt[:-1] - print(txt) + """ + >>> censor("one two three", "one") + *** two three + >>> censor("email, password", "password") + email, ******** + >>> censor("pin: 2536", "2536") + pin: **** -censor("one two three", "one") \ No newline at end of file + """ + + txt_lst = txt.split() + #here we loop through a list which consists of words from the text we should censor + for index, word1 in enumerate(txt_lst): + if word1 == word: + txt_lst[index] = "*" * len(word1) + txt = "" + #here we combine all of the split words in txt_lst into one string + for word1 in txt_lst: + txt += word1 + " " + txt = txt[:-1] + print(txt) + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/strings/code_message.py b/strings/code_message.py index 340c922f9764..ca40530cda24 100644 --- a/strings/code_message.py +++ b/strings/code_message.py @@ -1,4 +1,13 @@ def code_mesage(txt) -> str: + """ + >>> code_mesage("Def") + Abc + >>> code_mesage("bcF") + yzC + >>> code_mesage("qva") + nsx + + """ alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] @@ -46,6 +55,6 @@ def code_mesage(txt) -> str: print(res_str) - - -code_mesage("abcde!1234") \ No newline at end of file +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/strings/decode_message.py b/strings/decode_message.py index c229dd6a2882..028bd2ff553e 100644 --- a/strings/decode_message.py +++ b/strings/decode_message.py @@ -1,4 +1,13 @@ def decode_mesage(txt) -> str: + """ + >>> decode_mesage("eFg") + hIj + >>> decode_mesage("yzC") + bcF + >>> decode_mesage("nsx") + qva + + """ alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] @@ -46,4 +55,6 @@ def decode_mesage(txt) -> str: print(res_str) -print(decode_mesage("xyzab!1234")) \ No newline at end of file +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file diff --git a/strings/decrypt.py b/strings/decrypt.py index 3cbacbce0284..6d824653e790 100644 --- a/strings/decrypt.py +++ b/strings/decrypt.py @@ -1,4 +1,13 @@ def decrypt(txt, symbol) -> str: + """ + >>> decrypt("o$ne", "$") + one + >>> decrypt("dec@rypt", "@") + decrypt + >>> decrypt("p#ython cod#e", "#") + python code + + """ #here we split the string we need to decypher into a list txt_lst = txt.split() res_str = "" @@ -24,4 +33,6 @@ def decrypt(txt, symbol) -> str: #and here we print out the final result print(res_str) -decrypt("o$ne t$wo three$", "$") \ No newline at end of file +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From c600b10545bc6a070198950f614cef948a5fde44 Mon Sep 17 00:00:00 2001 From: yauchuscom Date: Wed, 25 Aug 2021 14:54:01 +0300 Subject: [PATCH 15/22] added type hints for the function variables --- strings/censor.py | 2 +- strings/code_message.py | 2 +- strings/decode_message.py | 2 +- strings/decrypt.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/strings/censor.py b/strings/censor.py index de3c0b076c8b..fe10a08c5bc4 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -1,4 +1,4 @@ -def censor(txt, word) -> str: +def censor(txt: str, word: str) -> str: """ >>> censor("one two three", "one") *** two three diff --git a/strings/code_message.py b/strings/code_message.py index ca40530cda24..cb8603329115 100644 --- a/strings/code_message.py +++ b/strings/code_message.py @@ -1,4 +1,4 @@ -def code_mesage(txt) -> str: +def code_mesage(txt: str) -> str: """ >>> code_mesage("Def") Abc diff --git a/strings/decode_message.py b/strings/decode_message.py index 028bd2ff553e..9c100f7c2ec7 100644 --- a/strings/decode_message.py +++ b/strings/decode_message.py @@ -1,4 +1,4 @@ -def decode_mesage(txt) -> str: +def decode_mesage(txt: str) -> str: """ >>> decode_mesage("eFg") hIj diff --git a/strings/decrypt.py b/strings/decrypt.py index 6d824653e790..3535d2443a2c 100644 --- a/strings/decrypt.py +++ b/strings/decrypt.py @@ -1,4 +1,4 @@ -def decrypt(txt, symbol) -> str: +def decrypt(txt: str, symbol: str) -> str: """ >>> decrypt("o$ne", "$") one From 7a909e24a5bb5ba2d9e3a8582cad346613b9b1c3 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 25 Dec 2021 14:49:16 +0200 Subject: [PATCH 16/22] Deleted decode message --- strings/decode_message.py | 60 --------------------------------------- 1 file changed, 60 deletions(-) delete mode 100644 strings/decode_message.py diff --git a/strings/decode_message.py b/strings/decode_message.py deleted file mode 100644 index 9c100f7c2ec7..000000000000 --- a/strings/decode_message.py +++ /dev/null @@ -1,60 +0,0 @@ -def decode_mesage(txt: str) -> str: - """ - >>> decode_mesage("eFg") - hIj - >>> decode_mesage("yzC") - bcF - >>> decode_mesage("nsx") - qva - - """ - alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] - alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] - - info = [] - for char in txt: - if (char.isalpha() == False): - info.append(2) - elif char.isupper() == True: - info.append(1) - else: - info.append(0) - - indx_lst=[] - for char in txt: - if (char.isalpha() == False): - indx_lst+=char - else: - if char.isupper() == True: - indx_lst.append(alphabet_upper.index(char)) - else: - indx_lst.append(alphabet_lower.index(char)) - - res_indx=[] - for char in indx_lst: - if type(char) == int: - if int(char) >= 23: - res_indx.append((int(char) - 23)) - else: - res_indx.append(int(char) + 3) - else: - res_indx+=char - - - res_str = "" - count = 0 - for char in res_indx: - if type(char) != int: - res_str += char - else: - if info[count] == 0: - res_str += str(alphabet_lower[int(char)]) - elif info[count] == 1: - res_str += str(alphabet_upper[int(char)]) - count+=1 - - print(res_str) - -if __name__ == "__main__": - import doctest - doctest.testmod() \ No newline at end of file From f5b5411a2126df879ed757bbd2944f54a41ccc23 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 25 Dec 2021 14:49:38 +0200 Subject: [PATCH 17/22] Deleted code message --- strings/code_message.py | 60 ----------------------------------------- 1 file changed, 60 deletions(-) delete mode 100644 strings/code_message.py diff --git a/strings/code_message.py b/strings/code_message.py deleted file mode 100644 index cb8603329115..000000000000 --- a/strings/code_message.py +++ /dev/null @@ -1,60 +0,0 @@ -def code_mesage(txt: str) -> str: - """ - >>> code_mesage("Def") - Abc - >>> code_mesage("bcF") - yzC - >>> code_mesage("qva") - nsx - - """ - alphabet_lower = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] - alphabet_upper = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', "X", 'Y', 'Z'] - - info = [] - for char in txt: - if (char.isalpha() == False): - info.append(2) - elif char.isupper() == True: - info.append(1) - else: - info.append(0) - - indx_lst=[] - for char in txt: - if (char.isalpha() == False): - indx_lst+=char - else: - if char.isupper() == True: - indx_lst.append(alphabet_upper.index(char)) - else: - indx_lst.append(alphabet_lower.index(char)) - - res_indx=[] - for char in indx_lst: - if type(char) == int: - if int(char) <= 2: - res_indx.append(26 + (int(char) - 3)) - else: - res_indx.append(int(char) - 3) - else: - res_indx+=char - - - res_str = "" - count = 0 - for char in res_indx: - if type(char) != int: - res_str += char - else: - if info[count] == 0: - res_str += str(alphabet_lower[int(char)]) - elif info[count] == 1: - res_str += str(alphabet_upper[int(char)]) - count+=1 - - print(res_str) - -if __name__ == "__main__": - import doctest - doctest.testmod() \ No newline at end of file From bc8388c9d61f9d495fef5352fdb63b41cae1377b Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 29 Jan 2022 14:56:33 +0200 Subject: [PATCH 18/22] Welford average algorithm --- maths/average_welford.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 maths/average_welford.py diff --git a/maths/average_welford.py b/maths/average_welford.py new file mode 100644 index 000000000000..2fe94116d9a4 --- /dev/null +++ b/maths/average_welford.py @@ -0,0 +1,21 @@ +def average_welford(values): + """ + >>> average_welford([1, 2, 3, 4, 5]) + 3.0 + >>> average_welford([1.2329435, 2.209462409, 3.230925, 4.47626462, 5.2938759204]) + 3.2886942898799996 + >>> average_welford([-57386462.2329435, 2246262.209462409, 4632463.230925, 856737354.47626462, -243664265.2938759204]) + 112513070.4779665 + """ + avg = 0.0 + """while looping through the list, + we calculate the average of the + current element and the average + of the elements before it""" + for index in range(0, len(values)): + avg += (values[index]-avg)/(index+1) + return avg + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 9be479e9ac273131d47da5602f83cfb8e41babc3 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:31:38 +0200 Subject: [PATCH 19/22] added average welford algorithm --- maths/average_welford.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/average_welford.py b/maths/average_welford.py index 2fe94116d9a4..2aafb18ab936 100644 --- a/maths/average_welford.py +++ b/maths/average_welford.py @@ -1,4 +1,4 @@ -def average_welford(values): +def average_welford(values: list) -> float: """ >>> average_welford([1, 2, 3, 4, 5]) 3.0 From 5ebf7851992376bb26063e11e5bd4b1923aeab8f Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 26 Mar 2022 14:53:17 +0200 Subject: [PATCH 20/22] indentation added --- maths/average_welford.py | 32 ++++++++++---------- strings/decrypt.py | 64 ++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/maths/average_welford.py b/maths/average_welford.py index 2aafb18ab936..522a34adfc06 100644 --- a/maths/average_welford.py +++ b/maths/average_welford.py @@ -1,20 +1,20 @@ def average_welford(values: list) -> float: - """ - >>> average_welford([1, 2, 3, 4, 5]) - 3.0 - >>> average_welford([1.2329435, 2.209462409, 3.230925, 4.47626462, 5.2938759204]) - 3.2886942898799996 - >>> average_welford([-57386462.2329435, 2246262.209462409, 4632463.230925, 856737354.47626462, -243664265.2938759204]) - 112513070.4779665 - """ - avg = 0.0 - """while looping through the list, - we calculate the average of the - current element and the average - of the elements before it""" - for index in range(0, len(values)): - avg += (values[index]-avg)/(index+1) - return avg + """ + >>> average_welford([1, 2, 3, 4, 5]) + 3.0 + >>> average_welford([1.2329435, 2.209462409, 3.230925, 4.47626462, 5.2938759204]) + 3.2886942898799996 + >>> average_welford([-57386462.2329435, 2246262.209462409, 4632463.230925, 856737354.47626462, -243664265.2938759204]) + 112513070.4779665 + """ + avg = 0.0 + """while looping through the list, + we calculate the average of the + current element and the average + of the elements before it""" + for index in range(0, len(values)): + avg += (values[index]-avg)/(index+1) + return avg if __name__ == "__main__": import doctest diff --git a/strings/decrypt.py b/strings/decrypt.py index 3535d2443a2c..3e4893468802 100644 --- a/strings/decrypt.py +++ b/strings/decrypt.py @@ -1,37 +1,37 @@ def decrypt(txt: str, symbol: str) -> str: - """ - >>> decrypt("o$ne", "$") - one - >>> decrypt("dec@rypt", "@") - decrypt - >>> decrypt("p#ython cod#e", "#") - python code + """ + >>> decrypt("o$ne", "$") + one + >>> decrypt("dec@rypt", "@") + decrypt + >>> decrypt("p#ython cod#e", "#") + python code - """ - #here we split the string we need to decypher into a list - txt_lst = txt.split() - res_str = "" - #we create a for loop which loops through the newly created list - for word1 in txt_lst: - #here we convert the current word we got through the loop into a list of charachters - word1_lst = list(word1) - #we loop through each charachter in the newly created list of charachters form the word we are looping throuh - for char1 in word1_lst: - #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it - if char1 == symbol: - #we find the charachters index from the word - indx = word1.index(char1) - #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. - word1_lst[indx] = "" - res_wrd = "" - #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string - for char1 in word1_lst: - res_wrd += char1 - #here we add the newly created, decyphered word to a new string which will contain our full result - res_str += res_wrd + " " - res_str = res_str[:-1] - #and here we print out the final result - print(res_str) + """ + #here we split the string we need to decypher into a list + txt_lst = txt.split() + res_str = "" + #we create a for loop which loops through the newly created list + for word1 in txt_lst: + #here we convert the current word we got through the loop into a list of charachters + word1_lst = list(word1) + #we loop through each charachter in the newly created list of charachters form the word we are looping throuh + for char1 in word1_lst: + #we set a condition, where if the symbol the loop is currently on corresponds to the symbol we need to remove, we stop on it and examine it + if char1 == symbol: + #we find the charachters index from the word + indx = word1.index(char1) + #we remove the list (the list which contains every element and characahter from the word we are looping throuh) element with the same index from the list. + word1_lst[indx] = "" + res_wrd = "" + #we loop throuh the list containing all the charachters from the word, and append those charachters to a new string + for char1 in word1_lst: + res_wrd += char1 + #here we add the newly created, decyphered word to a new string which will contain our full result + res_str += res_wrd + " " + res_str = res_str[:-1] + #and here we print out the final result + print(res_str) if __name__ == "__main__": import doctest From 05706922d7c033f935d5bb90e97970409a70f1c5 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 26 Mar 2022 15:01:34 +0200 Subject: [PATCH 21/22] switched print to return in the end --- strings/censor.py | 2 +- strings/decrypt.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/censor.py b/strings/censor.py index fe10a08c5bc4..935283f5c8ef 100644 --- a/strings/censor.py +++ b/strings/censor.py @@ -19,7 +19,7 @@ def censor(txt: str, word: str) -> str: for word1 in txt_lst: txt += word1 + " " txt = txt[:-1] - print(txt) + return txt if __name__ == "__main__": import doctest diff --git a/strings/decrypt.py b/strings/decrypt.py index 3e4893468802..b5fcd1b83122 100644 --- a/strings/decrypt.py +++ b/strings/decrypt.py @@ -30,8 +30,8 @@ def decrypt(txt: str, symbol: str) -> str: #here we add the newly created, decyphered word to a new string which will contain our full result res_str += res_wrd + " " res_str = res_str[:-1] - #and here we print out the final result - print(res_str) + #and here we return out the final result + return res_str if __name__ == "__main__": import doctest From b5891778283a0999c6e080b56697ecdb377cb6ad Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 7 May 2022 14:32:32 +0300 Subject: [PATCH 22/22] added digital_root function --- maths/sum_of_digits.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/maths/sum_of_digits.py b/maths/sum_of_digits.py index 64da00d4634c..07ae94075240 100644 --- a/maths/sum_of_digits.py +++ b/maths/sum_of_digits.py @@ -54,10 +54,25 @@ def sum_of_digits_compact(n: int) -> int: """ return sum(int(c) for c in str(abs(n))) +def digital_root(n: int) -> int: + """ + finding the digital root of n + https://en.wikipedia.org/wiki/Digital_root + + >>> digital_root(394328) + 2 + >>> digital_root(-394328) + "Input wasn't valid" + >>> digital_root(123) + 6 + >>> digital_root(0) + 0 + """ + return n%9 or n and 9 if n >= 0 else "Input wasn't valid" def benchmark() -> None: """ - Benchmark code for comparing 3 functions, + Benchmark code for comparing 4 functions, with 3 different length int values. """ print("\nFor small_num = ", small_num, ":") @@ -85,6 +100,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.small_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\tans =", + digital_root(small_num), + "\ttime =", + timeit("z.digital_root(z.small_num)", setup="import __main__ as z"), + "seconds", + ) print("\nFor medium_num = ", medium_num, ":") print( @@ -111,6 +134,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.medium_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\t\tans =", + digital_root(medium_num), + "\ttime =", + timeit("z.digital_root(z.medium_num)", setup="import __main__ as z"), + "seconds", + ) print("\nFor large_num = ", large_num, ":") print( @@ -137,6 +168,14 @@ def benchmark() -> None: timeit("z.sum_of_digits_compact(z.large_num)", setup="import __main__ as z"), "seconds", ) + print( + "> digital_root()", + "\tans =", + digital_root(large_num), + "\ttime =", + timeit("z.digital_root(z.large_num)", setup="import __main__ as z"), + "seconds", + ) if __name__ == "__main__":