diff --git a/maths/greedy_coin_change.py b/maths/greedy_coin_change.py index 5a7d9e8d84ae..31e102494989 100644 --- a/maths/greedy_coin_change.py +++ b/maths/greedy_coin_change.py @@ -74,8 +74,8 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: # Driver Code if __name__ == "__main__": - denominations = list() - value = 0 + denominations: list[int] = list() + value: int = 0 if ( input("Do you want to enter your denominations ? (yY/n): ").strip().lower() @@ -85,13 +85,13 @@ def find_minimum_change(denominations: list[int], value: int) -> list[int]: for i in range(0, n): denominations.append(int(input(f"Denomination {i}: ").strip())) - value = input("Enter the change you want to make in Indian Currency: ").strip() + value = int(input("Enter the change you want to make in Indian Currency: ").strip()) else: # All denominations of Indian Currency if user does not enter denominations = [1, 2, 5, 10, 20, 50, 100, 500, 2000] - value = input("Enter the change you want to make: ").strip() + value = int(input("Enter the change you want to make: ").strip()) - if int(value) == 0 or int(value) < 0: + if value <= 0: print("The total value cannot be zero or negative.") else: diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index d12382e6d8c4..2e518db018cb 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -11,7 +11,7 @@ """ -def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list: +def geometric_series(nth_term: int, 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 @@ -20,15 +20,13 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l 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'] + [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'] + [2.0, 4.0, 8.0, 16.0] >>> geometric_series(4, 2, -2) - [2, '-4.0', '8.0', '-16.0'] + [2, -4.0, 8.0, -16.0] >>> geometric_series(4, -2, 2) - [-2, '-4.0', '-8.0', '-16.0'] + [-2, -4.0, -8.0, -16.0] >>> geometric_series(-4, 2, 2) [] >>> geometric_series(0, 100, 500) @@ -38,26 +36,17 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l >>> geometric_series(0, 0, 0) [] """ - if "" in (nth_term, start_term_a, common_ratio_r): - return "" series = [] - power = 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))) - multiple = pow(float(common_ratio_r), power) + for power in range(nth_term): + series.append(start_term_a * pow(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" - ) + nth_term = int(input("Enter the last number (n term) of the Geometric Series: ")) + start_term_a = int(input("Enter the starting term (a) of the Geometric Series: ")) + common_ratio_r = int(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)) diff --git a/maths/series/harmonic_series.py b/maths/series/harmonic_series.py index 91b5944583e4..48abaf3801a0 100644 --- a/maths/series/harmonic_series.py +++ b/maths/series/harmonic_series.py @@ -10,9 +10,10 @@ For manual testing run: python3 harmonic_series.py """ +from fractions import Fraction -def harmonic_series(n_term: str) -> list: +def harmonic_series(n_term: int) -> list[Fraction]: """Pure Python implementation of Harmonic Series algorithm :param n_term: The last (nth) term of Harmonic Series @@ -32,15 +33,13 @@ def harmonic_series(n_term: str) -> list: >>> harmonic_series(1) ['1'] """ - if n_term == "": - return n_term series = [] for temp in range(int(n_term)): - series.append(f"1/{temp + 1}" if series else "1") + series.append(Fraction(1, temp + 1)) return series if __name__ == "__main__": - nth_term = input("Enter the last number (nth term) of the Harmonic Series") + nth_term = int(input("Enter the last number (nth term) of the Harmonic Series: ")) print("Formula of Harmonic Series => 1+1/2+1/3 ..... 1/n") - print(harmonic_series(nth_term)) + print([str(i) for i in harmonic_series(nth_term)]) diff --git a/maths/series/p_series.py b/maths/series/p_series.py index 04019aed5a85..9fa60940b577 100644 --- a/maths/series/p_series.py +++ b/maths/series/p_series.py @@ -10,9 +10,10 @@ For manual testing run: python3 p_series.py """ +from fractions import Fraction -def p_series(nth_term: int, power: int) -> list: +def p_series(nth_term: int, power: int) -> list[Fraction]: """Pure Python implementation of P-Series algorithm :return: The P-Series starting from 1 to last (nth) term @@ -24,25 +25,19 @@ def p_series(nth_term: int, power: int) -> list: [] >>> 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 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) + for temp in range(nth_term): + series.append(Fraction(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") + 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)) + print([str(i) for i in p_series(nth_term, power)]) diff --git a/maths/sigmoid.py b/maths/sigmoid.py index 147588e8871f..cb45bde2702c 100644 --- a/maths/sigmoid.py +++ b/maths/sigmoid.py @@ -11,7 +11,7 @@ import numpy as np -def sigmoid(vector: np.array) -> np.array: +def sigmoid(vector: np.ndarray) -> np.ndarray: """ Implements the sigmoid function diff --git a/maths/triplet_sum.py b/maths/triplet_sum.py index 22fab17d30c2..df0d67ea7225 100644 --- a/maths/triplet_sum.py +++ b/maths/triplet_sum.py @@ -5,7 +5,7 @@ """ from __future__ import annotations -from itertools import permutations +from itertools import combinations from random import randint from timeit import repeat @@ -32,9 +32,9 @@ def triplet_sum1(arr: list[int], target: int) -> tuple[int, int, int]: >>> triplet_sum1(arr, target) (0, 0, 0) """ - for triplet in permutations(arr, 3): + for triplet in combinations(sorted(arr), 3): if sum(triplet) == target: - return tuple(sorted(triplet)) + return triplet return (0, 0, 0) diff --git a/maths/two_sum.py b/maths/two_sum.py index 5209acbc7e44..c9ec570ee5c3 100644 --- a/maths/two_sum.py +++ b/maths/two_sum.py @@ -31,7 +31,7 @@ def two_sum(nums: list[int], target: int) -> list[int]: >>> two_sum([3 * i for i in range(10)], 19) [] """ - chk_map = {} + chk_map: dict = {} for index, val in enumerate(nums): compl = target - val if compl in chk_map: diff --git a/mypy.ini b/mypy.ini index 9eec22e22717..e33507cb09c1 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True ; FIXME: #4052 fix mypy errors in the exclude directories and remove them below -exclude = (data_structures|dynamic_programming|graphs|maths|matrix|other|project_euler|searches|strings*)/$ +exclude = (data_structures|dynamic_programming|graphs|matrix|other|project_euler|searches|strings*)/$