From 33f392e2ae0535c7e10478a89fb6be1a1ca103fd Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Sat, 5 Jun 2021 15:36:47 +0530 Subject: [PATCH 1/6] case switch using python --- other/switch_case_with_python_function.py | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 other/switch_case_with_python_function.py diff --git a/other/switch_case_with_python_function.py b/other/switch_case_with_python_function.py new file mode 100644 index 000000000000..45c1491ed97f --- /dev/null +++ b/other/switch_case_with_python_function.py @@ -0,0 +1,46 @@ +''' +@author : Suyash Shrivastava +''' +import sys + +def switch_case_mapper(case_key, attr1, attr2): + """ + >>> switch_case_mapper('add',1,2) + 3 + >>> switch_case_mapper('multiply',1,2) + 2 + >>> switch_case_mapper('divide',1,2) + 0.5 + >>> switch_case_mapper('modulo',1,2) + 1 + >>> switch_case_mapper('subtract',1,2) + -1 + >>> switch_case_mapper('invalid case',1,2) + 'Case Not Found!' + """ + method = getattr(sys.modules[__name__], case_key, lambda x,y: "Case Not Found!") + return method(attr1, attr2) + +def add(attr1, attr2): + return attr1+attr2 + +def subtract(attr1, attr2): + return attr1-attr2 + +def multiply(attr1, attr2): + return attr1*attr2 + +def modulo(attr1, attr2): + return attr1%attr2 + +def divide(attr1, attr2): + return attr1/attr2 + + +if __name__ == "__main__": + switch_case_mapper('multiply',1,2) + switch_case_mapper('add',1,2) + switch_case_mapper('subtract',1,2) + switch_case_mapper('divide',1,2) + switch_case_mapper('modulo',1,2) + switch_case_mapper('invalid case',1,2) \ No newline at end of file From c3bd33d2c531011f0a7087d76a5a8aad7372429e Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Sat, 5 Jun 2021 16:19:44 +0530 Subject: [PATCH 2/6] review comments --- other/switch_case_with_python_function.py | 38 +++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/other/switch_case_with_python_function.py b/other/switch_case_with_python_function.py index 45c1491ed97f..b544962efe50 100644 --- a/other/switch_case_with_python_function.py +++ b/other/switch_case_with_python_function.py @@ -3,14 +3,14 @@ ''' import sys -def switch_case_mapper(case_key, attr1, attr2): +def switch_case_mapper(case_key, attr1, attr2) -> int: """ >>> switch_case_mapper('add',1,2) 3 >>> switch_case_mapper('multiply',1,2) 2 >>> switch_case_mapper('divide',1,2) - 0.5 + 0 >>> switch_case_mapper('modulo',1,2) 1 >>> switch_case_mapper('subtract',1,2) @@ -18,23 +18,43 @@ def switch_case_mapper(case_key, attr1, attr2): >>> switch_case_mapper('invalid case',1,2) 'Case Not Found!' """ - method = getattr(sys.modules[__name__], case_key, lambda x,y: "Case Not Found!") + method = getattr(sys.modules[__name__], case_key, lambda attr1,attr2: "Case Not Found!") return method(attr1, attr2) -def add(attr1, attr2): +def add(attr1:int, attr2:int) -> int: + """ + >>> add(1,2) + 3 + """ return attr1+attr2 -def subtract(attr1, attr2): +def subtract(attr1:int, attr2:int) -> int: + """ + >>> subtract(1,2) + -1 + """ return attr1-attr2 -def multiply(attr1, attr2): +def multiply(attr1:int, attr2:int) -> int: + """ + >>> multiply(1,2) + 2 + """ return attr1*attr2 -def modulo(attr1, attr2): +def modulo(attr1:int, attr2:int) -> int: + """ + >>> modulo(1,2) + 1 + """ return attr1%attr2 -def divide(attr1, attr2): - return attr1/attr2 +def divide(attr1:int, attr2:int) -> int: + """ + >>> divide(1,2) + 0 + """ + return attr1//attr2 if __name__ == "__main__": From 7f0c9442b92b36a3545b2b32dcf511ae85ee4a95 Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Sat, 5 Jun 2021 16:28:14 +0530 Subject: [PATCH 3/6] added type hints --- other/switch_case_with_python_function.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/switch_case_with_python_function.py b/other/switch_case_with_python_function.py index b544962efe50..eae51571cf3b 100644 --- a/other/switch_case_with_python_function.py +++ b/other/switch_case_with_python_function.py @@ -3,7 +3,7 @@ ''' import sys -def switch_case_mapper(case_key, attr1, attr2) -> int: +def switch_case_mapper(case_key:str, attr1:int, attr2:int) -> int: """ >>> switch_case_mapper('add',1,2) 3 From 22963d11faece15184ff6e5310cc694838cbb686 Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Fri, 11 Jun 2021 08:34:19 +0530 Subject: [PATCH 4/6] general code format --- other/switch_case_with_python_function.py | 35 ++++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/other/switch_case_with_python_function.py b/other/switch_case_with_python_function.py index eae51571cf3b..8baf3260f2d0 100644 --- a/other/switch_case_with_python_function.py +++ b/other/switch_case_with_python_function.py @@ -3,7 +3,8 @@ ''' import sys -def switch_case_mapper(case_key:str, attr1:int, attr2:int) -> int: + +def switch_case_mapper(case_key: str, attr1: int, attr2: int) -> int: """ >>> switch_case_mapper('add',1,2) 3 @@ -18,38 +19,44 @@ def switch_case_mapper(case_key:str, attr1:int, attr2:int) -> int: >>> switch_case_mapper('invalid case',1,2) 'Case Not Found!' """ - method = getattr(sys.modules[__name__], case_key, lambda attr1,attr2: "Case Not Found!") + method = getattr(sys.modules[__name__], case_key, + lambda attr1, attr2: "Case Not Found!") return method(attr1, attr2) -def add(attr1:int, attr2:int) -> int: + +def add(attr1: int, attr2: int) -> int: """ >>> add(1,2) 3 """ return attr1+attr2 -def subtract(attr1:int, attr2:int) -> int: + +def subtract(attr1: int, attr2: int) -> int: """ >>> subtract(1,2) -1 """ return attr1-attr2 -def multiply(attr1:int, attr2:int) -> int: + +def multiply(attr1: int, attr2: int) -> int: """ >>> multiply(1,2) 2 """ return attr1*attr2 -def modulo(attr1:int, attr2:int) -> int: + +def modulo(attr1: int, attr2: int) -> int: """ >>> modulo(1,2) 1 """ - return attr1%attr2 + return attr1 % attr2 + -def divide(attr1:int, attr2:int) -> int: +def divide(attr1: int, attr2: int) -> int: """ >>> divide(1,2) 0 @@ -58,9 +65,9 @@ def divide(attr1:int, attr2:int) -> int: if __name__ == "__main__": - switch_case_mapper('multiply',1,2) - switch_case_mapper('add',1,2) - switch_case_mapper('subtract',1,2) - switch_case_mapper('divide',1,2) - switch_case_mapper('modulo',1,2) - switch_case_mapper('invalid case',1,2) \ No newline at end of file + switch_case_mapper('multiply', 1, 2) + switch_case_mapper('add', 1, 2) + switch_case_mapper('subtract', 1, 2) + switch_case_mapper('divide', 1, 2) + switch_case_mapper('modulo', 1, 2) + switch_case_mapper('invalid case', 1, 2) From b8681071776c4c8aa68bc3a2fc30d59c8a8b0b31 Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Fri, 11 Jun 2021 09:12:06 +0530 Subject: [PATCH 5/6] [mypy] Fix type annotations for secant_method.py --- arithmetic_analysis/secant_method.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py index 7eb1dd8f5c6b..45bcb185fc3e 100644 --- a/arithmetic_analysis/secant_method.py +++ b/arithmetic_analysis/secant_method.py @@ -26,4 +26,4 @@ def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float if __name__ == "__main__": - print(f"Example: {secant_method(1, 3, 2) = }") + print(f"Example: {secant_method(1, 3, 2)}") From 3e8a20d2fe9cc4e070497050df41cda70e57a74d Mon Sep 17 00:00:00 2001 From: suyash shrivastava Date: Fri, 11 Jun 2021 09:22:37 +0530 Subject: [PATCH 6/6] remove bad push --- other/switch_case_with_python_function.py | 73 ----------------------- 1 file changed, 73 deletions(-) delete mode 100644 other/switch_case_with_python_function.py diff --git a/other/switch_case_with_python_function.py b/other/switch_case_with_python_function.py deleted file mode 100644 index 8baf3260f2d0..000000000000 --- a/other/switch_case_with_python_function.py +++ /dev/null @@ -1,73 +0,0 @@ -''' -@author : Suyash Shrivastava -''' -import sys - - -def switch_case_mapper(case_key: str, attr1: int, attr2: int) -> int: - """ - >>> switch_case_mapper('add',1,2) - 3 - >>> switch_case_mapper('multiply',1,2) - 2 - >>> switch_case_mapper('divide',1,2) - 0 - >>> switch_case_mapper('modulo',1,2) - 1 - >>> switch_case_mapper('subtract',1,2) - -1 - >>> switch_case_mapper('invalid case',1,2) - 'Case Not Found!' - """ - method = getattr(sys.modules[__name__], case_key, - lambda attr1, attr2: "Case Not Found!") - return method(attr1, attr2) - - -def add(attr1: int, attr2: int) -> int: - """ - >>> add(1,2) - 3 - """ - return attr1+attr2 - - -def subtract(attr1: int, attr2: int) -> int: - """ - >>> subtract(1,2) - -1 - """ - return attr1-attr2 - - -def multiply(attr1: int, attr2: int) -> int: - """ - >>> multiply(1,2) - 2 - """ - return attr1*attr2 - - -def modulo(attr1: int, attr2: int) -> int: - """ - >>> modulo(1,2) - 1 - """ - return attr1 % attr2 - - -def divide(attr1: int, attr2: int) -> int: - """ - >>> divide(1,2) - 0 - """ - return attr1//attr2 - - -if __name__ == "__main__": - switch_case_mapper('multiply', 1, 2) - switch_case_mapper('add', 1, 2) - switch_case_mapper('subtract', 1, 2) - switch_case_mapper('divide', 1, 2) - switch_case_mapper('modulo', 1, 2) - switch_case_mapper('invalid case', 1, 2)