From 61547f465ef72fcc479f0cb98783e7e7fd402053 Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Mon, 10 Oct 2022 14:52:08 +0200 Subject: [PATCH 01/10] Fix a `__name__ == "__main__"` mistake --- digital_image_processing/filters/local_binary_pattern.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index e73aa59bfa53..e92e554a3e5f 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( From 8f3fa346b20fde007403e40b569c7adbe42a6bfe Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Tue, 11 Oct 2022 17:35:01 +0200 Subject: [PATCH 02/10] Misc fixes @dhruvmanila told me to commit these here so here we go --- compression/huffman.py | 2 +- data_structures/linked_list/is_palindrome.py | 2 +- data_structures/stacks/stack.py | 3 ++- fuzzy_logic/fuzzy_operations.py | 5 +---- graphs/dijkstra_algorithm.py | 4 ++-- ...irected_and_undirected_(weighted)_graph.py | 11 ++++------- hashes/hamming_code.py | 4 +++- linear_algebra/src/test_linear_algebra.py | 2 +- maths/extended_euclidean_algorithm.py | 2 +- maths/jaccard_similarity.py | 19 +++++++++++-------- matrix/matrix_class.py | 2 +- other/lfu_cache.py | 3 ++- other/lru_cache.py | 3 ++- project_euler/problem_001/sol7.py | 4 +--- project_euler/problem_042/solution42.py | 13 ++++++------- project_euler/problem_067/sol1.py | 4 ++-- project_euler/problem_089/sol1.py | 5 +++-- project_euler/problem_099/sol1.py | 3 ++- scheduling/first_come_first_served.py | 6 ++++-- scheduling/multi_level_feedback_queue.py | 4 +++- web_programming/emails_from_url.py | 4 +++- 21 files changed, 56 insertions(+), 49 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index d5d78b753c3f..f619ed82c764 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 acc87c1c272b..ec19e99f78c0 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/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index d1c73df43067..af1ec0b7e5af 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -110,7 +110,8 @@ def test_stack() -> None: assert not stack.is_empty() assert stack.is_full() assert str(stack) == str(list(range(10))) - assert stack.pop() == 9 + test = stack.pop() + assert test == 9 assert stack.peek() == 8 stack.push(100) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index fbaca9421327..4478d6c4568e 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -9,10 +9,7 @@ """ 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 6b64834acd81..9abc719918cf 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -89,13 +89,13 @@ def add_edge(self, u, v, w): # 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 5cfa9e13edd9..2341e9f6b55e 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -225,10 +225,9 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: + # FIXME: there used to be unreachable code here. + # Code removed because of linter complaints. 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]) @@ -453,11 +452,9 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: + # FIXME: there used to be unreachable code here. + # Code removed because of linter complaints. 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 ac20fe03b3fb..c436af76d258 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -80,7 +80,9 @@ def emitterConverter(sizePar, data): """ if sizePar + len(data) <= 2**sizePar - (len(data) - 1): print("ERROR - size of parity don't match with size of data") - exit(0) + import sys + + sys.exit(0) dataOut = [] parity = [] diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index 724ceef2599a..49196d678a63 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -89,7 +89,7 @@ def test_zeroVector(self) -> None: """ 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_unitBasisVector(self) -> None: """ diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index 72afd40aa707..61e1e244982a 100644 --- a/maths/extended_euclidean_algorithm.py +++ b/maths/extended_euclidean_algorithm.py @@ -75,7 +75,7 @@ def main(): """Call Extended Euclidean Algorithm.""" if len(sys.argv) < 3: print("2 integer arguments required") - exit(1) + sys.exit(1) a = int(sys.argv[1]) b = int(sys.argv[2]) print(extended_euclidean_algorithm(a, b)) diff --git a/maths/jaccard_similarity.py b/maths/jaccard_similarity.py index 4f24d308f340..ea706b2b1f7f 100644 --- a/maths/jaccard_similarity.py +++ b/maths/jaccard_similarity.py @@ -14,7 +14,7 @@ """ -def jaccard_similariy(setA, setB, alternativeUnion=False): +def jaccard_similarity(setA, setB, alternativeUnion=False): """ Finds the jaccard similarity between two sets. Essentially, its intersection over union. @@ -35,21 +35,24 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): Examples: >>> setA = {'a', 'b', 'c', 'd', 'e'} >>> setB = {'c', 'd', 'e', 'f', 'h', 'i'} - >>> jaccard_similariy(setA,setB) + >>> jaccard_similarity(setA,setB) 0.375 - >>> jaccard_similariy(setA,setA) + >>> jaccard_similarity(setA,setA) 1.0 - >>> jaccard_similariy(setA,setA,True) + >>> jaccard_similarity(setA,setA,True) 0.5 >>> setA = ['a', 'b', 'c', 'd', 'e'] >>> setB = ('c', 'd', 'e', 'f', 'h', 'i') - >>> jaccard_similariy(setA,setB) + >>> jaccard_similarity(setA,setB) 0.375 """ + if len(setA) == 0 or len(setB) == 0: + raise ValueError("Input must not be empty") + if isinstance(setA, set) and isinstance(setB, set): intersection = len(setA.intersection(setB)) @@ -67,14 +70,14 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): if alternativeUnion: union = len(setA) + len(setB) + return len(intersection) / union else: union = setA + [element for element in setB if element not in setA] - - return len(intersection) / len(union) + return len(intersection) / len(union) if __name__ == "__main__": setA = {"a", "b", "c", "d", "e"} setB = {"c", "d", "e", "f", "h", "i"} - print(jaccard_similariy(setA, setB)) + print(jaccard_similarity(setA, setB)) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index 305cad0a5a9c..0bac966fff70 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -286,7 +286,7 @@ def add_column(self, column: list[int], position: int | None = None) -> None: # 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 False return self.rows == other.rows def __ne__(self, other: object) -> bool: diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 072d00ab58c8..d30e67c99c68 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -264,7 +264,8 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None - assert self.list.remove(first_node) is not None + remove_test = self.list.remove(first_node) + assert remove_test is not None # first_node guaranteed to be in list del self.cache[first_node.key] diff --git a/other/lru_cache.py b/other/lru_cache.py index b68ae0a8e296..1c11f3b38d7d 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -280,8 +280,9 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None + test_removal = self.list.remove(first_node) assert ( - self.list.remove(first_node) is not None + test_removal is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py index 8f5d1977fdde..6ada70c12dbd 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 b3aecf4cf144..17eb27c3beb0 100644 --- a/project_euler/problem_042/solution42.py +++ b/project_euler/problem_042/solution42.py @@ -33,13 +33,12 @@ def solution(): with open(wordsFilePath) as f: words = f.readline() - words = list(map(lambda word: word.strip('"'), words.strip("\r\n").split(","))) - words = list( - filter( - lambda word: word in TRIANGULAR_NUMBERS, - map(lambda word: sum(map(lambda x: ord(x) - 64, word)), words), - ) - ) + words = [word.strip('"') for word in words.strip("\r\n").split(",")] + 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 527d4dc592ac..174e697f0bab 100644 --- a/project_euler/problem_067/sol1.py +++ b/project_euler/problem_067/sol1.py @@ -28,8 +28,8 @@ def solution(): with open(triangle) as f: triangle = f.readlines() - a = map(lambda x: x.rstrip("\r\n").split(" "), triangle) - a = list(map(lambda x: list(map(int, x)), a)) + a = [x.rstrip("\r\n").split(" ") for x in triangle] + a = [[int(i) for i in x] for x in a] 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 1c4e2600f847..83609cd236e1 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/project_euler/problem_099/sol1.py b/project_euler/problem_099/sol1.py index bf5621c6583c..2ddbdd95921c 100644 --- a/project_euler/problem_099/sol1.py +++ b/project_euler/problem_099/sol1.py @@ -25,7 +25,8 @@ def solution(data_file: str = "base_exp.txt") -> int: largest: float = 0 result = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): - a, x = list(map(int, line.split(","))) + a, x = line.split(",") + a, x = int(a), int(x) if x * log10(a) > largest: largest = x * log10(a) result = i + 1 diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index c5f61720f97e..d9e233bd3d48 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -73,13 +73,15 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: if __name__ == "__main__": + import sys + # process id's processes = [1, 2, 3] # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") - exit() + sys.exit() # duration time of all processes duration_times = [19, 8, 9] @@ -87,7 +89,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: # 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() + sys.exit() # 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 b54cc8719039..e773e31e27ce 100644 --- a/scheduling/multi_level_feedback_queue.py +++ b/scheduling/multi_level_feedback_queue.py @@ -276,7 +276,9 @@ def multi_level_feedback_queue(self) -> deque[Process]: queue = deque([P1, P2, P3, P4]) if len(time_slices) != number_of_queues - 1: - exit() + import sys + + sys.exit() 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 afaee5bbe854..e5e1a49371c8 100644 --- a/web_programming/emails_from_url.py +++ b/web_programming/emails_from_url.py @@ -93,7 +93,9 @@ def emails_from_url(url: str = "https://github.com") -> list[str]: except ValueError: pass except ValueError: - exit(-1) + import sys + + sys.exit(-1) # Finally return a sorted list of email addresses with no duplicates. return sorted(valid_emails) From 424ba28448e3f1e9e7d781f5b13b819263319813 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 15:36:47 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 4478d6c4568e..0786ef8b0c67 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -8,7 +8,6 @@ - 3.5 """ import numpy as np - import skfuzzy as fuzz if __name__ == "__main__": From 0d115a6dbbbd91ccbaa9a2ddddbb59bb85d1683e Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Tue, 11 Oct 2022 17:35:01 +0200 Subject: [PATCH 04/10] Misc fixes @dhruvmanila told me to commit these here so here we go Fixes #6916 among other issues --- compression/huffman.py | 2 +- data_structures/linked_list/is_palindrome.py | 2 +- data_structures/stacks/stack.py | 3 ++- fuzzy_logic/fuzzy_operations.py | 3 ++- graphs/dijkstra_algorithm.py | 4 ++-- ...irected_and_undirected_(weighted)_graph.py | 11 ++++------- hashes/hamming_code.py | 4 +++- linear_algebra/src/test_linear_algebra.py | 2 +- maths/extended_euclidean_algorithm.py | 2 +- maths/jaccard_similarity.py | 19 +++++++++++-------- matrix/matrix_class.py | 2 +- other/lfu_cache.py | 3 ++- other/lru_cache.py | 3 ++- project_euler/problem_001/sol7.py | 4 +--- project_euler/problem_042/solution42.py | 13 ++++++------- project_euler/problem_067/sol1.py | 4 ++-- project_euler/problem_089/sol1.py | 5 +++-- project_euler/problem_099/sol1.py | 3 ++- scheduling/first_come_first_served.py | 6 ++++-- scheduling/multi_level_feedback_queue.py | 4 +++- web_programming/emails_from_url.py | 4 +++- 21 files changed, 57 insertions(+), 46 deletions(-) diff --git a/compression/huffman.py b/compression/huffman.py index d5d78b753c3f..f619ed82c764 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 acc87c1c272b..ec19e99f78c0 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/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index d1c73df43067..af1ec0b7e5af 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -110,7 +110,8 @@ def test_stack() -> None: assert not stack.is_empty() assert stack.is_full() assert str(stack) == str(list(range(10))) - assert stack.pop() == 9 + test = stack.pop() + assert test == 9 assert stack.peek() == 8 stack.push(100) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index fbaca9421327..05b14d6a2bf5 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -12,7 +12,8 @@ try: import skfuzzy as fuzz except ImportError: - fuzz = None + import sys + sys.exit() # This is so the CI doesn't complain about an unknown library if __name__ == "__main__": # Create universe of discourse in Python using linspace () diff --git a/graphs/dijkstra_algorithm.py b/graphs/dijkstra_algorithm.py index 6b64834acd81..9abc719918cf 100644 --- a/graphs/dijkstra_algorithm.py +++ b/graphs/dijkstra_algorithm.py @@ -89,13 +89,13 @@ def add_edge(self, u, v, w): # 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 5cfa9e13edd9..2341e9f6b55e 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -225,10 +225,9 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: + # FIXME: there used to be unreachable code here. + # Code removed because of linter complaints. 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]) @@ -453,11 +452,9 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: + # FIXME: there used to be unreachable code here. + # Code removed because of linter complaints. 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 ac20fe03b3fb..c436af76d258 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -80,7 +80,9 @@ def emitterConverter(sizePar, data): """ if sizePar + len(data) <= 2**sizePar - (len(data) - 1): print("ERROR - size of parity don't match with size of data") - exit(0) + import sys + + sys.exit(0) dataOut = [] parity = [] diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index 724ceef2599a..49196d678a63 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -89,7 +89,7 @@ def test_zeroVector(self) -> None: """ 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_unitBasisVector(self) -> None: """ diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index 72afd40aa707..61e1e244982a 100644 --- a/maths/extended_euclidean_algorithm.py +++ b/maths/extended_euclidean_algorithm.py @@ -75,7 +75,7 @@ def main(): """Call Extended Euclidean Algorithm.""" if len(sys.argv) < 3: print("2 integer arguments required") - exit(1) + sys.exit(1) a = int(sys.argv[1]) b = int(sys.argv[2]) print(extended_euclidean_algorithm(a, b)) diff --git a/maths/jaccard_similarity.py b/maths/jaccard_similarity.py index 4f24d308f340..ea706b2b1f7f 100644 --- a/maths/jaccard_similarity.py +++ b/maths/jaccard_similarity.py @@ -14,7 +14,7 @@ """ -def jaccard_similariy(setA, setB, alternativeUnion=False): +def jaccard_similarity(setA, setB, alternativeUnion=False): """ Finds the jaccard similarity between two sets. Essentially, its intersection over union. @@ -35,21 +35,24 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): Examples: >>> setA = {'a', 'b', 'c', 'd', 'e'} >>> setB = {'c', 'd', 'e', 'f', 'h', 'i'} - >>> jaccard_similariy(setA,setB) + >>> jaccard_similarity(setA,setB) 0.375 - >>> jaccard_similariy(setA,setA) + >>> jaccard_similarity(setA,setA) 1.0 - >>> jaccard_similariy(setA,setA,True) + >>> jaccard_similarity(setA,setA,True) 0.5 >>> setA = ['a', 'b', 'c', 'd', 'e'] >>> setB = ('c', 'd', 'e', 'f', 'h', 'i') - >>> jaccard_similariy(setA,setB) + >>> jaccard_similarity(setA,setB) 0.375 """ + if len(setA) == 0 or len(setB) == 0: + raise ValueError("Input must not be empty") + if isinstance(setA, set) and isinstance(setB, set): intersection = len(setA.intersection(setB)) @@ -67,14 +70,14 @@ def jaccard_similariy(setA, setB, alternativeUnion=False): if alternativeUnion: union = len(setA) + len(setB) + return len(intersection) / union else: union = setA + [element for element in setB if element not in setA] - - return len(intersection) / len(union) + return len(intersection) / len(union) if __name__ == "__main__": setA = {"a", "b", "c", "d", "e"} setB = {"c", "d", "e", "f", "h", "i"} - print(jaccard_similariy(setA, setB)) + print(jaccard_similarity(setA, setB)) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index 305cad0a5a9c..0bac966fff70 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -286,7 +286,7 @@ def add_column(self, column: list[int], position: int | None = None) -> None: # 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 False return self.rows == other.rows def __ne__(self, other: object) -> bool: diff --git a/other/lfu_cache.py b/other/lfu_cache.py index 072d00ab58c8..d30e67c99c68 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -264,7 +264,8 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None - assert self.list.remove(first_node) is not None + remove_test = self.list.remove(first_node) + assert remove_test is not None # first_node guaranteed to be in list del self.cache[first_node.key] diff --git a/other/lru_cache.py b/other/lru_cache.py index b68ae0a8e296..1c11f3b38d7d 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -280,8 +280,9 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None + test_removal = self.list.remove(first_node) assert ( - self.list.remove(first_node) is not None + test_removal is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] diff --git a/project_euler/problem_001/sol7.py b/project_euler/problem_001/sol7.py index 8f5d1977fdde..6ada70c12dbd 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 b3aecf4cf144..17eb27c3beb0 100644 --- a/project_euler/problem_042/solution42.py +++ b/project_euler/problem_042/solution42.py @@ -33,13 +33,12 @@ def solution(): with open(wordsFilePath) as f: words = f.readline() - words = list(map(lambda word: word.strip('"'), words.strip("\r\n").split(","))) - words = list( - filter( - lambda word: word in TRIANGULAR_NUMBERS, - map(lambda word: sum(map(lambda x: ord(x) - 64, word)), words), - ) - ) + words = [word.strip('"') for word in words.strip("\r\n").split(",")] + 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 527d4dc592ac..174e697f0bab 100644 --- a/project_euler/problem_067/sol1.py +++ b/project_euler/problem_067/sol1.py @@ -28,8 +28,8 @@ def solution(): with open(triangle) as f: triangle = f.readlines() - a = map(lambda x: x.rstrip("\r\n").split(" "), triangle) - a = list(map(lambda x: list(map(int, x)), a)) + a = [x.rstrip("\r\n").split(" ") for x in triangle] + a = [[int(i) for i in x] for x in a] 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 1c4e2600f847..83609cd236e1 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/project_euler/problem_099/sol1.py b/project_euler/problem_099/sol1.py index bf5621c6583c..2ddbdd95921c 100644 --- a/project_euler/problem_099/sol1.py +++ b/project_euler/problem_099/sol1.py @@ -25,7 +25,8 @@ def solution(data_file: str = "base_exp.txt") -> int: largest: float = 0 result = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): - a, x = list(map(int, line.split(","))) + a, x = line.split(",") + a, x = int(a), int(x) if x * log10(a) > largest: largest = x * log10(a) result = i + 1 diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index c5f61720f97e..d9e233bd3d48 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -73,13 +73,15 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: if __name__ == "__main__": + import sys + # process id's processes = [1, 2, 3] # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") - exit() + sys.exit() # duration time of all processes duration_times = [19, 8, 9] @@ -87,7 +89,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: # 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() + sys.exit() # 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 b54cc8719039..e773e31e27ce 100644 --- a/scheduling/multi_level_feedback_queue.py +++ b/scheduling/multi_level_feedback_queue.py @@ -276,7 +276,9 @@ def multi_level_feedback_queue(self) -> deque[Process]: queue = deque([P1, P2, P3, P4]) if len(time_slices) != number_of_queues - 1: - exit() + import sys + + sys.exit() 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 afaee5bbe854..e5e1a49371c8 100644 --- a/web_programming/emails_from_url.py +++ b/web_programming/emails_from_url.py @@ -93,7 +93,9 @@ def emails_from_url(url: str = "https://github.com") -> list[str]: except ValueError: pass except ValueError: - exit(-1) + import sys + + sys.exit(-1) # Finally return a sorted list of email addresses with no duplicates. return sorted(valid_emails) From a9cf1435c5c63b3d1841cb6585be62f2055725cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Oct 2022 15:54:05 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 05b14d6a2bf5..8ef3114cfd01 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -13,7 +13,8 @@ import skfuzzy as fuzz except ImportError: import sys - sys.exit() # This is so the CI doesn't complain about an unknown library + + sys.exit() # This is so the CI doesn't complain about an unknown library if __name__ == "__main__": # Create universe of discourse in Python using linspace () From 8fe73117f9f5fb790a23a2ade3ebc710d66bc3fd Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Thu, 13 Oct 2022 13:58:55 +0200 Subject: [PATCH 06/10] Make the requested changes Requested by @dhruvmanila --- data_structures/stacks/stack.py | 3 +-- fuzzy_logic/fuzzy_operations.py | 7 +------ graphs/directed_and_undirected_(weighted)_graph.py | 4 ---- hashes/hamming_code.py | 5 +---- maths/extended_euclidean_algorithm.py | 5 +++-- matrix/matrix_class.py | 2 +- other/lfu_cache.py | 3 +-- other/lru_cache.py | 3 +-- project_euler/problem_067/sol1.py | 8 ++++++-- project_euler/problem_099/sol1.py | 3 +-- scheduling/first_come_first_served.py | 6 ++---- scheduling/multi_level_feedback_queue.py | 4 +--- web_programming/emails_from_url.py | 4 +--- 13 files changed, 20 insertions(+), 37 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index af1ec0b7e5af..d1c73df43067 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -110,8 +110,7 @@ def test_stack() -> None: assert not stack.is_empty() assert stack.is_full() assert str(stack) == str(list(range(10))) - test = stack.pop() - assert test == 9 + assert stack.pop() == 9 assert stack.peek() == 8 stack.push(100) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 8ef3114cfd01..4478d6c4568e 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -9,12 +9,7 @@ """ import numpy as np -try: - import skfuzzy as fuzz -except ImportError: - import sys - - sys.exit() # This is so the CI doesn't complain about an unknown library +import skfuzzy as fuzz if __name__ == "__main__": # Create universe of discourse in Python using linspace () diff --git a/graphs/directed_and_undirected_(weighted)_graph.py b/graphs/directed_and_undirected_(weighted)_graph.py index 2341e9f6b55e..43a72b89e3a7 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -225,8 +225,6 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: - # FIXME: there used to be unreachable code here. - # Code removed because of linter complaints. return True if visited.count(node[1]) < 1: stack.append(node[1]) @@ -452,8 +450,6 @@ def has_cycle(self): anticipating_nodes.add(node[1]) break else: - # FIXME: there used to be unreachable code here. - # Code removed because of linter complaints. return True if visited.count(node[1]) < 1: stack.append(node[1]) diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index c436af76d258..15b0ec154382 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -79,10 +79,7 @@ def emitterConverter(sizePar, data): ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1'] """ if sizePar + len(data) <= 2**sizePar - (len(data) - 1): - print("ERROR - size of parity don't match with size of data") - import sys - - sys.exit(0) + raise ValueError("ERROR - size of parity don't match with size of data") dataOut = [] parity = [] diff --git a/maths/extended_euclidean_algorithm.py b/maths/extended_euclidean_algorithm.py index 61e1e244982a..c54909e19101 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") - sys.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/matrix/matrix_class.py b/matrix/matrix_class.py index 0bac966fff70..9dd6807e9ff9 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -286,7 +286,7 @@ def add_column(self, column: list[int], position: int | None = None) -> None: # MATRIX OPERATIONS def __eq__(self, other: object) -> bool: if not isinstance(other, Matrix): - return False + return NotImplemented return self.rows == other.rows def __ne__(self, other: object) -> bool: diff --git a/other/lfu_cache.py b/other/lfu_cache.py index d30e67c99c68..072d00ab58c8 100644 --- a/other/lfu_cache.py +++ b/other/lfu_cache.py @@ -264,8 +264,7 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None - remove_test = self.list.remove(first_node) - assert remove_test is not None + assert self.list.remove(first_node) is not None # first_node guaranteed to be in list del self.cache[first_node.key] diff --git a/other/lru_cache.py b/other/lru_cache.py index 1c11f3b38d7d..b68ae0a8e296 100644 --- a/other/lru_cache.py +++ b/other/lru_cache.py @@ -280,9 +280,8 @@ def set(self, key: T, value: U) -> None: # explain to type checker via assertions assert first_node is not None assert first_node.key is not None - test_removal = self.list.remove(first_node) assert ( - test_removal is not None + self.list.remove(first_node) is not None ) # node guaranteed to be in list assert node.key is not None del self.cache[first_node.key] diff --git a/project_euler/problem_067/sol1.py b/project_euler/problem_067/sol1.py index 174e697f0bab..f20c206cca11 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 = [[int(i) for i in 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_099/sol1.py b/project_euler/problem_099/sol1.py index 2ddbdd95921c..bf5621c6583c 100644 --- a/project_euler/problem_099/sol1.py +++ b/project_euler/problem_099/sol1.py @@ -25,8 +25,7 @@ def solution(data_file: str = "base_exp.txt") -> int: largest: float = 0 result = 0 for i, line in enumerate(open(os.path.join(os.path.dirname(__file__), data_file))): - a, x = line.split(",") - a, x = int(a), int(x) + a, x = list(map(int, line.split(","))) if x * log10(a) > largest: largest = x * log10(a) result = i + 1 diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index d9e233bd3d48..eaf3feec1ef0 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -73,15 +73,13 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: if __name__ == "__main__": - import sys - # process id's processes = [1, 2, 3] # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") - sys.exit() + raise SystemExit # duration time of all processes duration_times = [19, 8, 9] @@ -89,7 +87,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: # 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") - sys.exit() + raise SystemExit # 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 e773e31e27ce..a87b31c6239f 100644 --- a/scheduling/multi_level_feedback_queue.py +++ b/scheduling/multi_level_feedback_queue.py @@ -276,9 +276,7 @@ def multi_level_feedback_queue(self) -> deque[Process]: queue = deque([P1, P2, P3, P4]) if len(time_slices) != number_of_queues - 1: - import sys - - sys.exit() + raise SystemExit 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 e5e1a49371c8..03e5c9ed404b 100644 --- a/web_programming/emails_from_url.py +++ b/web_programming/emails_from_url.py @@ -93,9 +93,7 @@ def emails_from_url(url: str = "https://github.com") -> list[str]: except ValueError: pass except ValueError: - import sys - - sys.exit(-1) + raise SystemExit # Finally return a sorted list of email addresses with no duplicates. return sorted(valid_emails) From 4a7189b487d80f507527a4088afdc1bf35fff703 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 12:09:07 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- fuzzy_logic/fuzzy_operations.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fuzzy_logic/fuzzy_operations.py b/fuzzy_logic/fuzzy_operations.py index 4478d6c4568e..0786ef8b0c67 100644 --- a/fuzzy_logic/fuzzy_operations.py +++ b/fuzzy_logic/fuzzy_operations.py @@ -8,7 +8,6 @@ - 3.5 """ import numpy as np - import skfuzzy as fuzz if __name__ == "__main__": From ed0c72438cc91a082c24269fa1c8c258c270a38c Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Thu, 13 Oct 2022 19:21:08 +0530 Subject: [PATCH 08/10] Apply suggestions from code review --- hashes/hamming_code.py | 2 +- scheduling/first_come_first_served.py | 4 ++-- scheduling/multi_level_feedback_queue.py | 2 +- web_programming/emails_from_url.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index 4bd7a713439a..dff7ced6558d 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -79,7 +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 sizePar + len(data) <= 2**sizePar - (len(data) - 1): - raise ValueError("ERROR - size of parity don't match with size of data") + raise ValueError("size of parity don't match with size of data") data_out = [] parity = [] diff --git a/scheduling/first_come_first_served.py b/scheduling/first_come_first_served.py index eaf3feec1ef0..06cdb8ddf821 100644 --- a/scheduling/first_come_first_served.py +++ b/scheduling/first_come_first_served.py @@ -79,7 +79,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: # ensure that we actually have processes if len(processes) == 0: print("Zero amount of processes") - raise SystemExit + raise SystemExit(0) # duration time of all processes duration_times = [19, 8, 9] @@ -87,7 +87,7 @@ def calculate_average_waiting_time(waiting_times: list[int]) -> float: # 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") - raise SystemExit + 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 a87b31c6239f..1af4cbdce843 100644 --- a/scheduling/multi_level_feedback_queue.py +++ b/scheduling/multi_level_feedback_queue.py @@ -276,7 +276,7 @@ def multi_level_feedback_queue(self) -> deque[Process]: queue = deque([P1, P2, P3, P4]) if len(time_slices) != number_of_queues - 1: - raise SystemExit + 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 03e5c9ed404b..074ef878c0d7 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: - raise SystemExit + raise SystemExit(1) # Finally return a sorted list of email addresses with no duplicates. return sorted(valid_emails) From 1f2366d807f33941616a6803d4886ddebeab1ce8 Mon Sep 17 00:00:00 2001 From: CenTdemeern1 Date: Sat, 15 Oct 2022 21:42:08 +0200 Subject: [PATCH 09/10] Fix issue caused by the mass variable name rename --- hashes/hamming_code.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index dff7ced6558d..481a6750773a 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -78,7 +78,7 @@ def emitter_converter(size_par, data): >>> emitter_converter(4, "101010111111") ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1'] """ - if sizePar + len(data) <= 2**sizePar - (len(data) - 1): + if size_par + len(data) <= 2**size_par - (len(data) - 1): raise ValueError("size of parity don't match with size of data") data_out = [] From a037145988f5634706e0a57db686de91ef1d95bd Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Sun, 16 Oct 2022 10:49:29 +0530 Subject: [PATCH 10/10] build: add scikit-fuzzy to requirements --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 0fbc1cc4b45c..b14a3eb0157c 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