diff --git a/backtracking/rat_in_maze.py b/backtracking/rat_in_maze.py index 2860880db..7bde886dd 100644 --- a/backtracking/rat_in_maze.py +++ b/backtracking/rat_in_maze.py @@ -88,12 +88,12 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) solutions[i][j] = 1 return True - lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds + lower_flag = (not i < 0) and (not j < 0) # Check lower bounds upper_flag = (i < size) and (j < size) # Check upper bounds if lower_flag and upper_flag: # check for already visited and block points. - block_flag = (not (solutions[i][j])) and (not (maze[i][j])) + block_flag = (not solutions[i][j]) and (not maze[i][j]) if block_flag: # check visited solutions[i][j] = 1 diff --git a/boolean_algebra/not_gate.py b/boolean_algebra/not_gate.py index b41da602d..eb85e9e44 100644 --- a/boolean_algebra/not_gate.py +++ b/boolean_algebra/not_gate.py @@ -34,4 +34,4 @@ def test_not_gate() -> None: if __name__ == "__main__": print(not_gate(0)) - print(not_gate(1)) + print(not_gate(1)) diff --git a/cellular_automata/nagel_schrekenberg.py b/cellular_automata/nagel_schrekenberg.py index be44761ec..3fd6afca0 100644 --- a/cellular_automata/nagel_schrekenberg.py +++ b/cellular_automata/nagel_schrekenberg.py @@ -45,8 +45,7 @@ def construct_highway( highway = [[-1] * number_of_cells] # Create a highway without any car i = 0 - if initial_speed < 0: - initial_speed = 0 + initial_speed = max(initial_speed, 0) while i < number_of_cells: highway[0][i] = ( randint(0, max_speed) if random_speed else initial_speed diff --git a/ciphers/mixed_keyword_cypher.py b/ciphers/mixed_keyword_cypher.py index f55c9c428..806004faa 100644 --- a/ciphers/mixed_keyword_cypher.py +++ b/ciphers/mixed_keyword_cypher.py @@ -42,7 +42,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str: s = [] for _ in range(len_temp): s.append(temp[k]) - if not (k < 25): + if k >= 25: break k += 1 modalpha.append(s) @@ -52,7 +52,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str: k = 0 for j in range(len_temp): for m in modalpha: - if not (len(m) - 1 >= j): + if not len(m) - 1 >= j: break d[alpha[k]] = m[j] if not k < 25: diff --git a/compression/huffman.py b/compression/huffman.py index f619ed82c..b337ac3ec 100644 --- a/compression/huffman.py +++ b/compression/huffman.py @@ -56,7 +56,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]: Recursively traverse the Huffman Tree to set each Letter's bitstring dictionary, and return the list of Letters """ - if type(root) is Letter: + if isinstance(root, Letter): root.bitstring[root.letter] = bitstring return [root] treenode: TreeNode = root # type: ignore diff --git a/data_structures/heap/min_heap.py b/data_structures/heap/min_heap.py index 0403624f2..ecb187649 100644 --- a/data_structures/heap/min_heap.py +++ b/data_structures/heap/min_heap.py @@ -121,7 +121,7 @@ class MinHeap: self.sift_up(len(self.heap) - 1) def is_empty(self): - return True if len(self.heap) == 0 else False + return len(self.heap) == 0 def decrease_key(self, node, new_value): assert ( diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index fdcebfdad..c999464ce 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -10,7 +10,7 @@ from digital_image_processing import change_contrast as cc from digital_image_processing import convert_to_negative as cn from digital_image_processing import sepia as sp from digital_image_processing.dithering import burkes as bs -from digital_image_processing.edge_detection import canny as canny +from digital_image_processing.edge_detection import canny from digital_image_processing.filters import convolve as conv from digital_image_processing.filters import gaussian_filter as gg from digital_image_processing.filters import local_binary_pattern as lbp diff --git a/dynamic_programming/fizz_buzz.py b/dynamic_programming/fizz_buzz.py index dd1d21b10..e77ab3de7 100644 --- a/dynamic_programming/fizz_buzz.py +++ b/dynamic_programming/fizz_buzz.py @@ -33,10 +33,9 @@ def fizz_buzz(number: int, iterations: int) -> str: ... ValueError: iterations must be defined as integers """ - - if not type(iterations) == int: + if not isinstance(iterations, int): raise ValueError("iterations must be defined as integers") - if not type(number) == int or not number >= 1: + if not isinstance(number, int) or not number >= 1: raise ValueError( """starting number must be and integer and be more than 0""" diff --git a/dynamic_programming/max_sub_array.py b/dynamic_programming/max_sub_array.py index 42eca79a9..07717fba4 100644 --- a/dynamic_programming/max_sub_array.py +++ b/dynamic_programming/max_sub_array.py @@ -62,8 +62,7 @@ def max_sub_array(nums: list[int]) -> int: current = 0 for i in nums: current += i - if current < 0: - current = 0 + current = max(current, 0) best = max(best, current) return best diff --git a/graphs/directed_and_undirected_(weighted)_graph.py b/graphs/directed_and_undirected_(weighted)_graph.py index 43a72b89e..b29485031 100644 --- a/graphs/directed_and_undirected_(weighted)_graph.py +++ b/graphs/directed_and_undirected_(weighted)_graph.py @@ -167,7 +167,7 @@ class DirectedGraph: and not on_the_way_back ): len_stack = len(stack) - 1 - while True and len_stack >= 0: + while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1]) break @@ -220,7 +220,7 @@ class DirectedGraph: and not on_the_way_back ): len_stack_minus_one = len(stack) - 1 - while True and len_stack_minus_one >= 0: + while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1]) break @@ -392,7 +392,7 @@ class Graph: and not on_the_way_back ): len_stack = len(stack) - 1 - while True and len_stack >= 0: + while len_stack >= 0: if stack[len_stack] == node[1]: anticipating_nodes.add(node[1]) break @@ -445,7 +445,7 @@ class Graph: and not on_the_way_back ): len_stack_minus_one = len(stack) - 1 - while True and len_stack_minus_one >= 0: + while len_stack_minus_one >= 0: if stack[len_stack_minus_one] == node[1]: anticipating_nodes.add(node[1]) break diff --git a/graphs/multi_heuristic_astar.py b/graphs/multi_heuristic_astar.py index e16a98393..cd8e37b00 100644 --- a/graphs/multi_heuristic_astar.py +++ b/graphs/multi_heuristic_astar.py @@ -1,4 +1,5 @@ import heapq +import sys import numpy as np @@ -116,7 +117,7 @@ def do_something(back_pointer, goal, start): print(x, end=" ") x = back_pointer[x] print(x) - quit() + sys.exit() def valid(p: TPos): diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 775e0244a..ac0398a31 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -129,7 +129,7 @@ class Vector: input: index (0-indexed) output: the i-th component of the vector. """ - if type(i) is int and -len(self.__components) <= i < len(self.__components): + if isinstance(i, int) and -len(self.__components) <= i < len(self.__components): return self.__components[i] else: raise Exception("index out of range") diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index 66535e806..3864f6421 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -388,16 +388,10 @@ class SmoSVM: return (data - self._min) / (self._max - self._min) def _is_unbound(self, index): - if 0.0 < self.alphas[index] < self._c: - return True - else: - return False + return bool(0.0 < self.alphas[index] < self._c) def _is_support(self, index): - if self.alphas[index] > 0: - return True - else: - return False + return bool(self.alphas[index] > 0) @property def unbound(self): diff --git a/maths/find_min.py b/maths/find_min.py index 228205ed7..2eac087c6 100644 --- a/maths/find_min.py +++ b/maths/find_min.py @@ -24,8 +24,7 @@ def find_min(nums: list[int | float]) -> int | float: raise ValueError("find_min() arg is an empty sequence") min_num = nums[0] for num in nums: - if min_num > num: - min_num = num + min_num = min(min_num, num) return min_num diff --git a/maths/kadanes.py b/maths/kadanes.py index b23409e2b..c2ea53a6c 100644 --- a/maths/kadanes.py +++ b/maths/kadanes.py @@ -49,10 +49,8 @@ def kadanes(arr: list) -> int: for i in arr: max_till_element += i - if max_sum <= max_till_element: - max_sum = max_till_element - if max_till_element < 0: - max_till_element = 0 + max_sum = max(max_sum, max_till_element) + max_till_element = max(max_till_element, 0) return max_sum diff --git a/maths/largest_subarray_sum.py b/maths/largest_subarray_sum.py index 0449e72e6..90f92c712 100644 --- a/maths/largest_subarray_sum.py +++ b/maths/largest_subarray_sum.py @@ -11,10 +11,8 @@ def max_sub_array_sum(a: list, size: int = 0): max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] - if max_so_far < max_ending_here: - max_so_far = max_ending_here - if max_ending_here < 0: - max_ending_here = 0 + max_so_far = max(max_so_far, max_ending_here) + max_ending_here = max(max_ending_here, 0) return max_so_far diff --git a/maths/series/geometric_series.py b/maths/series/geometric_series.py index a875ab89a..90c9fe77b 100644 --- a/maths/series/geometric_series.py +++ b/maths/series/geometric_series.py @@ -52,7 +52,7 @@ def geometric_series( power = 1 multiple = common_ratio_r for _ in range(int(nth_term)): - if series == []: + if not series: series.append(start_term_a) else: power += 1 diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 370e38482..716ed508e 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -21,7 +21,7 @@ def bfs(graph, s, t, parent): visited[ind] = True parent[ind] = u - return True if visited[t] else False + return visited[t] def ford_fulkerson(graph, source, sink): diff --git a/networking_flow/minimum_cut.py b/networking_flow/minimum_cut.py index 33131315f..164b45f10 100644 --- a/networking_flow/minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -24,7 +24,7 @@ def bfs(graph, s, t, parent): visited[ind] = True parent[ind] = u - return True if visited[t] else False + return visited[t] def mincut(graph, source, sink): diff --git a/other/password.py b/other/password.py index f463c7564..9a6161af8 100644 --- a/other/password.py +++ b/other/password.py @@ -89,13 +89,9 @@ def is_strong_password(password: str, min_length: int = 8) -> bool: num = any(char in digits for char in password) spec_char = any(char in punctuation for char in password) - if upper and lower and num and spec_char: - return True - - else: - # Passwords should contain UPPERCASE, lowerase - # numbers, and special characters - return False + return upper and lower and num and spec_char + # Passwords should contain UPPERCASE, lowerase + # numbers, and special characters def main(): diff --git a/project_euler/problem_025/sol1.py b/project_euler/problem_025/sol1.py index c30a74a43..803464b5d 100644 --- a/project_euler/problem_025/sol1.py +++ b/project_euler/problem_025/sol1.py @@ -43,7 +43,7 @@ def fibonacci(n: int) -> int: 144 """ - if n == 1 or type(n) is not int: + if n == 1 or not isinstance(n, int): return 0 elif n == 2: return 1 diff --git a/project_euler/problem_036/sol1.py b/project_euler/problem_036/sol1.py index 425c41221..1d27356ec 100644 --- a/project_euler/problem_036/sol1.py +++ b/project_euler/problem_036/sol1.py @@ -32,7 +32,7 @@ def is_palindrome(n: int | str) -> bool: False """ n = str(n) - return True if n == n[::-1] else False + return n == n[::-1] def solution(n: int = 1000000): diff --git a/quantum/q_fourier_transform.py b/quantum/q_fourier_transform.py index d138dfb45..07a257579 100644 --- a/quantum/q_fourier_transform.py +++ b/quantum/q_fourier_transform.py @@ -55,9 +55,9 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts ... ValueError: number of qubits must be exact integer. """ - if type(number_of_qubits) == str: + if isinstance(number_of_qubits, str): raise TypeError("number of qubits must be a integer.") - if not number_of_qubits > 0: + if number_of_qubits <= 0: raise ValueError("number of qubits must be > 0.") if math.floor(number_of_qubits) != number_of_qubits: raise ValueError("number of qubits must be exact integer.") diff --git a/quantum/q_full_adder.py b/quantum/q_full_adder.py index c6d03d170..66d931985 100644 --- a/quantum/q_full_adder.py +++ b/quantum/q_full_adder.py @@ -60,7 +60,11 @@ def quantum_full_adder( ... ValueError: inputs must be less or equal to 2. """ - if (type(input_1) == str) or (type(input_2) == str) or (type(carry_in) == str): + if ( + isinstance(input_1, str) + or isinstance(input_2, str) + or isinstance(carry_in, str) + ): raise TypeError("inputs must be integers.") if (input_1 < 0) or (input_2 < 0) or (carry_in < 0): diff --git a/quantum/superdense_coding.py b/quantum/superdense_coding.py index 10ebc2d35..1087312f9 100644 --- a/quantum/superdense_coding.py +++ b/quantum/superdense_coding.py @@ -53,7 +53,7 @@ def superdense_coding(bit_1: int = 1, bit_2: int = 1) -> qiskit.result.counts.Co ... ValueError: inputs must be less or equal to 1. """ - if (type(bit_1) == str) or (type(bit_2) == str): + if isinstance(bit_1, str) or isinstance(bit_2, str): raise TypeError("inputs must be integers.") if (bit_1 < 0) or (bit_2 < 0): raise ValueError("inputs must be positive.") diff --git a/sorts/msd_radix_sort.py b/sorts/msd_radix_sort.py index 84460e47b..74ce21762 100644 --- a/sorts/msd_radix_sort.py +++ b/sorts/msd_radix_sort.py @@ -133,7 +133,7 @@ def _msd_radix_sort_inplace( j = end_index - 1 while i <= j: changed = False - if not ((list_of_ints[i] >> bit_position) & 1): + if not (list_of_ints[i] >> bit_position) & 1: # found zero at the beginning i += 1 changed = True diff --git a/strings/aho_corasick.py b/strings/aho_corasick.py index 25ed649ce..e32a4ba64 100644 --- a/strings/aho_corasick.py +++ b/strings/aho_corasick.py @@ -84,7 +84,7 @@ class Automaton: else: current_state = next_state for key in self.adlist[current_state]["output"]: - if not (key in result): + if key not in result: result[key] = [] result[key].append(i - len(key) + 1) return result