From 04698538d816fc5f70c850e8b89c6d1f5599fa84 Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Sat, 15 Oct 2022 22:25:38 -0700 Subject: [PATCH] Misc fixes across multiple algorithms (#6912) Source: Snyk code quality Add scikit-fuzzy to requirements Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Dhruv Manilawala --- compression/huffman.py | 2 +- data_structures/linked_list/is_palindrome.py | 2 +- .../filters/local_binary_pattern.py | 2 +- fuzzy_logic/fuzzy_operations.py | 6 +----- graphs/dijkstra_algorithm.py | 4 ++-- .../directed_and_undirected_(weighted)_graph.py | 7 ------- hashes/hamming_code.py | 3 +-- linear_algebra/src/test_linear_algebra.py | 2 +- maths/extended_euclidean_algorithm.py | 5 +++-- maths/jaccard_similarity.py | 15 ++++++++------- matrix/matrix_class.py | 2 +- project_euler/problem_001/sol7.py | 4 +--- project_euler/problem_042/solution42.py | 11 +++++------ project_euler/problem_067/sol1.py | 8 ++++++-- project_euler/problem_089/sol1.py | 5 +++-- requirements.txt | 2 +- scheduling/first_come_first_served.py | 4 ++-- scheduling/multi_level_feedback_queue.py | 2 +- web_programming/emails_from_url.py | 2 +- 19 files changed, 40 insertions(+), 48 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index d5d78b753..f619ed82c 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -31,7 +31,7 @@ def parse_file(file_path: str) -> list[Letter]: c = f.read(1) if not c: break - chars[c] = chars[c] + 1 if c in chars.keys() else 1 + chars[c] = chars[c] + 1 if c in chars else 1 return sorted((Letter(c, f) for c, f in chars.items()), key=lambda l: l.freq) diff --git a/data_structures/linked_list/is_palindrome.py b/data_structures/linked_list/is_palindrome.py index acc87c1c2..ec19e99f7 100644 --- a/data_structures/linked_list/is_palindrome.py +++ b/data_structures/linked_list/is_palindrome.py @@ -55,7 +55,7 @@ def is_palindrome_dict(head): d = {} pos = 0 while head: - if head.val in d.keys(): + if head.val in d: d[head.val].append(pos) else: d[head.val] = [pos] diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index e73aa59bf..e92e554a3 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -60,7 +60,7 @@ def local_binary_value(image: np.ndarray, x_coordinate: int, y_coordinate: int) ) -if __name__ == "main": +if __name__ == "__main__": # Reading the image and converting it to grayscale. image = cv2.imread( diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index fbaca9421..0786ef8b0 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -8,11 +8,7 @@ Python: - 3.5 """ import numpy as np - -try: - import skfuzzy as fuzz -except ImportError: - fuzz = None +import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () diff --git a/graphs/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py index 122821a37..1845dad05 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -89,13 +89,13 @@ class Graph: # Edge going from node u to v and v to u with weight w # u (w)-> v, v (w) -> u # Check if u already in graph - if u in self.adjList.keys(): + if u in self.adjList: self.adjList[u].append((v, w)) else: self.adjList[u] = [(v, w)] # Assuming undirected graph - if v in self.adjList.keys(): + if v in self.adjList: self.adjList[v].append((u, w)) else: self.adjList[v] = [(u, w)] diff --git a/graphs/directed_and_undirected_(weighted)_graph.py b/graphs/directed_and_undirected_(weighted)_graph.py index 5cfa9e13e..43a72b89e 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -226,9 +226,6 @@ class DirectedGraph: break else: return True - # TODO:The following code is unreachable. - anticipating_nodes.add(stack[len_stack_minus_one]) - len_stack_minus_one -= 1 if visited.count(node[1]) < 1: stack.append(node[1]) visited.append(node[1]) @@ -454,10 +451,6 @@ class Graph: break else: return True - # TODO: the following code is unreachable - # is this meant to be called in the else ? - anticipating_nodes.add(stack[len_stack_minus_one]) - len_stack_minus_one -= 1 if visited.count(node[1]) < 1: stack.append(node[1]) visited.append(node[1]) diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index a62d092a1..481a67507 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -79,8 +79,7 @@ def emitter_converter(size_par, data): ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1'] """ if size_par + len(data) <= 2**size_par - (len(data) - 1): - print("ERROR - size of parity don't match with size of data") - exit(0) + raise ValueError("size of parity don't match with size of data") data_out = [] parity = [] diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index 97c06cb44..50d079572 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -89,7 +89,7 @@ class Test(unittest.TestCase): """ test for global function zero_vector() """ - self.assertTrue(str(zero_vector(10)).count("0") == 10) + self.assertEqual(str(zero_vector(10)).count("0"), 10) def test_unit_basis_vector(self) -> None: """ diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index 72afd40aa..c54909e19 100644 --- a/maths/extended_euclidean_algorithm.py +++ b/maths/extended_euclidean_algorithm.py @@ -75,11 +75,12 @@ def main(): """Call Extended Euclidean Algorithm.""" if len(sys.argv) < 3: print("2 integer arguments required") - exit(1) + return 1 a = int(sys.argv[1]) b = int(sys.argv[2]) print(extended_euclidean_algorithm(a, b)) + return 0 if __name__ == "__main__": - main() + raise SystemExit(main()) diff --git a/maths/jaccard_similarity.py b/maths/jaccard_similarity.py index 77f4b90ea..b299a8147 100644 --- a/maths/jaccard_similarity.py +++ b/maths/jaccard_similarity.py @@ -14,7 +14,7 @@ Jaccard similarity is widely used with MinHashing. """ -def jaccard_similariy(set_a, set_b, alternative_union=False): +def jaccard_similarity(set_a, set_b, alternative_union=False): """ Finds the jaccard similarity between two sets. Essentially, its intersection over union. @@ -35,18 +35,18 @@ def jaccard_similariy(set_a, set_b, alternative_union=False): Examples: >>> set_a = {'a', 'b', 'c', 'd', 'e'} >>> set_b = {'c', 'd', 'e', 'f', 'h', 'i'} - >>> jaccard_similariy(set_a, set_b) + >>> jaccard_similarity(set_a, set_b) 0.375 - >>> jaccard_similariy(set_a, set_a) + >>> jaccard_similarity(set_a, set_a) 1.0 - >>> jaccard_similariy(set_a, set_a, True) + >>> jaccard_similarity(set_a, set_a, True) 0.5 >>> set_a = ['a', 'b', 'c', 'd', 'e'] >>> set_b = ('c', 'd', 'e', 'f', 'h', 'i') - >>> jaccard_similariy(set_a, set_b) + >>> jaccard_similarity(set_a, set_b) 0.375 """ @@ -67,14 +67,15 @@ def jaccard_similariy(set_a, set_b, alternative_union=False): if alternative_union: union = len(set_a) + len(set_b) + return len(intersection) / union else: union = set_a + [element for element in set_b if element not in set_a] + return len(intersection) / len(union) return len(intersection) / len(union) if __name__ == "__main__": - set_a = {"a", "b", "c", "d", "e"} set_b = {"c", "d", "e", "f", "h", "i"} - print(jaccard_similariy(set_a, set_b)) + print(jaccard_similarity(set_a, set_b)) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index 6495bd8fc..8b6fefa21 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -286,7 +286,7 @@ class Matrix: # MATRIX OPERATIONS def __eq__(self, other: object) -> bool: if not isinstance(other, Matrix): - raise TypeError("A Matrix can only be compared with another Matrix") + return NotImplemented return self.rows == other.rows def __ne__(self, other: object) -> bool: diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py index 8f5d1977f..6ada70c12 100644 --- a/project_euler/problem_001/sol7.py +++ b/project_euler/problem_001/sol7.py @@ -26,9 +26,7 @@ def solution(n: int = 1000) -> int: result = 0 for i in range(n): - if i % 3 == 0: - result += i - elif i % 5 == 0: + if i % 3 == 0 or i % 5 == 0: result += i return result diff --git a/project_euler/problem_042/solution42.py b/project_euler/problem_042/solution42.py index c0fb2ad50..f8a54e40e 100644 --- a/project_euler/problem_042/solution42.py +++ b/project_euler/problem_042/solution42.py @@ -34,12 +34,11 @@ def solution(): words = f.readline() words = [word.strip('"') for word in words.strip("\r\n").split(",")] - words = list( - filter( - lambda word: word in TRIANGULAR_NUMBERS, - (sum(ord(x) - 64 for x in word) for word in words), - ) - ) + words = [ + word + for word in [sum(ord(x) - 64 for x in word) for word in words] + if word in TRIANGULAR_NUMBERS + ] return len(words) diff --git a/project_euler/problem_067/sol1.py b/project_euler/problem_067/sol1.py index ab305684d..f20c206cc 100644 --- a/project_euler/problem_067/sol1.py +++ b/project_euler/problem_067/sol1.py @@ -28,8 +28,12 @@ def solution(): with open(triangle) as f: triangle = f.readlines() - a = (x.rstrip("\r\n").split(" ") for x in triangle) - a = [list(map(int, x)) for x in a] + a = [] + for line in triangle: + numbers_from_line = [] + for number in line.strip().split(" "): + numbers_from_line.append(int(number)) + a.append(numbers_from_line) for i in range(1, len(a)): for j in range(len(a[i])): diff --git a/project_euler/problem_089/sol1.py b/project_euler/problem_089/sol1.py index 1c4e2600f..83609cd23 100644 --- a/project_euler/problem_089/sol1.py +++ b/project_euler/problem_089/sol1.py @@ -125,8 +125,9 @@ def solution(roman_numerals_filename: str = "/p089_roman.txt") -> int: savings = 0 - file1 = open(os.path.dirname(__file__) + roman_numerals_filename) - lines = file1.readlines() + with open(os.path.dirname(__file__) + roman_numerals_filename) as file1: + lines = file1.readlines() + for line in lines: original = line.strip() num = parse_roman_numerals(original) diff --git a/requirements.txt b/requirements.txt index 0fbc1cc4b..b14a3eb01 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,7 @@ pandas pillow qiskit requests -# scikit-fuzzy # Causing broken builds +scikit-fuzzy sklearn statsmodels sympy diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index c5f61720f..06cdb8ddf 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -79,7 +79,7 @@ if __name__ == "__main__": # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") - exit() + raise SystemExit(0) # duration time of all processes duration_times = [19, 8, 9] @@ -87,7 +87,7 @@ if __name__ == "__main__": # ensure we can match each id to a duration time if len(duration_times) != len(processes): print("Unable to match all id's with their duration time") - exit() + raise SystemExit(0) # get the waiting times and the turnaround times waiting_times = calculate_waiting_times(duration_times) diff --git a/scheduling/multi_level_feedback_queue.py b/scheduling/multi_level_feedback_queue.py index a3ba1b340..abee3c85c 100644 --- a/scheduling/multi_level_feedback_queue.py +++ b/scheduling/multi_level_feedback_queue.py @@ -276,7 +276,7 @@ if __name__ == "__main__": queue = deque([P1, P2, P3, P4]) if len(time_slices) != number_of_queues - 1: - exit() + raise SystemExit(0) doctest.testmod(extraglobs={"queue": deque([P1, P2, P3, P4])}) diff --git a/web_programming/emails_from_url.py b/web_programming/emails_from_url.py index afaee5bbe..074ef878c 100644 --- a/web_programming/emails_from_url.py +++ b/web_programming/emails_from_url.py @@ -93,7 +93,7 @@ def emails_from_url(url: str = "https://github.com") -> list[str]: except ValueError: pass except ValueError: - exit(-1) + raise SystemExit(1) # Finally return a sorted list of email addresses with no duplicates. return sorted(valid_emails)