From f97cda971ba8f079dc1fe51db5e773989c3386c6 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 5 Oct 2022 17:11:21 +0300 Subject: [PATCH 01/13] adding the remove digit algorithm --- maths/remove_digit.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 maths/remove_digit.py diff --git a/maths/remove_digit.py b/maths/remove_digit.py new file mode 100644 index 000000000000..f27d7ad48c11 --- /dev/null +++ b/maths/remove_digit.py @@ -0,0 +1,32 @@ +def remove_digit(num: int)->int: + """ + + returns the biggest possible result + that can be achieved by removing + one digit from the given number + + >>> remove_digit(152) + 52 + >>> remove_digit(6385) + 685 + >>> remove_digit(-11) + 1 + >>> remove_digit(2222222) + 222222 + >>> remove_digit("2222222") + 0 + >>> remove_digit("string input") + 0 + """ + + try: + num_str = str(abs(num)) + num_list = [[c for c in num_str] for c in range(len(num_str))] + for c in range(len(num_str)): + num_list[c].pop(c) + return sorted([int("".join([a for a in c])) for c in num_list])[-1] + except TypeError: + return 0 + +if __name__ == "__main__": + __import__("doctest").testmod() \ No newline at end of file From b9013e5826efc1bd018d9cfed91abcf0e597ff93 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 5 Oct 2022 14:43:59 +0000 Subject: [PATCH 02/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/remove_digit.py | 55 ++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index f27d7ad48c11..f4fa6aa553bb 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -1,32 +1,33 @@ -def remove_digit(num: int)->int: - """ +def remove_digit(num: int) -> int: + """ - returns the biggest possible result - that can be achieved by removing - one digit from the given number + returns the biggest possible result + that can be achieved by removing + one digit from the given number - >>> remove_digit(152) - 52 - >>> remove_digit(6385) - 685 - >>> remove_digit(-11) - 1 - >>> remove_digit(2222222) - 222222 - >>> remove_digit("2222222") - 0 - >>> remove_digit("string input") - 0 - """ + >>> remove_digit(152) + 52 + >>> remove_digit(6385) + 685 + >>> remove_digit(-11) + 1 + >>> remove_digit(2222222) + 222222 + >>> remove_digit("2222222") + 0 + >>> remove_digit("string input") + 0 + """ + + try: + num_str = str(abs(num)) + num_list = [[c for c in num_str] for c in range(len(num_str))] + for c in range(len(num_str)): + num_list[c].pop(c) + return sorted([int("".join([a for a in c])) for c in num_list])[-1] + except TypeError: + return 0 - try: - num_str = str(abs(num)) - num_list = [[c for c in num_str] for c in range(len(num_str))] - for c in range(len(num_str)): - num_list[c].pop(c) - return sorted([int("".join([a for a in c])) for c in num_list])[-1] - except TypeError: - return 0 if __name__ == "__main__": - __import__("doctest").testmod() \ No newline at end of file + __import__("doctest").testmod() From d4d1ef8d5f3d8a22a7b9dd44d74ca4df2bfeeece Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:30:44 +0300 Subject: [PATCH 03/13] refactoring code --- maths/remove_digit.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index f4fa6aa553bb..f78d6c44ea17 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -14,19 +14,28 @@ def remove_digit(num: int) -> int: >>> remove_digit(2222222) 222222 >>> remove_digit("2222222") - 0 + Traceback (most recent call last): + TypeError: only integers accepted as input >>> remove_digit("string input") - 0 + Traceback (most recent call last): + TypeError: only integers accepted as input """ - try: + if type(num) == int: num_str = str(abs(num)) - num_list = [[c for c in num_str] for c in range(len(num_str))] - for c in range(len(num_str)): - num_list[c].pop(c) - return sorted([int("".join([a for a in c])) for c in num_list])[-1] - except TypeError: - return 0 + num_transpositions = [ + [char for char in num_str] for char in range(len(num_str)) + ] + for index in range(len(num_str)): + num_transpositions[index].pop(index) + return sorted( + [ + int("".join([char for char in transposition])) + for transposition in num_transpositions + ] + )[-1] + else: + raise TypeError("only integers accepted as input") if __name__ == "__main__": From c4b81c20e0716484c18329acadefd52ba840d0e1 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 13:55:54 +0100 Subject: [PATCH 04/13] Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris --- maths/remove_digit.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index f78d6c44ea17..d1ce43a85220 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -21,7 +21,7 @@ def remove_digit(num: int) -> int: TypeError: only integers accepted as input """ - if type(num) == int: + if isinstance(num, int): num_str = str(abs(num)) num_transpositions = [ [char for char in num_str] for char in range(len(num_str)) @@ -29,11 +29,11 @@ def remove_digit(num: int) -> int: for index in range(len(num_str)): num_transpositions[index].pop(index) return sorted( - [ - int("".join([char for char in transposition])) - for transposition in num_transpositions + int("".join([char for char in transposition])) + for transposition in num_transpositions, + reverse=True ] - )[-1] + )[0] else: raise TypeError("only integers accepted as input") From a4a47ab688adc4bb3d1c92095744981d77f04dc7 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 14:09:05 +0100 Subject: [PATCH 05/13] Update remove_digit.py --- maths/remove_digit.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index d1ce43a85220..772b89687025 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -29,10 +29,8 @@ def remove_digit(num: int) -> int: for index in range(len(num_str)): num_transpositions[index].pop(index) return sorted( - int("".join([char for char in transposition])) - for transposition in num_transpositions, - reverse=True - ] + int("".join([char for char in transposition])) for transposition in num_transpositions, + reverse=True )[0] else: raise TypeError("only integers accepted as input") From 2e1aa1ae234a719d78ef2c5b77a2d495d4d06074 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 16 Nov 2022 17:20:23 +0200 Subject: [PATCH 06/13] fixing errors --- maths/remove_digit.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index 772b89687025..45d5a5bf7f80 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -29,8 +29,11 @@ def remove_digit(num: int) -> int: for index in range(len(num_str)): num_transpositions[index].pop(index) return sorted( - int("".join([char for char in transposition])) for transposition in num_transpositions, - reverse=True + ( + int("".join([char for char in transposition])) + for transposition in num_transpositions + ), + reverse=True, )[0] else: raise TypeError("only integers accepted as input") From a493d79cc45e42b3729ddac6feadda187ae56a3e Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 16 Nov 2022 17:30:55 +0200 Subject: [PATCH 07/13] fixing error C416 --- maths/remove_digit.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index 45d5a5bf7f80..a20fe5e07f3f 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -24,13 +24,13 @@ def remove_digit(num: int) -> int: if isinstance(num, int): num_str = str(abs(num)) num_transpositions = [ - [char for char in num_str] for char in range(len(num_str)) + list(num_str) for char in range(len(num_str)) ] for index in range(len(num_str)): num_transpositions[index].pop(index) return sorted( ( - int("".join([char for char in transposition])) + int("".join(list(transposition))) for transposition in num_transpositions ), reverse=True, From 80372daabaaddc76a3666f44b850adc00a8b4dfb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Nov 2022 15:32:12 +0000 Subject: [PATCH 08/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/remove_digit.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/maths/remove_digit.py b/maths/remove_digit.py index a20fe5e07f3f..6cec01945d94 100644 --- a/maths/remove_digit.py +++ b/maths/remove_digit.py @@ -23,16 +23,11 @@ def remove_digit(num: int) -> int: if isinstance(num, int): num_str = str(abs(num)) - num_transpositions = [ - list(num_str) for char in range(len(num_str)) - ] + num_transpositions = [list(num_str) for char in range(len(num_str))] for index in range(len(num_str)): num_transpositions[index].pop(index) return sorted( - ( - int("".join(list(transposition))) - for transposition in num_transpositions - ), + (int("".join(list(transposition))) for transposition in num_transpositions), reverse=True, )[0] else: From cca3470c06d2a2f362406f41b291a70c36eb9536 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 19 Nov 2022 15:31:20 +0200 Subject: [PATCH 09/13] adding a collection of switch case methods --- strings/string_switch_case.py | 93 +++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 strings/string_switch_case.py diff --git a/strings/string_switch_case.py b/strings/string_switch_case.py new file mode 100644 index 000000000000..19438b51ecc5 --- /dev/null +++ b/strings/string_switch_case.py @@ -0,0 +1,93 @@ +import re + +""" +general info: +https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby + +pascal case [ an upper Camel Case ]: https://en.wikipedia.org/wiki/Camel_case + +camel case: https://en.wikipedia.org/wiki/Camel_case + +kebab case [ can be found in general info ]: +https://en.wikipedia.org/wiki/Naming_convention_(programming)#Python_and_Ruby + +snake case: https://en.wikipedia.org/wiki/Snake_case +""" + +# assistant functions +def split_input(str_: str) -> str: + return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)] + + +def to_simple_case(str_: str) -> str: + string_split = split_input(str_) + return "".join( + ["".join([char.capitalize() for char in sub_str]) for sub_str in string_split] + ) + + +def to_complex_case(text: str, upper: bool, separator: str) -> str: + try: + string_split = split_input(text) + if upper: + res_str = "".join( + [ + separator.join([char.upper() for char in sub_str]) + for sub_str in string_split + ] + ) + else: + res_str = "".join( + [ + separator.join([char.lower() for char in sub_str]) + for sub_str in string_split + ] + ) + return res_str + except IndexError: + return "not valid string" + + +# main content +def to_pascal_case(text: str) -> str: + """ + >>> to_pascal_case("one two 31235three4four") + 'OneTwo31235three4four' + """ + return to_simple_case(text) + + +def to_camel_case(text: str) -> str: + """ + >>> to_camel_case("one two 31235three4four") + 'oneTwo31235three4four' + """ + try: + res_str = to_simple_case(text) + return res_str[0].lower() + res_str[1:] + except IndexError: + return "not valid string" + + +def to_snake_case(text: str, upper: bool) -> str: + """ + >>> to_snake_case("one two 31235three4four", True) + 'ONE_TWO_31235THREE4FOUR' + >>> to_snake_case("one two 31235three4four", False) + 'one_two_31235three4four' + """ + return to_complex_case(text, upper, "_") + + +def to_kebab_case(text: str, upper: bool) -> str: + """ + >>> to_kebab_case("one two 31235three4four", True) + 'ONE-TWO-31235THREE4FOUR' + >>> to_kebab_case("one two 31235three4four", False) + 'one-two-31235three4four' + """ + return to_complex_case(text, upper, "-") + + +if __name__ == "__main__": + __import__("doctest").testmod() From d11c7c9954dcb138a91feb9ea9b502e238c6147d Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 23 Nov 2022 17:45:25 +0200 Subject: [PATCH 10/13] deleting remove_digit deleting remove_digit because it is in the wrong pr --- maths/remove_digit.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 maths/remove_digit.py diff --git a/maths/remove_digit.py b/maths/remove_digit.py deleted file mode 100644 index 6cec01945d94..000000000000 --- a/maths/remove_digit.py +++ /dev/null @@ -1,38 +0,0 @@ -def remove_digit(num: int) -> int: - """ - - returns the biggest possible result - that can be achieved by removing - one digit from the given number - - >>> remove_digit(152) - 52 - >>> remove_digit(6385) - 685 - >>> remove_digit(-11) - 1 - >>> remove_digit(2222222) - 222222 - >>> remove_digit("2222222") - Traceback (most recent call last): - TypeError: only integers accepted as input - >>> remove_digit("string input") - Traceback (most recent call last): - TypeError: only integers accepted as input - """ - - if isinstance(num, int): - num_str = str(abs(num)) - num_transpositions = [list(num_str) for char in range(len(num_str))] - for index in range(len(num_str)): - num_transpositions[index].pop(index) - return sorted( - (int("".join(list(transposition))) for transposition in num_transpositions), - reverse=True, - )[0] - else: - raise TypeError("only integers accepted as input") - - -if __name__ == "__main__": - __import__("doctest").testmod() From 358abacae6a65ac7b4817535fa2f4e2a518e31a0 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 23 Nov 2022 18:00:45 +0200 Subject: [PATCH 11/13] adding tests for assistant functions --- strings/string_switch_case.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/strings/string_switch_case.py b/strings/string_switch_case.py index 19438b51ecc5..5b4c15798791 100644 --- a/strings/string_switch_case.py +++ b/strings/string_switch_case.py @@ -16,10 +16,18 @@ # assistant functions def split_input(str_: str) -> str: + """ + >>> split_input("one two 31235three4four") + [['one', 'two', '31235three4four']] + """ return [char.split() for char in re.split(r"[^ a-z A-Z 0-9 \s]", str_)] def to_simple_case(str_: str) -> str: + """ + >>> to_simple_case("one two 31235three4four") + 'OneTwo31235three4four' + """ string_split = split_input(str_) return "".join( ["".join([char.capitalize() for char in sub_str]) for sub_str in string_split] @@ -27,6 +35,12 @@ def to_simple_case(str_: str) -> str: def to_complex_case(text: str, upper: bool, separator: str) -> str: + """ + >>> to_complex_case("one two 31235three4four", True, "_") + 'ONE_TWO_31235THREE4FOUR' + >>> to_complex_case("one two 31235three4four", False, "-") + 'one-two-31235three4four' + """ try: string_split = split_input(text) if upper: From 479a637159a53cef987a21d519297de2b0f18f58 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 23 Nov 2022 18:09:33 +0200 Subject: [PATCH 12/13] fixing tests for assistant functions --- strings/string_switch_case.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/string_switch_case.py b/strings/string_switch_case.py index 5b4c15798791..fe3fc3181f88 100644 --- a/strings/string_switch_case.py +++ b/strings/string_switch_case.py @@ -15,7 +15,7 @@ """ # assistant functions -def split_input(str_: str) -> str: +def split_input(str_: str) -> list[[list[str]]]: """ >>> split_input("one two 31235three4four") [['one', 'two', '31235three4four']] From f38c544414abe0ea5b8aac0196589e303b4d2fed Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Sat, 3 Dec 2022 15:10:11 +0200 Subject: [PATCH 13/13] following bot reccomendations following bot reccomendations --- strings/string_switch_case.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/strings/string_switch_case.py b/strings/string_switch_case.py index fe3fc3181f88..9a07472dfd71 100644 --- a/strings/string_switch_case.py +++ b/strings/string_switch_case.py @@ -14,8 +14,9 @@ snake case: https://en.wikipedia.org/wiki/Snake_case """ + # assistant functions -def split_input(str_: str) -> list[[list[str]]]: +def split_input(str_: str) -> list: """ >>> split_input("one two 31235three4four") [['one', 'two', '31235three4four']]