From e34dd4d800571eacbe9c547687b52bd088d03b41 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:07:21 +0530 Subject: [PATCH 01/44] [mypy] Fix annotations in `maths/series/p_series.py` --- maths/series/p_series.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 04019aed5a85..c85ed1c1e7f8 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -1,48 +1,40 @@ """ This is a pure Python implementation of the P-Series algorithm https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series - For doctests run following command: python -m doctest -v p_series.py or python3 -m doctest -v p_series.py - For manual testing run: python3 p_series.py """ -def p_series(nth_term: int, power: int) -> list: - """Pure Python implementation of P-Series algorithm - +def p_series(nth_term: int, power: int) -> list[float]: + """ + Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term - Examples: >>> p_series(5, 2) - [1, '1/4', '1/9', '1/16', '1/25'] + [1.0, 0.25, 0.1111111111111111, 0.0625, 0.04] >>> p_series(-5, 2) [] >>> p_series(5, -2) - [1, '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] - >>> p_series("", 1000) - '' + [1.0, 4.0, 9.0, 16.0, 25.0] >>> p_series(0, 0) [] >>> p_series(1, 1) - [1] + [1.0] """ - if nth_term == "": - return nth_term - nth_term = int(nth_term) - power = int(power) - series = [] - for temp in range(int(nth_term)): - series.append(f"1/{pow(temp + 1, int(power))}" if series else 1) + if nth_term and power: + return [] + series = [1.0] + for temp in range(nth_term + 1): + series.append(1/(pow(temp + 1, power))) return series if __name__ == "__main__": - nth_term = input("Enter the last number (nth term) of the P-Series") - power = input("Enter the power for P-Series") - print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") - print(p_series(nth_term, power)) + import doctest + + doctest.testmod() From cea397e2f01f0e528f61397090b207137161a60e Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:13:29 +0530 Subject: [PATCH 02/44] Update p_series.py --- maths/series/p_series.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index c85ed1c1e7f8..6919e7792dc5 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -26,11 +26,9 @@ def p_series(nth_term: int, power: int) -> list[float]: >>> p_series(1, 1) [1.0] """ - if nth_term and power: - return [] - series = [1.0] + series = [] for temp in range(nth_term + 1): - series.append(1/(pow(temp + 1, power))) + series.append(1 / (pow(temp + 1, power)) if series else 1.0) return series From 71731667dd4070a877bfe53c830bfe4fd2e6538a Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:17:13 +0530 Subject: [PATCH 03/44] Update p_series.py --- maths/series/p_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 6919e7792dc5..8a0af7c0d723 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -27,7 +27,7 @@ def p_series(nth_term: int, power: int) -> list[float]: [1.0] """ series = [] - for temp in range(nth_term + 1): + for temp in range(nth_term): series.append(1 / (pow(temp + 1, power)) if series else 1.0) return series From b5440eca97f81aee334b470aca979f49557eb22e Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:22:18 +0530 Subject: [PATCH 04/44] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 1a2282c44846..3e8501a0ba1e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 1ce55e7e5734ac3af6f0eb18b07ba37387f88c20 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:26:57 +0530 Subject: [PATCH 05/44] Type annotation for series --- maths/series/p_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 8a0af7c0d723..23a3eb3a4024 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -26,7 +26,7 @@ def p_series(nth_term: int, power: int) -> list[float]: >>> p_series(1, 1) [1.0] """ - series = [] + series: list[float] = [] for temp in range(nth_term): series.append(1 / (pow(temp + 1, power)) if series else 1.0) return series From 95e41dd2b9623c7923882232badb8672112c25af Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:33:02 +0530 Subject: [PATCH 06/44] Annotate maths/proth_number.py (properly) --- maths/proth_number.py | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/maths/proth_number.py b/maths/proth_number.py index 065244ed7607..b05b4fdea0db 100644 --- a/maths/proth_number.py +++ b/maths/proth_number.py @@ -1,6 +1,5 @@ """ Calculate the nth Proth number - Source: https://handwiki.org/wiki/Proth_number """ @@ -12,22 +11,17 @@ def proth(number: int) -> int: """ :param number: nth number to calculate in the sequence :return: the nth number in Proth number - Note: indexing starts at 1 i.e. proth(1) gives the first Proth number of 3 - >>> proth(6) 25 - >>> proth(0) Traceback (most recent call last): ... ValueError: Input value of [number=0] must be > 0 - >>> proth(-1) Traceback (most recent call last): ... ValueError: Input value of [number=-1] must be > 0 - >>> proth(6.0) Traceback (most recent call last): ... @@ -44,18 +38,16 @@ def proth(number: int) -> int: elif number == 2: return 5 else: - block_index = number // 3 """ +1 for binary starting at 0 i.e. 2^0, 2^1, etc. +1 to start the sequence at the 3rd Proth number Hence, we have a +2 in the below statement """ - block_index = math.log(block_index, 2) + 2 - block_index = int(block_index) + block_index: int = int(math.log(number // 3, 2)) + 2 - proth_list = [3, 5] - proth_index = 2 - increment = 3 + proth_list: list[int] = [3, 5] + proth_index: int = 2 + increment: int = 3 for block in range(1, block_index): for move in range(increment): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1]) @@ -66,12 +58,6 @@ def proth(number: int) -> int: if __name__ == "__main__": - for number in range(11): - value = 0 - try: - value = proth(number) - except ValueError: - print(f"ValueError: there is no {number}th Proth number") - continue + import doctest - print(f"The {number}th Proth number: {value}") + doctest.testmod() From 4f00c093d9c3d64e2c3d4460577b9afe9b32c3c9 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 19:37:18 +0530 Subject: [PATCH 07/44] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 3e8501a0ba1e..000b7b61a0b0 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 99a0cf51e07353f29877c1cf6e35b069dc36fea2 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 22:49:50 +0530 Subject: [PATCH 08/44] Annotate average_mode.py --- maths/average_mode.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 83db820072bf..2532e927ad19 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -1,9 +1,11 @@ -def mode(input_list: list) -> list: # Defining function "mode." +from typing import Any +from collections import Counter + + +def mode(input_list: list) -> list[Any]: # Defining function "mode." """This function returns the mode(Mode as in the measures of central tendency) of the input data. - The input list may contain any Datastructure or any Datatype. - >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] >>> mode(input_list) [2] @@ -20,15 +22,8 @@ def mode(input_list: list) -> list: # Defining function "mode." >>> mode(input_list) ['x', 'y'] """ - result = list() # Empty list to store the counts of elements in input_list - for x in input_list: - result.append(input_list.count(x)) - if not result: - return [] - y = max(result) # Gets the maximum value in the result list. - # Gets values of modes - result = {input_list[i] for i, value in enumerate(result) if value == y} - return sorted(result) + counts: list[int] = list((Counter(input_list)).values()) + return list({val for val in input_list if input_list.count(val) == max(counts)}) if __name__ == "__main__": From e9c81abe6f27ca4516d469942f8648e24ef3cf5f Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 22:56:13 +0530 Subject: [PATCH 09/44] Update average_mode.py --- maths/average_mode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 2532e927ad19..e33f3cb5d17e 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -1,5 +1,5 @@ -from typing import Any from collections import Counter +from typing import Any def mode(input_list: list) -> list[Any]: # Defining function "mode." @@ -23,7 +23,7 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." ['x', 'y'] """ counts: list[int] = list((Counter(input_list)).values()) - return list({val for val in input_list if input_list.count(val) == max(counts)}) + return list({val for val in input_list if input_list.count(val) == max(counts)}).sort() if __name__ == "__main__": From d2a04cfa10ff4f146afb2e2ceeb8ed5e6c2e559d Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:02:24 +0530 Subject: [PATCH 10/44] Update average_mode.py --- maths/average_mode.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index e33f3cb5d17e..36b1617b6df8 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -2,7 +2,7 @@ from typing import Any -def mode(input_list: list) -> list[Any]: # Defining function "mode." +def mode(input_list: list): # Defining function "mode." """This function returns the mode(Mode as in the measures of central tendency) of the input data. The input list may contain any Datastructure or any Datatype. @@ -23,7 +23,9 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." ['x', 'y'] """ counts: list[int] = list((Counter(input_list)).values()) - return list({val for val in input_list if input_list.count(val) == max(counts)}).sort() + return list(sorted( + {val for val in input_list if input_list.count(val) == max(counts)} + )) if __name__ == "__main__": From 567210b3ac8aa84c859d36c2e4eca1c33eaf88d8 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:06:22 +0530 Subject: [PATCH 11/44] Update average_mode.py --- maths/average_mode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 36b1617b6df8..71bfa5367d31 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -23,9 +23,9 @@ def mode(input_list: list): # Defining function "mode." ['x', 'y'] """ counts: list[int] = list((Counter(input_list)).values()) - return list(sorted( - {val for val in input_list if input_list.count(val) == max(counts)} - )) + return list( + sorted({val for val in input_list if input_list.count(val) == max(counts)}) + ) if __name__ == "__main__": From 6b07a1f8c75f0518dbd251d2033ba9a206d7ad54 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:10:38 +0530 Subject: [PATCH 12/44] Update average_mode.py --- maths/average_mode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 71bfa5367d31..dc0efbcb8c8c 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -2,7 +2,7 @@ from typing import Any -def mode(input_list: list): # Defining function "mode." +def mode(input_list: list) -> list[Any]: # Defining function "mode." """This function returns the mode(Mode as in the measures of central tendency) of the input data. The input list may contain any Datastructure or any Datatype. From 175b785d7aa02da0270f98caee2224202bc6897d Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:14:11 +0530 Subject: [PATCH 13/44] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 000b7b61a0b0..6fbea9572cb9 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/gamma_recursive.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 9902c889c6ff0a2ca67d5ab6883eddafdf9df0ca Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:23:25 +0530 Subject: [PATCH 14/44] Fix annotations in gamma_recursive.py --- maths/gamma_recursive.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py index 683d7adb1aa8..8d30299a2f17 100644 --- a/maths/gamma_recursive.py +++ b/maths/gamma_recursive.py @@ -71,8 +71,3 @@ def test_gamma() -> None: from doctest import testmod testmod() - num = 1 - while num: - num = float(input("Gamma of: ")) - print(f"gamma({num}) = {gamma(num)}") - print("\nEnter 0 to exit...") From e52a2932a3f96329e2d69b50baca2b31adfde5b1 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:29:00 +0530 Subject: [PATCH 15/44] Remove from excluded in mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 6fbea9572cb9..1c2143c3eb1c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/gamma_recursive.py|maths/series/geometric_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From e98c58e23e54b61576b459d71627bbf82c61f94f Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:42:10 +0530 Subject: [PATCH 16/44] Annotations for geometric_series.py --- maths/series/geometric_series.py | 52 ++++++++++++++++---------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index d12382e6d8c4..ecec3630030b 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -1,7 +1,6 @@ """ This is a pure Python implementation of the Geometric Series algorithm https://en.wikipedia.org/wiki/Geometric_series - Run the doctests with the following command: python3 -m doctest -v geometric_series.py or @@ -11,53 +10,54 @@ """ -def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list: - """Pure Python implementation of Geometric Series algorithm +def geometric_series( + nth_term: float, start_term_a: float, common_ratio_r: float +) -> list[float]: + """ + Pure Python implementation of Geometric Series algorithm + :param nth_term: The last term (nth term of Geometric Series) :param start_term_a : The first term of Geometric Series :param common_ratio_r : The common ratio between all the terms :return: The Geometric Series starting from first term a and multiple of common ration with first term with increase in power till last term (nth term) + Examples: - >>> geometric_series(4, 2, 2) - [2, '4.0', '8.0', '16.0'] >>> geometric_series(4.0, 2.0, 2.0) - [2.0, '4.0', '8.0', '16.0'] + [2.0, 4.0, 8.0, 16.0] + >>> geometric_series(4.0, 2.0, 2.0) + [2.0, 4.0, 8.0, 16.0] >>> geometric_series(4.1, 2.1, 2.1) - [2.1, '4.41', '9.261000000000001', '19.448100000000004'] - >>> geometric_series(4, 2, -2) - [2, '-4.0', '8.0', '-16.0'] - >>> geometric_series(4, -2, 2) - [-2, '-4.0', '-8.0', '-16.0'] - >>> geometric_series(-4, 2, 2) + [2.1, 4.41, 9.261000000000001, 19.448100000000004] + >>> geometric_series(4.0, 2.0, -2.0) + [2.0, -4.0, 8.0, -16.0] + >>> geometric_series(4.0, -2.0, 2.0) + [-2.0, -4.0, -8.0, -16.0] + >>> geometric_series(-4.0, 2.0, 2.0) [] - >>> geometric_series(0, 100, 500) + >>> geometric_series(0, 100.0, 500.0) [] - >>> geometric_series(1, 1, 1) + >>> geometric_series(1.0, 1.0, 1.0) [1] >>> geometric_series(0, 0, 0) [] """ - if "" in (nth_term, start_term_a, common_ratio_r): - return "" - series = [] - power = 1 + if 0 in (nth_term, start_term_a, common_ratio_r): + return [] + series: list[float] = [] + power: int = 1 multiple = common_ratio_r for _ in range(int(nth_term)): if series == []: series.append(start_term_a) else: power += 1 - series.append(str(float(start_term_a) * float(multiple))) + series.append(start_term_a * multiple) multiple = pow(float(common_ratio_r), power) return series if __name__ == "__main__": - nth_term = input("Enter the last number (n term) of the Geometric Series") - start_term_a = input("Enter the starting term (a) of the Geometric Series") - common_ratio_r = input( - "Enter the common ratio between two terms (r) of the Geometric Series" - ) - print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") - print(geometric_series(nth_term, start_term_a, common_ratio_r)) + import doctest + + doctest.testmod() From 83af001c0f55782006a520c839d7efe1d6d2b4dc Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sat, 6 Nov 2021 23:46:35 +0530 Subject: [PATCH 17/44] Update geometric_series.py --- maths/series/geometric_series.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index ecec3630030b..03f90338efd3 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -15,13 +15,13 @@ def geometric_series( ) -> list[float]: """ Pure Python implementation of Geometric Series algorithm - + :param nth_term: The last term (nth term of Geometric Series) :param start_term_a : The first term of Geometric Series :param common_ratio_r : The common ratio between all the terms :return: The Geometric Series starting from first term a and multiple of common ration with first term with increase in power till last term (nth term) - + Examples: >>> geometric_series(4.0, 2.0, 2.0) [2.0, 4.0, 8.0, 16.0] @@ -38,7 +38,7 @@ def geometric_series( >>> geometric_series(0, 100.0, 500.0) [] >>> geometric_series(1.0, 1.0, 1.0) - [1] + [1.0] >>> geometric_series(0, 0, 0) [] """ From dc86c78ad4224920ed46ad3d267e0a0405409869 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 00:48:21 +0530 Subject: [PATCH 18/44] Update mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 1c2143c3eb1c..d2154429b930 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 33b097791e46f3b0fd2e3ddc9b284633104fc820 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 14:32:58 +0530 Subject: [PATCH 19/44] Update average_mode.py --- maths/average_mode.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/average_mode.py b/maths/average_mode.py index dc0efbcb8c8c..c12d8ec14939 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -5,7 +5,9 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." """This function returns the mode(Mode as in the measures of central tendency) of the input data. + The input list may contain any Datastructure or any Datatype. + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] >>> mode(input_list) [2] From 9bbace15ec368c862faad82302cdd6c317747579 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 14:39:47 +0530 Subject: [PATCH 20/44] Update average_mode.py --- maths/average_mode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index c12d8ec14939..1cac5ce9ca63 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -5,9 +5,9 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." """This function returns the mode(Mode as in the measures of central tendency) of the input data. - + The input list may contain any Datastructure or any Datatype. - + >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] >>> mode(input_list) [2] From bbfcc6e35d310b974c837715f9b581559c281413 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 17:25:48 +0530 Subject: [PATCH 21/44] Update average_mode.py --- maths/average_mode.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 1cac5ce9ca63..4a9a99c92b16 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -8,20 +8,15 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." The input list may contain any Datastructure or any Datatype. - >>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2] - >>> mode(input_list) + >>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]) [2] - >>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2] - >>> mode(input_list) + >>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]) [2] - >>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2] - >>> mode(input_list) + >>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]) [2, 4] - >>> input_list = ["x", "y", "y", "z"] - >>> mode(input_list) + >>> mode(["x", "y", "y", "z"]) ['y'] - >>> input_list = ["x", "x" , "y", "y", "z"] - >>> mode(input_list) + >>> mode(["x", "x" , "y", "y", "z"]) ['x', 'y'] """ counts: list[int] = list((Counter(input_list)).values()) From dbc81e963abeb35506aa740a0fea722474a2c152 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 17:27:07 +0530 Subject: [PATCH 22/44] Update mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index d2154429b930..0922ece0ff0e 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,5 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 289c0297da93160533c045eb76d09f32408573d4 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 17:33:11 +0530 Subject: [PATCH 23/44] Update mypy.ini --- mypy.ini | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mypy.ini b/mypy.ini index 0922ece0ff0e..36c614207571 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,5 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) - +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From 9f7c657242f9f01dfb032f255591e2819a6a5cca Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 17:34:45 +0530 Subject: [PATCH 24/44] Update mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index 36c614207571..df69fa841cef 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/fischer_yates_shuffle.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From d3841991c9665a92c7d7527b8fa63b995c61035b Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 18:46:16 +0530 Subject: [PATCH 25/44] Update average_mode.py --- maths/average_mode.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 4a9a99c92b16..7a52e9e3f535 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -1,4 +1,3 @@ -from collections import Counter from typing import Any @@ -19,10 +18,15 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." >>> mode(["x", "x" , "y", "y", "z"]) ['x', 'y'] """ - counts: list[int] = list((Counter(input_list)).values()) - return list( - sorted({val for val in input_list if input_list.count(val) == max(counts)}) - ) + result = list() # Empty list to store the counts of elements in input_list + for x in input_list: + result.append(input_list.count(x)) + if not result: + return [] + y = max(result) # Gets the maximum value in the result list. + # Gets values of modes + result = list({input_list[i] for i, value in enumerate(result) if value == y}) + return sorted(result) if __name__ == "__main__": From db95b6402df909dde82a237dcb35326b1cd25083 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 18:56:41 +0530 Subject: [PATCH 26/44] Update proth_number.py --- maths/proth_number.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/maths/proth_number.py b/maths/proth_number.py index b05b4fdea0db..a930aac883e4 100644 --- a/maths/proth_number.py +++ b/maths/proth_number.py @@ -43,11 +43,11 @@ def proth(number: int) -> int: +1 to start the sequence at the 3rd Proth number Hence, we have a +2 in the below statement """ - block_index: int = int(math.log(number // 3, 2)) + 2 + block_index = int(math.log(number // 3, 2)) + 2 - proth_list: list[int] = [3, 5] - proth_index: int = 2 - increment: int = 3 + proth_list = [3, 5] + proth_index = 2 + increment = 3 for block in range(1, block_index): for move in range(increment): proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1]) @@ -58,6 +58,12 @@ def proth(number: int) -> int: if __name__ == "__main__": - import doctest + for number in range(11): + value = 0 + try: + value = proth(number) + except ValueError: + print(f"ValueError: there is no {number}th Proth number") + continue - doctest.testmod() + print(f"The {number}th Proth number: {value}") From ae56fe963b10697fb498701ec4536f30aad43087 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:05:59 +0530 Subject: [PATCH 27/44] Update average_mode.py --- maths/average_mode.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index 7a52e9e3f535..e87a4b274009 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -18,15 +18,12 @@ def mode(input_list: list) -> list[Any]: # Defining function "mode." >>> mode(["x", "x" , "y", "y", "z"]) ['x', 'y'] """ - result = list() # Empty list to store the counts of elements in input_list - for x in input_list: - result.append(input_list.count(x)) - if not result: + if not input_list: return [] - y = max(result) # Gets the maximum value in the result list. + result = [input_list.count(value) for value in input_list] + y = max(result) # Gets the maximum count in the input list. # Gets values of modes - result = list({input_list[i] for i, value in enumerate(result) if value == y}) - return sorted(result) + return sorted({input_list[i] for i, value in enumerate(result) if value == y}) if __name__ == "__main__": From b610e7f83971d62b42e860ea7f37c1a2d84c22bd Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:09:32 +0530 Subject: [PATCH 28/44] Update gamma_recursive.py --- maths/gamma_recursive.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py index 8d30299a2f17..3d6b8c5e8138 100644 --- a/maths/gamma_recursive.py +++ b/maths/gamma_recursive.py @@ -2,7 +2,6 @@ Gamma function is a very useful tool in math and physics. It helps calculating complex integral in a convenient way. for more info: https://en.wikipedia.org/wiki/Gamma_function - Python's Standard Library math.gamma() function overflows around gamma(171.624). """ from math import pi, sqrt @@ -71,3 +70,8 @@ def test_gamma() -> None: from doctest import testmod testmod() + num = 1.0 + while num: + num = float(input("Gamma of: ")) + print(f"gamma({num}) = {gamma(num)}") + print("\nEnter 0 to exit...") From 882a12ce71057b0ba31d9359d9efb6aa5f0ae46b Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:13:15 +0530 Subject: [PATCH 29/44] Update proth_number.py --- maths/proth_number.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/maths/proth_number.py b/maths/proth_number.py index a930aac883e4..c06ccfac6d2b 100644 --- a/maths/proth_number.py +++ b/maths/proth_number.py @@ -58,6 +58,9 @@ def proth(number: int) -> int: if __name__ == "__main__": + import doctest + doctest.testmod() + for number in range(11): value = 0 try: From d112ce4b82be68ec16e85beb8e42e8945775721a Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 7 Nov 2021 15:00:23 +0100 Subject: [PATCH 30/44] Update mypy.ini --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index afeafacba5a6..e11af70d7713 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) From dd1ff0a0176292e4ff09cc04e90a8425eb63b1c4 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:39:06 +0530 Subject: [PATCH 31/44] Update geometric_series.py --- maths/series/geometric_series.py | 40 ++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index 03f90338efd3..214b226f318d 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -11,8 +11,10 @@ def geometric_series( - nth_term: float, start_term_a: float, common_ratio_r: float -) -> list[float]: + nth_term: float | int, + start_term_a: float | int, + common_ratio_r: float | int, +) -> list[float | int]: """ Pure Python implementation of Geometric Series algorithm @@ -21,28 +23,27 @@ def geometric_series( :param common_ratio_r : The common ratio between all the terms :return: The Geometric Series starting from first term a and multiple of common ration with first term with increase in power till last term (nth term) - Examples: - >>> geometric_series(4.0, 2.0, 2.0) - [2.0, 4.0, 8.0, 16.0] + >>> geometric_series(4, 2, 2) + [2, 4.0, 8.0, 16.0] >>> geometric_series(4.0, 2.0, 2.0) [2.0, 4.0, 8.0, 16.0] >>> geometric_series(4.1, 2.1, 2.1) [2.1, 4.41, 9.261000000000001, 19.448100000000004] - >>> geometric_series(4.0, 2.0, -2.0) - [2.0, -4.0, 8.0, -16.0] - >>> geometric_series(4.0, -2.0, 2.0) - [-2.0, -4.0, -8.0, -16.0] - >>> geometric_series(-4.0, 2.0, 2.0) + >>> geometric_series(4, 2, -2) + [2, -4.0, 8.0, -16.0] + >>> geometric_series(4, -2, 2) + [-2, -4.0, -8.0, -16.0] + >>> geometric_series(-4, 2, 2) [] - >>> geometric_series(0, 100.0, 500.0) + >>> geometric_series(0, 100, 500) [] - >>> geometric_series(1.0, 1.0, 1.0) - [1.0] + >>> geometric_series(1, 1, 1) + [1] >>> geometric_series(0, 0, 0) [] """ - if 0 in (nth_term, start_term_a, common_ratio_r): + if not all((nth_term, start_term_a, common_ratio_r)): return [] series: list[float] = [] power: int = 1 @@ -59,5 +60,14 @@ def geometric_series( if __name__ == "__main__": import doctest - doctest.testmod() + + nth_term = float(input("Enter the last number (n term) of the Geometric Series")) + start_term_a = float(input("Enter the starting term (a) of the Geometric Series")) + common_ratio_r = float( + input( + "Enter the common ratio between two terms (r) of the Geometric Series" + ) + ) + print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") + print(geometric_series(nth_term, start_term_a, common_ratio_r)) From c416d7400ed5b17eae93c2ffc6345cfafc93cb4b Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:39:51 +0530 Subject: [PATCH 32/44] Update average_mode.py --- maths/average_mode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/average_mode.py b/maths/average_mode.py index e87a4b274009..40f88f41f8ca 100644 --- a/maths/average_mode.py +++ b/maths/average_mode.py @@ -1,7 +1,7 @@ from typing import Any -def mode(input_list: list) -> list[Any]: # Defining function "mode." +def mode(input_list: list) -> list[Any]: """This function returns the mode(Mode as in the measures of central tendency) of the input data. From a9d9c1e7219ae38202cc54dd9d68dbe0828de95e Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:43:24 +0530 Subject: [PATCH 33/44] Update proth_number.py --- maths/proth_number.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/proth_number.py b/maths/proth_number.py index c06ccfac6d2b..e175031435b0 100644 --- a/maths/proth_number.py +++ b/maths/proth_number.py @@ -59,6 +59,7 @@ def proth(number: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() for number in range(11): From 85b0ba87b73cca65bb621a0a1051ace6f7f20202 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:45:23 +0530 Subject: [PATCH 34/44] Update geometric_series.py --- maths/series/geometric_series.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index 214b226f318d..b0b1cb3185ba 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -45,8 +45,8 @@ def geometric_series( """ if not all((nth_term, start_term_a, common_ratio_r)): return [] - series: list[float] = [] - power: int = 1 + series = [] + power = 1 multiple = common_ratio_r for _ in range(int(nth_term)): if series == []: @@ -60,14 +60,13 @@ def geometric_series( if __name__ == "__main__": import doctest + doctest.testmod() nth_term = float(input("Enter the last number (n term) of the Geometric Series")) start_term_a = float(input("Enter the starting term (a) of the Geometric Series")) common_ratio_r = float( - input( - "Enter the common ratio between two terms (r) of the Geometric Series" - ) + input("Enter the common ratio between two terms (r) of the Geometric Series") ) print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") print(geometric_series(nth_term, start_term_a, common_ratio_r)) From 507882107471d8ff87bdaf87f8230bd5e5e27c99 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:51:52 +0530 Subject: [PATCH 35/44] Update geometric_series.py --- maths/series/geometric_series.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index b0b1cb3185ba..09c555fc0c6f 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -10,11 +10,14 @@ """ +from typing import Union + + def geometric_series( - nth_term: float | int, - start_term_a: float | int, - common_ratio_r: float | int, -) -> list[float | int]: + nth_term: Union[float, int], + start_term_a: Union[float, int], + common_ratio_r: Union[float, int], +) -> list[Union[float, int]]: """ Pure Python implementation of Geometric Series algorithm @@ -45,7 +48,7 @@ def geometric_series( """ if not all((nth_term, start_term_a, common_ratio_r)): return [] - series = [] + series: list[Union[float, int]] = [] power = 1 multiple = common_ratio_r for _ in range(int(nth_term)): @@ -62,11 +65,3 @@ def geometric_series( import doctest doctest.testmod() - - nth_term = float(input("Enter the last number (n term) of the Geometric Series")) - start_term_a = float(input("Enter the starting term (a) of the Geometric Series")) - common_ratio_r = float( - input("Enter the common ratio between two terms (r) of the Geometric Series") - ) - print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") - print(geometric_series(nth_term, start_term_a, common_ratio_r)) From 6bf7b4cd288838bf1d50f731324fe5df9ea41063 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 19:57:40 +0530 Subject: [PATCH 36/44] Update geometric_series.py --- maths/series/geometric_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index 09c555fc0c6f..33973b5dda11 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -56,7 +56,7 @@ def geometric_series( series.append(start_term_a) else: power += 1 - series.append(start_term_a * multiple) + series.append(float(start_term_a * multiple)) multiple = pow(float(common_ratio_r), power) return series From 8da52be122daa14017ce5e353ec27f05f74950ff Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:10:29 +0530 Subject: [PATCH 37/44] Update p_series.py --- maths/series/p_series.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 23a3eb3a4024..d93cbbcc2c9f 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -10,25 +10,36 @@ """ -def p_series(nth_term: int, power: int) -> list[float]: +from typing import Union + + +def p_series( + nth_term: Union[int, float, str], power: Union[int, float, str] +) -> Union[list[str], str]: """ Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term Examples: >>> p_series(5, 2) - [1.0, 0.25, 0.1111111111111111, 0.0625, 0.04] + ['1', '1/4', '1/9', '1/16', '1/25'] >>> p_series(-5, 2) [] >>> p_series(5, -2) - [1.0, 4.0, 9.0, 16.0, 25.0] + ['1', '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] + >>> p_series("", 1000) + '' >>> p_series(0, 0) [] >>> p_series(1, 1) - [1.0] + ['1'] """ - series: list[float] = [] - for temp in range(nth_term): - series.append(1 / (pow(temp + 1, power)) if series else 1.0) + if nth_term == "": + return '' + nth_term = int(nth_term) + power = int(power) + series: list[str] = [] + for temp in range(int(nth_term)): + series.append(f"1/{pow(temp + 1, int(power))}" if series else '1') return series @@ -36,3 +47,8 @@ def p_series(nth_term: int, power: int) -> list[float]: import doctest doctest.testmod() + + nth_term = int(input("Enter the last number (nth term) of the P-Series")) + power = int(input("Enter the power for P-Series")) + print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p") + print(p_series(nth_term, power)) From db0178072b78a236e86d45d6001cd4540a2488ed Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:14:42 +0530 Subject: [PATCH 38/44] Update geometric_series.py --- maths/series/geometric_series.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index 33973b5dda11..c02c1193dba3 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -10,14 +10,14 @@ """ -from typing import Union +from __future__ import annotations def geometric_series( - nth_term: Union[float, int], - start_term_a: Union[float, int], - common_ratio_r: Union[float, int], -) -> list[Union[float, int]]: + nth_term: float | int, + start_term_a: float | int, + common_ratio_r: float | int, +) -> list[float | int]: """ Pure Python implementation of Geometric Series algorithm @@ -48,7 +48,7 @@ def geometric_series( """ if not all((nth_term, start_term_a, common_ratio_r)): return [] - series: list[Union[float, int]] = [] + series: list[float | int] = [] power = 1 multiple = common_ratio_r for _ in range(int(nth_term)): @@ -65,3 +65,11 @@ def geometric_series( import doctest doctest.testmod() + + nth_term = input("Enter the last number (n term) of the Geometric Series") + start_term_a = input("Enter the starting term (a) of the Geometric Series") + common_ratio_r = input( + "Enter the common ratio between two terms (r) of the Geometric Series" + ) + print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") + print(geometric_series(nth_term, start_term_a, common_ratio_r)) From 2e5cafbb90f6c20a8e284d8935089d8be9105f66 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:16:43 +0530 Subject: [PATCH 39/44] Update p_series.py --- maths/series/p_series.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index d93cbbcc2c9f..bc9b01c5664c 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -10,12 +10,12 @@ """ -from typing import Union +from __future__ import annotations def p_series( - nth_term: Union[int, float, str], power: Union[int, float, str] -) -> Union[list[str], str]: + nth_term: int | float | str, power: int | float | str +) -> list[str] | str: """ Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term @@ -39,7 +39,7 @@ def p_series( power = int(power) series: list[str] = [] for temp in range(int(nth_term)): - series.append(f"1/{pow(temp + 1, int(power))}" if series else '1') + series.append(f"1 / {pow(temp + 1, int(power))}" if series else '1') return series From 7508c253bb32b57ca5c931554f016852e68fd2ab Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:19:34 +0530 Subject: [PATCH 40/44] Update p_series.py --- maths/series/p_series.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index bc9b01c5664c..08a57375e993 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -15,7 +15,7 @@ def p_series( nth_term: int | float | str, power: int | float | str -) -> list[str] | str: +) -> list[str]: """ Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term @@ -27,19 +27,19 @@ def p_series( >>> p_series(5, -2) ['1', '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] >>> p_series("", 1000) - '' + [''] >>> p_series(0, 0) [] >>> p_series(1, 1) ['1'] """ if nth_term == "": - return '' + return [''] nth_term = int(nth_term) power = int(power) series: list[str] = [] for temp in range(int(nth_term)): - series.append(f"1 / {pow(temp + 1, int(power))}" if series else '1') + series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1") return series From e8cb83385ed7f1a6f508ec774ffa7a8154b8a458 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:24:49 +0530 Subject: [PATCH 41/44] Update geometric_series.py --- maths/series/geometric_series.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index c02c1193dba3..a875ab89a0c5 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -66,10 +66,10 @@ def geometric_series( doctest.testmod() - nth_term = input("Enter the last number (n term) of the Geometric Series") - start_term_a = input("Enter the starting term (a) of the Geometric Series") - common_ratio_r = input( - "Enter the common ratio between two terms (r) of the Geometric Series" + nth_term = float(input("Enter the last number (n term) of the Geometric Series")) + start_term_a = float(input("Enter the starting term (a) of the Geometric Series")) + common_ratio_r = float( + input("Enter the common ratio between two terms (r) of the Geometric Series") ) print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n") print(geometric_series(nth_term, start_term_a, common_ratio_r)) From bdb576d08f4924a9e037f518f0ed9a7f2e34f552 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:26:05 +0530 Subject: [PATCH 42/44] Update p_series.py --- maths/series/p_series.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 08a57375e993..4eb25746bfbc 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -13,9 +13,7 @@ from __future__ import annotations -def p_series( - nth_term: int | float | str, power: int | float | str -) -> list[str]: +def p_series(nth_term: int | float | str, power: int | float | str) -> list[str]: """ Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term @@ -34,7 +32,7 @@ def p_series( ['1'] """ if nth_term == "": - return [''] + return [""] nth_term = int(nth_term) power = int(power) series: list[str] = [] From a362ea5d0032af4456ff79ed36cd69f1cf0249a2 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Sun, 7 Nov 2021 20:31:13 +0530 Subject: [PATCH 43/44] Update p_series.py --- maths/series/p_series.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 4eb25746bfbc..34fa3f2399af 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -19,11 +19,11 @@ def p_series(nth_term: int | float | str, power: int | float | str) -> list[str] :return: The P-Series starting from 1 to last (nth) term Examples: >>> p_series(5, 2) - ['1', '1/4', '1/9', '1/16', '1/25'] + ['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25'] >>> p_series(-5, 2) [] >>> p_series(5, -2) - ['1', '1/0.25', '1/0.1111111111111111', '1/0.0625', '1/0.04'] + ['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04'] >>> p_series("", 1000) [''] >>> p_series(0, 0) From f4597b5b6838ac4acf4626ac7b01f82e513963db Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 7 Nov 2021 16:10:22 +0100 Subject: [PATCH 44/44] Remove data_structures/stacks/next_greater_element.py| --- mypy.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy.ini b/mypy.ini index e11af70d7713..df69fa841cef 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py) +exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)