The Wayback Machine - https://web.archive.org/web/20211017081121/https://github.com/TheAlgorithms/Python/commit/cecf43d6481173e831af829da77911e1a3868a6c
Skip to content
Permalink
Browse files
Pyupgrade to Python 3.9 (#4718)
* Pyupgrade to Python 3.9

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
  • Loading branch information
cclauss and github-actions committed Sep 7, 2021
1 parent 5d5831b commit cecf43d6481173e831af829da77911e1a3868a6c
Showing with 523 additions and 530 deletions.
  1. +2 −1 DIRECTORY.md
  2. +2 −2 arithmetic_analysis/in_static_equilibrium.py
  3. +2 −2 arithmetic_analysis/lu_decomposition.py
  4. +2 −2 arithmetic_analysis/newton_forward_interpolation.py
  5. +3 −2 arithmetic_analysis/newton_raphson.py
  6. +6 −6 backtracking/all_combinations.py
  7. +7 −7 backtracking/all_permutations.py
  8. +6 −4 backtracking/all_subsequences.py
  9. +3 −4 backtracking/coloring.py
  10. +3 −4 backtracking/hamiltonian_cycle.py
  11. +5 −5 backtracking/knight_tour.py
  12. +3 −2 backtracking/minimax.py
  13. +4 −4 backtracking/n_queens.py
  14. +6 −6 backtracking/n_queens_math.py
  15. +3 −3 backtracking/rat_in_maze.py
  16. +4 −4 backtracking/sudoku.py
  17. +7 −7 backtracking/sum_of_subsets.py
  18. +2 −2 blockchain/chinese_remainder_theorem.py
  19. +3 −3 blockchain/diophantine_equation.py
  20. +3 −3 blockchain/modular_division.py
  21. +6 −6 boolean_algebra/quine_mc_cluskey.py
  22. +1 −4 cellular_automata/conways_game_of_life.py
  23. +5 −4 ciphers/caesar_cipher.py
  24. +3 −4 ciphers/decrypt_caesar_with_chi_squared.py
  25. +2 −2 ciphers/diffie.py
  26. +3 −2 ciphers/shuffled_shift_cipher.py
  27. +1 −1 compression/huffman.py
  28. +1 −1 conversions/molecular_chemistry.py
  29. +6 −5 conversions/prefix_conversions.py
  30. +13 −12 data_structures/binary_tree/avl_tree.py
  31. +5 −5 data_structures/binary_tree/basic_binary_tree.py
  32. +12 −12 data_structures/binary_tree/binary_search_tree_recursive.py
  33. +4 −3 data_structures/binary_tree/binary_tree_traversals.py
  34. +2 −3 data_structures/binary_tree/lazy_segment_tree.py
  35. +5 −5 data_structures/binary_tree/merge_two_binary_trees.py
  36. +14 −12 data_structures/binary_tree/red_black_tree.py
  37. +9 −12 data_structures/binary_tree/treap.py
  38. +3 −4 data_structures/binary_tree/wavelet_tree.py
  39. +1 −1 data_structures/hashing/hash_table.py
  40. +1 −1 data_structures/hashing/hash_table_with_linked_list.py
  41. +7 −5 data_structures/heap/heap.py
  42. +8 −8 data_structures/heap/randomized_heap.py
  43. +7 −7 data_structures/heap/skew_heap.py
  44. +2 −3 data_structures/linked_list/merge_two_lists.py
  45. +2 −2 data_structures/linked_list/print_reverse.py
  46. +3 −4 data_structures/linked_list/skip_list.py
  47. +4 −3 data_structures/stacks/evaluate_postfix_notations.py
  48. +4 −2 data_structures/stacks/linked_stack.py
  49. +2 −2 data_structures/stacks/stack.py
  50. +10 −9 divide_and_conquer/convex_hull.py
  51. +3 −2 divide_and_conquer/kth_order_statistic.py
  52. +3 −3 divide_and_conquer/mergesort.py
  53. +2 −2 divide_and_conquer/peak.py
  54. +3 −2 electronics/electric_power.py
  55. +2 −2 electronics/ohms_law.py
  56. +4 −4 graphs/basic_graphs.py
  57. +4 −4 graphs/bellman_ford.py
  58. +7 −7 graphs/bfs_zero_one_shortest_path.py
  59. +2 −5 graphs/bidirectional_a_star.py
  60. +4 −6 graphs/bidirectional_breadth_first_search.py
  61. +3 −4 graphs/breadth_first_search.py
  62. +1 −3 graphs/breadth_first_search_shortest_path.py
  63. +1 −4 graphs/depth_first_search.py
  64. +3 −5 graphs/greedy_best_first.py
  65. +1 −1 graphs/minimum_spanning_tree_kruskal.py
  66. +4 −3 graphs/minimum_spanning_tree_prims2.py
  67. +1 −1 graphs/page_rank.py
  68. +7 −7 graphs/scc_kosaraju.py
  69. +3 −3 hashes/luhn.py
  70. +2 −3 knapsack/knapsack.py
  71. +13 −13 linear_algebra/src/lib.py
  72. +3 −2 machine_learning/similarity_search.py
  73. +5 −4 maths/area_under_curve.py
  74. +2 −2 maths/average_mean.py
  75. +2 −2 maths/average_median.py
  76. +3 −3 maths/entropy.py
  77. +2 −0 maths/euclidean_distance.py
  78. +2 −2 maths/extended_euclidean_algorithm.py
  79. +1 −1 maths/hardy_ramanujanalgo.py
  80. +6 −4 maths/line_length.py
  81. +2 −2 maths/max_sum_sliding_window.py
  82. +2 −2 maths/median_of_two_arrays.py
  83. +5 −4 maths/numerical_integration.py
  84. +2 −3 maths/sieve_of_eratosthenes.py
  85. +3 −2 maths/volume.py
  86. +1 −3 matrix/searching_in_sorted_matrix.py
  87. +1 −1 other/date_to_weekday.py
  88. +12 −12 other/{davis–putnam–logemann–loveland.py → davisb_putnamb_logemannb_loveland.py}
  89. +4 −2 other/lfu_cache.py
  90. +4 −2 other/lru_cache.py
  91. +1 −1 project_euler/problem_001/sol1.py
  92. +1 −1 project_euler/problem_001/sol5.py
  93. +1 −1 project_euler/problem_006/sol3.py
  94. +1 −4 project_euler/problem_008/sol2.py
  95. +1 −1 project_euler/problem_012/sol2.py
  96. +1 −1 project_euler/problem_013/sol1.py
  97. +3 −3 project_euler/problem_014/sol2.py
  98. +1 −1 project_euler/problem_020/sol2.py
  99. +3 −5 project_euler/problem_021/sol1.py
  100. +3 −2 project_euler/problem_033/sol1.py
  101. +2 −3 project_euler/problem_036/sol1.py
  102. +2 −3 project_euler/problem_038/sol1.py
  103. +1 −1 project_euler/problem_049/sol1.py
  104. +2 −2 project_euler/problem_050/sol1.py
  105. +3 −3 project_euler/problem_051/sol1.py
  106. +2 −2 project_euler/problem_054/sol1.py
  107. +3 −5 project_euler/problem_056/sol1.py
  108. +11 −12 project_euler/problem_059/sol1.py
  109. +2 −2 project_euler/problem_070/sol1.py
  110. +1 −1 project_euler/problem_074/sol1.py
  111. +4 −4 project_euler/problem_077/sol1.py
  112. +1 −1 project_euler/problem_080/sol1.py
  113. +1 −1 project_euler/problem_081/sol1.py
  114. +2 −3 project_euler/problem_085/sol1.py
  115. +1 −1 project_euler/problem_089/sol1.py
  116. +9 −9 project_euler/problem_101/sol1.py
  117. +7 −7 project_euler/problem_102/sol1.py
  118. +11 −10 project_euler/problem_107/sol1.py
  119. +1 −1 project_euler/problem_119/sol1.py
  120. +3 −2 project_euler/problem_123/sol1.py
  121. +3 −4 project_euler/problem_180/sol1.py
  122. +6 −6 project_euler/problem_203/sol1.py
  123. +6 −6 scheduling/first_come_first_served.py
  124. +5 −4 scheduling/round_robin.py
  125. +6 −6 scheduling/shortest_job_first.py
  126. +10 −9 searches/binary_search.py
  127. +1 −1 searches/fibonacci_search.py
  128. +4 −4 searches/ternary_search.py
  129. +4 −4 sorts/bitonic_sort.py
  130. +2 −2 sorts/bucket_sort.py
  131. +5 −5 sorts/msd_radix_sort.py
  132. +4 −3 sorts/patience_sort.py
  133. +2 −2 sorts/pigeon_sort.py
  134. +3 −3 sorts/quick_sort.py
  135. +2 −4 sorts/radix_sort.py
  136. +1 −4 sorts/recursive_insertion_sort.py
  137. +2 −5 sorts/slowsort.py
  138. +4 −3 strings/aho_corasick.py
  139. +2 −2 strings/boyer_moore_search.py
  140. +2 −2 strings/knuth_morris_pratt.py
  141. +3 −2 web_programming/emails_from_url.py
  142. +4 −2 web_programming/fetch_github_info.py
@@ -545,7 +545,7 @@
## Other
* [Activity Selection](https://github.com/TheAlgorithms/Python/blob/master/other/activity_selection.py)
* [Date To Weekday](https://github.com/TheAlgorithms/Python/blob/master/other/date_to_weekday.py)
* [Davis–Putnam–Logemann–Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davis–putnam–logemann–loveland.py)
* [Davisb Putnamb Logemannb Loveland](https://github.com/TheAlgorithms/Python/blob/master/other/davisb_putnamb_logemannb_loveland.py)
* [Dijkstra Bankers Algorithm](https://github.com/TheAlgorithms/Python/blob/master/other/dijkstra_bankers_algorithm.py)
* [Doomsday](https://github.com/TheAlgorithms/Python/blob/master/other/doomsday.py)
* [Fischer Yates Shuffle](https://github.com/TheAlgorithms/Python/blob/master/other/fischer_yates_shuffle.py)
@@ -860,6 +860,7 @@
* [Counting Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/counting_sort.py)
* [Cycle Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/cycle_sort.py)
* [Double Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/double_sort.py)
* [Dutch National Flag Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/dutch_national_flag_sort.py)
* [Exchange Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/exchange_sort.py)
* [External Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/external_sort.py)
* [Gnome Sort](https://github.com/TheAlgorithms/Python/blob/master/sorts/gnome_sort.py)
@@ -1,14 +1,14 @@
"""
Checks if a system of forces is in static equilibrium.
"""
from typing import List
from __future__ import annotations

from numpy import array, cos, cross, ndarray, radians, sin


def polar_force(
magnitude: float, angle: float, radian_mode: bool = False
) -> List[float]:
) -> list[float]:
"""
Resolves force along rectangular components.
(force, angle) => (force_x, force_y)
@@ -3,12 +3,12 @@
Reference:
- https://en.wikipedia.org/wiki/LU_decomposition
"""
from typing import Tuple
from __future__ import annotations

import numpy as np


def lower_upper_decomposition(table: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""Lower-Upper (LU) Decomposition
Example:
@@ -1,7 +1,7 @@
# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/
from __future__ import annotations

This comment has been minimized.

Copy link
@Mecsy101

Mecsy101 Oct 7, 2021

X: edit pasted algorithm for resolution YY (content: resolution) -} (global peace)}

import math
from typing import List


# for calculating u value
@@ -22,7 +22,7 @@ def ucal(u: float, p: int) -> float:

def main() -> None:
n = int(input("enter the numbers of values: "))
y: List[List[float]] = []
y: list[list[float]] = []
for i in range(n):
y.append([])
for i in range(n):
@@ -2,15 +2,16 @@
# Author: Syed Haseeb Shah (github.com/QuantumNovice)
# The Newton-Raphson method (also known as Newton's method) is a way to
# quickly find a good approximation for the root of a real-valued function
from __future__ import annotations

from decimal import Decimal
from math import * # noqa: F401, F403
from typing import Union

from sympy import diff


def newton_raphson(
func: str, a: Union[float, Decimal], precision: float = 10 ** -10
func: str, a: float | Decimal, precision: float = 10 ** -10
) -> float:
"""Finds root from the point 'a' onwards by Newton-Raphson method
>>> newton_raphson("sin(x)", 2)
@@ -3,16 +3,16 @@
numbers out of 1 ... n. We use backtracking to solve this problem.
Time complexity: O(C(n,k)) which is O(n choose k) = O((n!/(k! * (n - k)!)))
"""
from typing import List
from __future__ import annotations


def generate_all_combinations(n: int, k: int) -> List[List[int]]:
def generate_all_combinations(n: int, k: int) -> list[list[int]]:
"""
>>> generate_all_combinations(n=4, k=2)
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
"""

result: List[List[int]] = []
result: list[list[int]] = []
create_all_state(1, n, k, [], result)
return result

@@ -21,8 +21,8 @@ def create_all_state(
increment: int,
total_number: int,
level: int,
current_list: List[int],
total_list: List[List[int]],
current_list: list[int],
total_list: list[list[int]],
) -> None:
if level == 0:
total_list.append(current_list[:])
@@ -34,7 +34,7 @@ def create_all_state(
current_list.pop()


def print_all_state(total_list: List[List[int]]) -> None:
def print_all_state(total_list: list[list[int]]) -> None:
for i in total_list:
print(*i)

@@ -5,18 +5,18 @@
Time complexity: O(n! * n),
where n denotes the length of the given sequence.
"""
from typing import List, Union
from __future__ import annotations


def generate_all_permutations(sequence: List[Union[int, str]]) -> None:
def generate_all_permutations(sequence: list[int | str]) -> None:
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])


def create_state_space_tree(
sequence: List[Union[int, str]],
current_sequence: List[Union[int, str]],
sequence: list[int | str],
current_sequence: list[int | str],
index: int,
index_used: List[int],
index_used: list[int],
) -> None:
"""
Creates a state space tree to iterate through each branch using DFS.
@@ -44,8 +44,8 @@ def create_state_space_tree(
sequence = list(map(int, input().split()))
"""

sequence: List[Union[int, str]] = [3, 1, 2, 4]
sequence: list[int | str] = [3, 1, 2, 4]
generate_all_permutations(sequence)

sequence_2: List[Union[int, str]] = ["A", "B", "C"]
sequence_2: list[int | str] = ["A", "B", "C"]
generate_all_permutations(sequence_2)
@@ -5,15 +5,17 @@
Time complexity: O(2^n),
where n denotes the length of the given sequence.
"""
from typing import Any, List
from __future__ import annotations

from typing import Any

def generate_all_subsequences(sequence: List[Any]) -> None:

def generate_all_subsequences(sequence: list[Any]) -> None:
create_state_space_tree(sequence, [], 0)


def create_state_space_tree(
sequence: List[Any], current_subsequence: List[Any], index: int
sequence: list[Any], current_subsequence: list[Any], index: int
) -> None:
"""
Creates a state space tree to iterate through each branch using DFS.
@@ -32,7 +34,7 @@ def create_state_space_tree(


if __name__ == "__main__":
seq: List[Any] = [3, 1, 2, 4]
seq: list[Any] = [3, 1, 2, 4]
generate_all_subsequences(seq)

seq.clear()
@@ -5,11 +5,10 @@
Wikipedia: https://en.wikipedia.org/wiki/Graph_coloring
"""
from typing import List


def valid_coloring(
neighbours: List[int], colored_vertices: List[int], color: int
neighbours: list[int], colored_vertices: list[int], color: int
) -> bool:
"""
For each neighbour check if coloring constraint is satisfied
@@ -35,7 +34,7 @@ def valid_coloring(


def util_color(
graph: List[List[int]], max_colors: int, colored_vertices: List[int], index: int
graph: list[list[int]], max_colors: int, colored_vertices: list[int], index: int
) -> bool:
"""
Pseudo-Code
@@ -86,7 +85,7 @@ def util_color(
return False


def color(graph: List[List[int]], max_colors: int) -> List[int]:
def color(graph: list[list[int]], max_colors: int) -> list[int]:
"""
Wrapper function to call subroutine called util_color
which will either return True or False.
@@ -6,11 +6,10 @@
Wikipedia: https://en.wikipedia.org/wiki/Hamiltonian_path
"""
from typing import List


def valid_connection(
graph: List[List[int]], next_ver: int, curr_ind: int, path: List[int]
graph: list[list[int]], next_ver: int, curr_ind: int, path: list[int]
) -> bool:
"""
Checks whether it is possible to add next into path by validating 2 statements
@@ -47,7 +46,7 @@ def valid_connection(
return not any(vertex == next_ver for vertex in path)


def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int) -> bool:
def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int) -> bool:
"""
Pseudo-Code
Base Case:
@@ -108,7 +107,7 @@ def util_hamilton_cycle(graph: List[List[int]], path: List[int], curr_ind: int)
return False


def hamilton_cycle(graph: List[List[int]], start_index: int = 0) -> List[int]:
def hamilton_cycle(graph: list[list[int]], start_index: int = 0) -> list[int]:
r"""
Wrapper function to call subroutine called util_hamilton_cycle,
which will either return array of vertices indicating hamiltonian cycle
@@ -1,9 +1,9 @@
# Knight Tour Intro: https://www.youtube.com/watch?v=ab_dY3dZFHM

from typing import List, Tuple
from __future__ import annotations


def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
def get_valid_pos(position: tuple[int, int], n: int) -> list[tuple[int, int]]:
"""
Find all the valid positions a knight can move to from the current position.
@@ -32,7 +32,7 @@ def get_valid_pos(position: Tuple[int, int], n: int) -> List[Tuple[int, int]]:
return permissible_positions


def is_complete(board: List[List[int]]) -> bool:
def is_complete(board: list[list[int]]) -> bool:
"""
Check if the board (matrix) has been completely filled with non-zero values.
@@ -47,7 +47,7 @@ def is_complete(board: List[List[int]]) -> bool:


def open_knight_tour_helper(
board: List[List[int]], pos: Tuple[int, int], curr: int
board: list[list[int]], pos: tuple[int, int], curr: int
) -> bool:
"""
Helper function to solve knight tour problem.
@@ -68,7 +68,7 @@ def open_knight_tour_helper(
return False


def open_knight_tour(n: int) -> List[List[int]]:
def open_knight_tour(n: int) -> list[list[int]]:
"""
Find the solution for the knight tour problem for a board of size n. Raises
ValueError if the tour cannot be performed for the given size.
@@ -7,12 +7,13 @@
leaves of game tree is stored in scores[]
height is maximum height of Game tree
"""
from __future__ import annotations

import math
from typing import List


def minimax(
depth: int, node_index: int, is_max: bool, scores: List[int], height: float
depth: int, node_index: int, is_max: bool, scores: list[int], height: float
) -> int:
"""
>>> import math
@@ -7,12 +7,12 @@
diagonal lines.
"""
from typing import List
from __future__ import annotations

solution = []


def isSafe(board: List[List[int]], row: int, column: int) -> bool:
def isSafe(board: list[list[int]], row: int, column: int) -> bool:
"""
This function returns a boolean value True if it is safe to place a queen there
considering the current state of the board.
@@ -40,7 +40,7 @@ def isSafe(board: List[List[int]], row: int, column: int) -> bool:
return True


def solve(board: List[List[int]], row: int) -> bool:
def solve(board: list[list[int]], row: int) -> bool:
"""
It creates a state space tree and calls the safe function until it receives a
False Boolean and terminates that branch and backtracks to the next
@@ -70,7 +70,7 @@ def solve(board: List[List[int]], row: int) -> bool:
return False


def printboard(board: List[List[int]]) -> None:
def printboard(board: list[list[int]]) -> None:
"""
Prints the boards that have a successful combination.
"""
@@ -75,14 +75,14 @@
for another one or vice versa.
"""
from typing import List
from __future__ import annotations


def depth_first_search(
possible_board: List[int],
diagonal_right_collisions: List[int],
diagonal_left_collisions: List[int],
boards: List[List[str]],
possible_board: list[int],
diagonal_right_collisions: list[int],
diagonal_left_collisions: list[int],
boards: list[list[str]],
n: int,
) -> None:
"""
@@ -139,7 +139,7 @@ def depth_first_search(


def n_queens_solution(n: int) -> None:
boards: List[List[str]] = []
boards: list[list[str]] = []
depth_first_search([], [], [], boards, n)

# Print all the boards
@@ -1,7 +1,7 @@
from typing import List
from __future__ import annotations


def solve_maze(maze: List[List[int]]) -> bool:
def solve_maze(maze: list[list[int]]) -> bool:
"""
This method solves the "rat in maze" problem.
In this problem we have some n by n matrix, a start point and an end point.
@@ -70,7 +70,7 @@ def solve_maze(maze: List[List[int]]) -> bool:
return solved


def run_maze(maze: List[List[int]], i: int, j: int, solutions: List[List[int]]) -> bool:
def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]]) -> bool:
"""
This method is recursive starting from (i, j) and going in one of four directions:
up, down, left, right.

0 comments on commit cecf43d

Please sign in to comment.