Skip to content

[mypy] Fix type annotations for backtracking/sudoku.py, graphics/vector3_for_2d_rendering.py, maths/{area.py, two_pointer.py, two_sum.py, ugly_numbers.py} sorts/cocktail_shaker_sort.py, insertion_sort.py #4672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
8 changes: 4 additions & 4 deletions backtracking/sudoku.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
Given a partially filled 9×9 2D array, the objective is to fill a 9×9
square grid with digits numbered 1 to 9, so that every row, column, and
and each of the nine 3×3 sub-grids contains all of the digits.
square grid with digits numbered 1 to 9, so that every row, column, and each of the nine 3×3 sub-grids contains all of the digits.

This can be solved using Backtracking and is similar to n-queens.
We check to see if a cell is safe or not and recursively call the
Expand Down Expand Up @@ -90,8 +89,9 @@ def sudoku(grid: Matrix) -> Optional[Matrix]:
>>> sudoku(no_solution) is None
True
"""
if location := find_empty_location(grid):
row, column = location
if find_empty_location(grid)!=None:
#Assigning list values to row and column
row, column = find_empty_location(grid)
else:
# If the location is ``None``, then the grid is solved.
return grid
Expand Down
6 changes: 3 additions & 3 deletions graphics/vector3_for_2d_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ def rotate(
if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) = }")
print(f"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }")
doctest.testmod(verbose=True)
print(f"convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0): {convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0)}")
print(f"rotate(1.0, 2.0, 3.0, 'y', 90.0): {rotate(1.0, 2.0, 3.0, 'y', 90.0)}")
20 changes: 10 additions & 10 deletions maths/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,14 @@ def area_rhombus(diagonal_1: float, diagonal_2: float) -> float:
doctest.testmod(verbose=True) # verbose so we can see methods missing tests

print("[DEMO] Areas of various geometric shapes: \n")
print(f"Rectangle: {area_rectangle(10, 20) = }")
print(f"Square: {area_square(10) = }")
print(f"Triangle: {area_triangle(10, 10) = }")
print(f"Triangle: {area_triangle_three_sides(5, 12, 13) = }")
print(f"Parallelogram: {area_parallelogram(10, 20) = }")
print(f"Trapezium: {area_trapezium(10, 20, 30) = }")
print(f"Circle: {area_circle(20) = }")
print(f"Rectangle: {area_rectangle(10, 20)}")
print(f"Square: {area_square(10)}")
print(f"Triangle: {area_triangle(10, 10)}")
print(f"Triangle: {area_triangle_three_sides(5, 12, 13)}")
print(f"Parallelogram: {area_parallelogram(10, 20)}")
print(f"Trapezium: {area_trapezium(10, 20, 30)}")
print(f"Circle: {area_circle(20)}")
print("\nSurface Areas of various geometric shapes: \n")
print(f"Cube: {surface_area_cube(20) = }")
print(f"Sphere: {surface_area_sphere(20) = }")
print(f"Rhombus: {area_rhombus(10, 20) = }")
print(f"Cube: {surface_area_cube(20)}")
print(f"Sphere: {surface_area_sphere(20)}")
print(f"Rhombus: {area_rhombus(10, 20)}")
4 changes: 2 additions & 2 deletions maths/two_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ def two_pointer(nums: list[int], target: int) -> list[int]:
if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{two_pointer([2, 7, 11, 15], 9) = }")
doctest.testmod(verbose=True)
print(f"two_pointer([2, 7, 11, 15], 9): {two_pointer([2, 7, 11, 15], 9)}")
4 changes: 2 additions & 2 deletions maths/two_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ def two_sum(nums: list[int], target: int) -> list[int]:
if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{two_sum([2, 7, 11, 15], 9) = }")
doctest.testmod(verbose=True)
print(f"two_sum([2, 7, 11, 15], 9): {two_sum([2, 7, 11, 15], 9)}")
2 changes: 1 addition & 1 deletion maths/ugly_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ def ugly_numbers(n: int) -> int:
from doctest import testmod

testmod(verbose=True)
print(f"{ugly_numbers(200) = }")
print(f"ugly_numbers(200): {ugly_numbers(200)}")
6 changes: 3 additions & 3 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ def main():
matrix_b = [[3, 4], [7, 4]]
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
print(f"Add Operation, {add(matrix_a, matrix_b) = } \n")
print(f"Add Operation, {add(matrix_a, matrix_b)} \n")
print(
f"Multiply Operation, {multiply(matrix_a, matrix_b) = } \n",
f"Multiply Operation, {multiply(matrix_a, matrix_b)} \n",
)
print(f"Identity: {identity(5)}\n")
print(f"Minor of {matrix_c} = {minor(matrix_c, 1, 2)} \n")
Expand All @@ -180,5 +180,5 @@ def main():
if __name__ == "__main__":
import doctest

doctest.testmod()
doctest.testmod(verbose=True)
main()
4 changes: 2 additions & 2 deletions sorts/cocktail_shaker_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def cocktail_shaker_sort(unsorted: list) -> list:
if __name__ == "__main__":
import doctest

doctest.testmod()
doctest.testmod(verbose=True)
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(f"{cocktail_shaker_sort(unsorted) = }")
print(f"{cocktail_shaker_sort(unsorted)}")
3 changes: 2 additions & 1 deletion sorts/exchange_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def exchange_sort(numbers: list[int]) -> list[int]:

if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
unsorted = list([int(item) for item in user_input.split(",")])
list(unsorted)
print(exchange_sort(unsorted))
4 changes: 2 additions & 2 deletions sorts/insertion_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def insertion_sort(collection: list) -> list:
if __name__ == "__main__":
from doctest import testmod

testmod()
testmod(verbose=True)

user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(f"{insertion_sort(unsorted) = }")
print(f"{insertion_sort(unsorted)}")