From 421ace81edb0d9af3a173f4ca7e66cc900078c1d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 29 Aug 2023 15:18:10 +0200 Subject: [PATCH] [pre-commit.ci] pre-commit autoupdate (#9013) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.285 → v0.0.286](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.285...v0.0.286) - [github.com/tox-dev/pyproject-fmt: 0.13.1 → 1.1.0](https://github.com/tox-dev/pyproject-fmt/compare/0.13.1...1.1.0) * updating DIRECTORY.md * Fis ruff rules PIE808,PLR1714 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss --- .pre-commit-config.yaml | 4 ++-- DIRECTORY.md | 1 - arithmetic_analysis/jacobi_iteration_method.py | 4 ++-- arithmetic_analysis/secant_method.py | 2 +- backtracking/hamiltonian_cycle.py | 2 +- backtracking/sudoku.py | 2 +- bit_manipulation/reverse_bits.py | 2 +- ciphers/trafid_cipher.py | 2 +- data_structures/binary_tree/lazy_segment_tree.py | 6 +++--- data_structures/linked_list/circular_linked_list.py | 2 +- data_structures/linked_list/doubly_linked_list.py | 6 +++--- data_structures/linked_list/is_palindrome.py | 2 +- data_structures/linked_list/singly_linked_list.py | 8 ++++---- data_structures/stacks/stock_span_problem.py | 2 +- digital_image_processing/filters/bilateral_filter.py | 4 ++-- digital_image_processing/filters/convolve.py | 4 ++-- .../filters/local_binary_pattern.py | 4 ++-- .../test_digital_image_processing.py | 4 ++-- divide_and_conquer/strassen_matrix_multiplication.py | 4 ++-- dynamic_programming/floyd_warshall.py | 10 +++++----- hashes/chaos_machine.py | 2 +- hashes/hamming_code.py | 4 ++-- hashes/sha1.py | 2 +- hashes/sha256.py | 2 +- machine_learning/gradient_descent.py | 2 +- machine_learning/linear_regression.py | 4 ++-- machine_learning/lstm/lstm_prediction.py | 4 ++-- maths/entropy.py | 2 +- maths/eulers_totient.py | 2 +- maths/greedy_coin_change.py | 2 +- maths/persistence.py | 4 ++-- maths/series/harmonic.py | 2 +- matrix/spiral_print.py | 2 +- other/magicdiamondpattern.py | 6 +++--- project_euler/problem_070/sol1.py | 2 +- project_euler/problem_112/sol1.py | 2 +- quantum/q_full_adder.py | 2 +- scheduling/highest_response_ratio_next.py | 6 +++--- sorts/counting_sort.py | 2 +- sorts/cycle_sort.py | 2 +- sorts/double_sort.py | 4 ++-- sorts/odd_even_transposition_parallel.py | 4 ++-- strings/rabin_karp.py | 2 +- 43 files changed, 70 insertions(+), 71 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ad3e0cd87..5c4e8579e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,7 @@ repos: - id: auto-walrus - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.285 + rev: v0.0.286 hooks: - id: ruff @@ -33,7 +33,7 @@ repos: - tomli - repo: https://github.com/tox-dev/pyproject-fmt - rev: "0.13.1" + rev: "1.1.0" hooks: - id: pyproject-fmt diff --git a/DIRECTORY.md b/DIRECTORY.md index ebb164d04..43da91cb8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -245,7 +245,6 @@ * Stacks * [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py) * [Dijkstras Two Stack Algorithm](data_structures/stacks/dijkstras_two_stack_algorithm.py) - * [Evaluate Postfix Notations](data_structures/stacks/evaluate_postfix_notations.py) * [Infix To Postfix Conversion](data_structures/stacks/infix_to_postfix_conversion.py) * [Infix To Prefix Conversion](data_structures/stacks/infix_to_prefix_conversion.py) * [Next Greater Element](data_structures/stacks/next_greater_element.py) diff --git a/arithmetic_analysis/jacobi_iteration_method.py b/arithmetic_analysis/jacobi_iteration_method.py index 17edf4bf4..dba8a9ff4 100644 --- a/arithmetic_analysis/jacobi_iteration_method.py +++ b/arithmetic_analysis/jacobi_iteration_method.py @@ -152,9 +152,9 @@ def strictly_diagonally_dominant(table: NDArray[float64]) -> bool: is_diagonally_dominant = True - for i in range(0, rows): + for i in range(rows): total = 0 - for j in range(0, cols - 1): + for j in range(cols - 1): if i == j: continue else: diff --git a/arithmetic_analysis/secant_method.py b/arithmetic_analysis/secant_method.py index d28a46206..d39cb0ff3 100644 --- a/arithmetic_analysis/secant_method.py +++ b/arithmetic_analysis/secant_method.py @@ -20,7 +20,7 @@ def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float """ x0 = lower_bound x1 = upper_bound - for _ in range(0, repeats): + for _ in range(repeats): x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0)) return x1 diff --git a/backtracking/hamiltonian_cycle.py b/backtracking/hamiltonian_cycle.py index 4a4156d70..e9916f83f 100644 --- a/backtracking/hamiltonian_cycle.py +++ b/backtracking/hamiltonian_cycle.py @@ -95,7 +95,7 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) return graph[path[curr_ind - 1]][path[0]] == 1 # Recursive Step - for next_ver in range(0, len(graph)): + for next_ver in range(len(graph)): if valid_connection(graph, next_ver, curr_ind, path): # Insert current vertex into path as next transition path[curr_ind] = next_ver diff --git a/backtracking/sudoku.py b/backtracking/sudoku.py index 698dedcc2..6e4e3e878 100644 --- a/backtracking/sudoku.py +++ b/backtracking/sudoku.py @@ -48,7 +48,7 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool: is found) else returns True if it is 'safe' """ for i in range(9): - if grid[row][i] == n or grid[i][column] == n: + if n in {grid[row][i], grid[i][column]}: return False for i in range(3): diff --git a/bit_manipulation/reverse_bits.py b/bit_manipulation/reverse_bits.py index a8c77c11b..74b4f2563 100644 --- a/bit_manipulation/reverse_bits.py +++ b/bit_manipulation/reverse_bits.py @@ -20,7 +20,7 @@ def get_reverse_bit_string(number: int) -> str: ) raise TypeError(msg) bit_string = "" - for _ in range(0, 32): + for _ in range(32): bit_string += str(number % 2) number = number >> 1 return bit_string diff --git a/ciphers/trafid_cipher.py b/ciphers/trafid_cipher.py index 108ac652f..8aa2263ca 100644 --- a/ciphers/trafid_cipher.py +++ b/ciphers/trafid_cipher.py @@ -119,7 +119,7 @@ def decrypt_message( for i in range(0, len(message) + 1, period): a, b, c = __decrypt_part(message[i : i + period], character_to_number) - for j in range(0, len(a)): + for j in range(len(a)): decrypted_numeric.append(a[j] + b[j] + c[j]) for each in decrypted_numeric: diff --git a/data_structures/binary_tree/lazy_segment_tree.py b/data_structures/binary_tree/lazy_segment_tree.py index 050dfe0a6..c26b06193 100644 --- a/data_structures/binary_tree/lazy_segment_tree.py +++ b/data_structures/binary_tree/lazy_segment_tree.py @@ -7,10 +7,10 @@ class SegmentTree: def __init__(self, size: int) -> None: self.size = size # approximate the overall size of segment tree with given value - self.segment_tree = [0 for i in range(0, 4 * size)] + self.segment_tree = [0 for i in range(4 * size)] # create array to store lazy update - self.lazy = [0 for i in range(0, 4 * size)] - self.flag = [0 for i in range(0, 4 * size)] # flag for lazy update + self.lazy = [0 for i in range(4 * size)] + self.flag = [0 for i in range(4 * size)] # flag for lazy update def left(self, idx: int) -> int: """ diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 325d91026..d9544f426 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -125,7 +125,7 @@ def test_circular_linked_list() -> None: circular_linked_list.insert_tail(6) assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7)) circular_linked_list.insert_head(0) - assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7)) + assert str(circular_linked_list) == "->".join(str(i) for i in range(7)) assert circular_linked_list.delete_front() == 0 assert circular_linked_list.delete_tail() == 6 diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index 1a6c48191..bd3445f9f 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -98,7 +98,7 @@ class DoublyLinkedList: self.tail = new_node else: temp = self.head - for _ in range(0, index): + for _ in range(index): temp = temp.next temp.previous.next = new_node new_node.previous = temp.previous @@ -149,7 +149,7 @@ class DoublyLinkedList: self.tail.next = None else: temp = self.head - for _ in range(0, index): + for _ in range(index): temp = temp.next delete_node = temp temp.next.previous = temp.previous @@ -215,7 +215,7 @@ def test_doubly_linked_list() -> None: linked_list.insert_at_head(0) linked_list.insert_at_tail(11) - assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) + assert str(linked_list) == "->".join(str(i) for i in range(12)) assert linked_list.delete_head() == 0 assert linked_list.delete_at_nth(9) == 10 diff --git a/data_structures/linked_list/is_palindrome.py b/data_structures/linked_list/is_palindrome.py index ec19e99f7..d540fb69f 100644 --- a/data_structures/linked_list/is_palindrome.py +++ b/data_structures/linked_list/is_palindrome.py @@ -68,7 +68,7 @@ def is_palindrome_dict(head): middle += 1 else: step = 0 - for i in range(0, len(v)): + for i in range(len(v)): if v[i] + v[len(v) - 1 - step] != checksum: return False step += 1 diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 890e21c9b..f4b2ddce1 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -370,7 +370,7 @@ def test_singly_linked_list() -> None: linked_list.insert_head(0) linked_list.insert_tail(11) - assert str(linked_list) == "->".join(str(i) for i in range(0, 12)) + assert str(linked_list) == "->".join(str(i) for i in range(12)) assert linked_list.delete_head() == 0 assert linked_list.delete_nth(9) == 10 @@ -378,11 +378,11 @@ def test_singly_linked_list() -> None: assert len(linked_list) == 9 assert str(linked_list) == "->".join(str(i) for i in range(1, 10)) - assert all(linked_list[i] == i + 1 for i in range(0, 9)) is True + assert all(linked_list[i] == i + 1 for i in range(9)) is True - for i in range(0, 9): + for i in range(9): linked_list[i] = -i - assert all(linked_list[i] == -i for i in range(0, 9)) is True + assert all(linked_list[i] == -i for i in range(9)) is True linked_list.reverse() assert str(linked_list) == "->".join(str(i) for i in range(-8, 1)) diff --git a/data_structures/stacks/stock_span_problem.py b/data_structures/stacks/stock_span_problem.py index de423c1eb..5efe58d25 100644 --- a/data_structures/stacks/stock_span_problem.py +++ b/data_structures/stacks/stock_span_problem.py @@ -36,7 +36,7 @@ def calculation_span(price, s): # A utility function to print elements of array def print_array(arr, n): - for i in range(0, n): + for i in range(n): print(arr[i], end=" ") diff --git a/digital_image_processing/filters/bilateral_filter.py b/digital_image_processing/filters/bilateral_filter.py index 565da73f6..199ac4d99 100644 --- a/digital_image_processing/filters/bilateral_filter.py +++ b/digital_image_processing/filters/bilateral_filter.py @@ -31,8 +31,8 @@ def get_slice(img: np.ndarray, x: int, y: int, kernel_size: int) -> np.ndarray: def get_gauss_kernel(kernel_size: int, spatial_variance: float) -> np.ndarray: # Creates a gaussian kernel of given dimension. arr = np.zeros((kernel_size, kernel_size)) - for i in range(0, kernel_size): - for j in range(0, kernel_size): + for i in range(kernel_size): + for j in range(kernel_size): arr[i, j] = math.sqrt( abs(i - kernel_size // 2) ** 2 + abs(j - kernel_size // 2) ** 2 ) diff --git a/digital_image_processing/filters/convolve.py b/digital_image_processing/filters/convolve.py index 299682010..004402f29 100644 --- a/digital_image_processing/filters/convolve.py +++ b/digital_image_processing/filters/convolve.py @@ -11,8 +11,8 @@ def im2col(image, block_size): dst_width = rows - block_size[0] + 1 image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0])) row = 0 - for i in range(0, dst_height): - for j in range(0, dst_width): + for i in range(dst_height): + for j in range(dst_width): window = ravel(image[i : i + block_size[0], j : j + block_size[1]]) image_array[row, :] = window row += 1 diff --git a/digital_image_processing/filters/local_binary_pattern.py b/digital_image_processing/filters/local_binary_pattern.py index 907fe2cb0..861369ba6 100644 --- a/digital_image_processing/filters/local_binary_pattern.py +++ b/digital_image_processing/filters/local_binary_pattern.py @@ -71,8 +71,8 @@ if __name__ == "__main__": # Iterating through the image and calculating the # local binary pattern value for each pixel. - for i in range(0, image.shape[0]): - for j in range(0, image.shape[1]): + for i in range(image.shape[0]): + for j in range(image.shape[1]): lbp_image[i][j] = local_binary_value(image, i, j) cv2.imshow("local binary pattern", lbp_image) diff --git a/digital_image_processing/test_digital_image_processing.py b/digital_image_processing/test_digital_image_processing.py index fee7ab247..528b4bc3b 100644 --- a/digital_image_processing/test_digital_image_processing.py +++ b/digital_image_processing/test_digital_image_processing.py @@ -118,8 +118,8 @@ def test_local_binary_pattern(): # Iterating through the image and calculating the local binary pattern value # for each pixel. - for i in range(0, image.shape[0]): - for j in range(0, image.shape[1]): + for i in range(image.shape[0]): + for j in range(image.shape[1]): lbp_image[i][j] = lbp.local_binary_value(image, i, j) assert lbp_image.any() diff --git a/divide_and_conquer/strassen_matrix_multiplication.py b/divide_and_conquer/strassen_matrix_multiplication.py index cbfc7e565..1d03950ef 100644 --- a/divide_and_conquer/strassen_matrix_multiplication.py +++ b/divide_and_conquer/strassen_matrix_multiplication.py @@ -131,7 +131,7 @@ def strassen(matrix1: list, matrix2: list) -> list: # Adding zeros to the matrices so that the arrays dimensions are the same and also # power of 2 - for i in range(0, maxim): + for i in range(maxim): if i < dimension1[0]: for _ in range(dimension1[1], maxim): new_matrix1[i].append(0) @@ -146,7 +146,7 @@ def strassen(matrix1: list, matrix2: list) -> list: final_matrix = actual_strassen(new_matrix1, new_matrix2) # Removing the additional zeros - for i in range(0, maxim): + for i in range(maxim): if i < dimension1[0]: for _ in range(dimension2[1], maxim): final_matrix[i].pop() diff --git a/dynamic_programming/floyd_warshall.py b/dynamic_programming/floyd_warshall.py index 614a3c72a..2331f3e65 100644 --- a/dynamic_programming/floyd_warshall.py +++ b/dynamic_programming/floyd_warshall.py @@ -5,19 +5,19 @@ class Graph: def __init__(self, n=0): # a graph with Node 0,1,...,N-1 self.n = n self.w = [ - [math.inf for j in range(0, n)] for i in range(0, n) + [math.inf for j in range(n)] for i in range(n) ] # adjacency matrix for weight self.dp = [ - [math.inf for j in range(0, n)] for i in range(0, n) + [math.inf for j in range(n)] for i in range(n) ] # dp[i][j] stores minimum distance from i to j def add_edge(self, u, v, w): self.dp[u][v] = w def floyd_warshall(self): - for k in range(0, self.n): - for i in range(0, self.n): - for j in range(0, self.n): + for k in range(self.n): + for i in range(self.n): + for j in range(self.n): self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j]) def show_min(self, u, v): diff --git a/hashes/chaos_machine.py b/hashes/chaos_machine.py index 238fdb1c0..d2fde2f5e 100644 --- a/hashes/chaos_machine.py +++ b/hashes/chaos_machine.py @@ -53,7 +53,7 @@ def pull(): key = machine_time % m # Evolution (Time Length) - for _ in range(0, t): + for _ in range(t): # Variables (Position + Parameters) r = params_space[key] value = buffer_space[key] diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index dc9303218..8498ca920 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -135,7 +135,7 @@ def emitter_converter(size_par, data): # Mount the message cont_bp = 0 # parity bit counter - for x in range(0, size_par + len(data)): + for x in range(size_par + len(data)): if data_ord[x] is None: data_out.append(str(parity[cont_bp])) cont_bp += 1 @@ -228,7 +228,7 @@ def receptor_converter(size_par, data): # Mount the message cont_bp = 0 # Parity bit counter - for x in range(0, size_par + len(data_output)): + for x in range(size_par + len(data_output)): if data_ord[x] is None: data_out.append(str(parity[cont_bp])) cont_bp += 1 diff --git a/hashes/sha1.py b/hashes/sha1.py index b325ce3e4..8a03673f3 100644 --- a/hashes/sha1.py +++ b/hashes/sha1.py @@ -97,7 +97,7 @@ class SHA1Hash: for block in self.blocks: expanded_block = self.expand_block(block) a, b, c, d, e = self.h - for i in range(0, 80): + for i in range(80): if 0 <= i < 20: f = (b & c) | ((~b) & d) k = 0x5A827999 diff --git a/hashes/sha256.py b/hashes/sha256.py index 98f7c096e..ba9aff8db 100644 --- a/hashes/sha256.py +++ b/hashes/sha256.py @@ -138,7 +138,7 @@ class SHA256: a, b, c, d, e, f, g, h = self.hashes - for index in range(0, 64): + for index in range(64): if index > 15: # modify the zero-ed indexes at the end of the array s0 = ( diff --git a/machine_learning/gradient_descent.py b/machine_learning/gradient_descent.py index 5b74dad08..9ffc02bbc 100644 --- a/machine_learning/gradient_descent.py +++ b/machine_learning/gradient_descent.py @@ -110,7 +110,7 @@ def run_gradient_descent(): while True: j += 1 temp_parameter_vector = [0, 0, 0, 0] - for i in range(0, len(parameter_vector)): + for i in range(len(parameter_vector)): cost_derivative = get_cost_derivative(i - 1) temp_parameter_vector[i] = ( parameter_vector[i] - LEARNING_RATE * cost_derivative diff --git a/machine_learning/linear_regression.py b/machine_learning/linear_regression.py index 75943ac9f..0847112ad 100644 --- a/machine_learning/linear_regression.py +++ b/machine_learning/linear_regression.py @@ -78,7 +78,7 @@ def run_linear_regression(data_x, data_y): theta = np.zeros((1, no_features)) - for i in range(0, iterations): + for i in range(iterations): theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta) error = sum_of_square_error(data_x, data_y, len_data, theta) print(f"At Iteration {i + 1} - Error is {error:.5f}") @@ -107,7 +107,7 @@ def main(): theta = run_linear_regression(data_x, data_y) len_result = theta.shape[1] print("Resultant Feature vector : ") - for i in range(0, len_result): + for i in range(len_result): print(f"{theta[0, i]:.5f}") diff --git a/machine_learning/lstm/lstm_prediction.py b/machine_learning/lstm/lstm_prediction.py index 74197c46a..16530e935 100644 --- a/machine_learning/lstm/lstm_prediction.py +++ b/machine_learning/lstm/lstm_prediction.py @@ -32,10 +32,10 @@ if __name__ == "__main__": train_x, train_y = [], [] test_x, test_y = [], [] - for i in range(0, len(train_data) - forward_days - look_back + 1): + for i in range(len(train_data) - forward_days - look_back + 1): train_x.append(train_data[i : i + look_back]) train_y.append(train_data[i + look_back : i + look_back + forward_days]) - for i in range(0, len(test_data) - forward_days - look_back + 1): + for i in range(len(test_data) - forward_days - look_back + 1): test_x.append(test_data[i : i + look_back]) test_y.append(test_data[i + look_back : i + look_back + forward_days]) x_train = np.array(train_x) diff --git a/maths/entropy.py b/maths/entropy.py index 498c28f31..23753d884 100644 --- a/maths/entropy.py +++ b/maths/entropy.py @@ -101,7 +101,7 @@ def analyze_text(text: str) -> tuple[dict, dict]: # first case when we have space at start. two_char_strings[" " + text[0]] += 1 - for i in range(0, len(text) - 1): + for i in range(len(text) - 1): single_char_strings[text[i]] += 1 two_char_strings[text[i : i + 2]] += 1 return single_char_strings, two_char_strings diff --git a/maths/eulers_totient.py b/maths/eulers_totient.py index a15664703..00f0254c2 100644 --- a/maths/eulers_totient.py +++ b/maths/eulers_totient.py @@ -21,7 +21,7 @@ def totient(n: int) -> list: for i in range(2, n + 1): if is_prime[i]: primes.append(i) - for j in range(0, len(primes)): + for j in range(len(primes)): if i * primes[j] >= n: break is_prime[i * primes[j]] = False diff --git a/maths/greedy_coin_change.py b/maths/greedy_coin_change.py index 7cf669bcb..db2c381bc 100644 --- a/maths/greedy_coin_change.py +++ b/maths/greedy_coin_change.py @@ -81,7 +81,7 @@ if __name__ == "__main__": ): n = int(input("Enter the number of denominations you want to add: ").strip()) - for i in range(0, n): + for i in range(n): denominations.append(int(input(f"Denomination {i}: ").strip())) value = input("Enter the change you want to make in Indian Currency: ").strip() else: diff --git a/maths/persistence.py b/maths/persistence.py index 607641e67..c61a69a7c 100644 --- a/maths/persistence.py +++ b/maths/persistence.py @@ -28,7 +28,7 @@ def multiplicative_persistence(num: int) -> int: numbers = [int(i) for i in num_string] total = 1 - for i in range(0, len(numbers)): + for i in range(len(numbers)): total *= numbers[i] num_string = str(total) @@ -67,7 +67,7 @@ def additive_persistence(num: int) -> int: numbers = [int(i) for i in num_string] total = 0 - for i in range(0, len(numbers)): + for i in range(len(numbers)): total += numbers[i] num_string = str(total) diff --git a/maths/series/harmonic.py b/maths/series/harmonic.py index 50f29c93d..35792d38a 100644 --- a/maths/series/harmonic.py +++ b/maths/series/harmonic.py @@ -45,7 +45,7 @@ def is_harmonic_series(series: list) -> bool: return True rec_series = [] series_len = len(series) - for i in range(0, series_len): + for i in range(series_len): if series[i] == 0: raise ValueError("Input series cannot have 0 as an element") rec_series.append(1 / series[i]) diff --git a/matrix/spiral_print.py b/matrix/spiral_print.py index 0d0be1527..5eef263f7 100644 --- a/matrix/spiral_print.py +++ b/matrix/spiral_print.py @@ -54,7 +54,7 @@ def spiral_print_clockwise(a: list[list[int]]) -> None: return # horizotal printing increasing - for i in range(0, mat_col): + for i in range(mat_col): print(a[0][i]) # vertical printing down for i in range(1, mat_row): diff --git a/other/magicdiamondpattern.py b/other/magicdiamondpattern.py index 0fc41d7a2..89b973bb4 100644 --- a/other/magicdiamondpattern.py +++ b/other/magicdiamondpattern.py @@ -7,10 +7,10 @@ def floyd(n): Parameters: n : size of pattern """ - for i in range(0, n): - for _ in range(0, n - i - 1): # printing spaces + for i in range(n): + for _ in range(n - i - 1): # printing spaces print(" ", end="") - for _ in range(0, i + 1): # printing stars + for _ in range(i + 1): # printing stars print("* ", end="") print() diff --git a/project_euler/problem_070/sol1.py b/project_euler/problem_070/sol1.py index 273f37efc..57a6c1916 100644 --- a/project_euler/problem_070/sol1.py +++ b/project_euler/problem_070/sol1.py @@ -44,7 +44,7 @@ def get_totients(max_one: int) -> list[int]: """ totients = [0] * max_one - for i in range(0, max_one): + for i in range(max_one): totients[i] = i for i in range(2, max_one): diff --git a/project_euler/problem_112/sol1.py b/project_euler/problem_112/sol1.py index b3ea6b356..31996d070 100644 --- a/project_euler/problem_112/sol1.py +++ b/project_euler/problem_112/sol1.py @@ -49,7 +49,7 @@ def check_bouncy(n: int) -> bool: raise ValueError("check_bouncy() accepts only integer arguments") str_n = str(n) sorted_str_n = "".join(sorted(str_n)) - return sorted_str_n != str_n and sorted_str_n[::-1] != str_n + return str_n not in {sorted_str_n, sorted_str_n[::-1]} def solution(percent: float = 99) -> int: diff --git a/quantum/q_full_adder.py b/quantum/q_full_adder.py index 66d931985..ec4efa434 100644 --- a/quantum/q_full_adder.py +++ b/quantum/q_full_adder.py @@ -88,7 +88,7 @@ def quantum_full_adder( quantum_circuit = qiskit.QuantumCircuit(qr, cr) - for i in range(0, 3): + for i in range(3): if entry[i] == 2: quantum_circuit.h(i) # for hadamard entries elif entry[i] == 1: diff --git a/scheduling/highest_response_ratio_next.py b/scheduling/highest_response_ratio_next.py index 9c999ec65..057bd64cc 100644 --- a/scheduling/highest_response_ratio_next.py +++ b/scheduling/highest_response_ratio_next.py @@ -53,7 +53,7 @@ def calculate_turn_around_time( loc = 0 # Saves the current response ratio. temp = 0 - for i in range(0, no_of_process): + for i in range(no_of_process): if finished_process[i] == 0 and arrival_time[i] <= current_time: temp = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[ i @@ -87,7 +87,7 @@ def calculate_waiting_time( """ waiting_time = [0] * no_of_process - for i in range(0, no_of_process): + for i in range(no_of_process): waiting_time[i] = turn_around_time[i] - burst_time[i] return waiting_time @@ -106,7 +106,7 @@ if __name__ == "__main__": ) print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time") - for i in range(0, no_of_process): + for i in range(no_of_process): print( f"{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t" f"{turn_around_time[i]}\t\t\t{waiting_time[i]}" diff --git a/sorts/counting_sort.py b/sorts/counting_sort.py index 18c4b0323..256952df5 100644 --- a/sorts/counting_sort.py +++ b/sorts/counting_sort.py @@ -49,7 +49,7 @@ def counting_sort(collection): # place the elements in the output, respecting the original order (stable # sort) from end to begin, updating counting_arr - for i in reversed(range(0, coll_len)): + for i in reversed(range(coll_len)): ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i] counting_arr[collection[i] - coll_min] -= 1 diff --git a/sorts/cycle_sort.py b/sorts/cycle_sort.py index 806f40441..7177c8ea1 100644 --- a/sorts/cycle_sort.py +++ b/sorts/cycle_sort.py @@ -19,7 +19,7 @@ def cycle_sort(array: list) -> list: [] """ array_len = len(array) - for cycle_start in range(0, array_len - 1): + for cycle_start in range(array_len - 1): item = array[cycle_start] pos = cycle_start diff --git a/sorts/double_sort.py b/sorts/double_sort.py index 5ca88a674..a19641d94 100644 --- a/sorts/double_sort.py +++ b/sorts/double_sort.py @@ -16,9 +16,9 @@ def double_sort(lst): """ no_of_elements = len(lst) for _ in range( - 0, int(((no_of_elements - 1) / 2) + 1) + int(((no_of_elements - 1) / 2) + 1) ): # we don't need to traverse to end of list as - for j in range(0, no_of_elements - 1): + for j in range(no_of_elements - 1): if ( lst[j + 1] < lst[j] ): # applying bubble sort algorithm from left to right (or forwards) diff --git a/sorts/odd_even_transposition_parallel.py b/sorts/odd_even_transposition_parallel.py index 87b0e4d1e..9e0d228bd 100644 --- a/sorts/odd_even_transposition_parallel.py +++ b/sorts/odd_even_transposition_parallel.py @@ -33,7 +33,7 @@ def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe): # we perform n swaps since after n swaps we know we are sorted # we *could* stop early if we are sorted already, but it takes as long to # find out we are sorted as it does to sort the list with this algorithm - for i in range(0, 10): + for i in range(10): if (i + position) % 2 == 0 and r_send is not None: # send your value to your right neighbor process_lock.acquire() @@ -123,7 +123,7 @@ def odd_even_transposition(arr): p.start() # wait for the processes to end and write their values to the list - for p in range(0, len(result_pipe)): + for p in range(len(result_pipe)): arr[p] = result_pipe[p][0].recv() process_array_[p].join() return arr diff --git a/strings/rabin_karp.py b/strings/rabin_karp.py index 81ca611a7..532c689f8 100644 --- a/strings/rabin_karp.py +++ b/strings/rabin_karp.py @@ -38,7 +38,7 @@ def rabin_karp(pattern: str, text: str) -> bool: continue modulus_power = (modulus_power * alphabet_size) % modulus - for i in range(0, t_len - p_len + 1): + for i in range(t_len - p_len + 1): if text_hash == p_hash and text[i : i + p_len] == pattern: return True if i == t_len - p_len: