mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[mypy] Added/fixed type annotations for "rotate_matrix.py" & "test_matrix_operation.py" (#4221)
* [mypy] Added/fixed type annotations for "rotate_matrix.py" * [mypy] Added/fixed type annotations for "test_matrix_operation.py"
This commit is contained in:
parent
67b33a295b
commit
4c76e3cba0
@ -5,8 +5,10 @@ Discussion in stackoverflow:
|
|||||||
https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
|
https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
def make_matrix(row_size: int = 4) -> [[int]]:
|
|
||||||
|
def make_matrix(row_size: int = 4) -> list[list]:
|
||||||
"""
|
"""
|
||||||
>>> make_matrix()
|
>>> make_matrix()
|
||||||
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
|
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
|
||||||
@ -23,7 +25,7 @@ def make_matrix(row_size: int = 4) -> [[int]]:
|
|||||||
return [[1 + x + y * row_size for x in range(row_size)] for y in range(row_size)]
|
return [[1 + x + y * row_size for x in range(row_size)] for y in range(row_size)]
|
||||||
|
|
||||||
|
|
||||||
def rotate_90(matrix: [[]]) -> [[]]:
|
def rotate_90(matrix: list[list]) -> list[list]:
|
||||||
"""
|
"""
|
||||||
>>> rotate_90(make_matrix())
|
>>> rotate_90(make_matrix())
|
||||||
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]]
|
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]]
|
||||||
@ -35,7 +37,7 @@ def rotate_90(matrix: [[]]) -> [[]]:
|
|||||||
# OR.. transpose(reverse_column(matrix))
|
# OR.. transpose(reverse_column(matrix))
|
||||||
|
|
||||||
|
|
||||||
def rotate_180(matrix: [[]]) -> [[]]:
|
def rotate_180(matrix: list[list]) -> list[list]:
|
||||||
"""
|
"""
|
||||||
>>> rotate_180(make_matrix())
|
>>> rotate_180(make_matrix())
|
||||||
[[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]]
|
[[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]]
|
||||||
@ -47,7 +49,7 @@ def rotate_180(matrix: [[]]) -> [[]]:
|
|||||||
# OR.. reverse_column(reverse_row(matrix))
|
# OR.. reverse_column(reverse_row(matrix))
|
||||||
|
|
||||||
|
|
||||||
def rotate_270(matrix: [[]]) -> [[]]:
|
def rotate_270(matrix: list[list]) -> list[list]:
|
||||||
"""
|
"""
|
||||||
>>> rotate_270(make_matrix())
|
>>> rotate_270(make_matrix())
|
||||||
[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]
|
[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]
|
||||||
@ -59,22 +61,22 @@ def rotate_270(matrix: [[]]) -> [[]]:
|
|||||||
# OR.. transpose(reverse_row(matrix))
|
# OR.. transpose(reverse_row(matrix))
|
||||||
|
|
||||||
|
|
||||||
def transpose(matrix: [[]]) -> [[]]:
|
def transpose(matrix: list[list]) -> list[list]:
|
||||||
matrix[:] = [list(x) for x in zip(*matrix)]
|
matrix[:] = [list(x) for x in zip(*matrix)]
|
||||||
return matrix
|
return matrix
|
||||||
|
|
||||||
|
|
||||||
def reverse_row(matrix: [[]]) -> [[]]:
|
def reverse_row(matrix: list[list]) -> list[list]:
|
||||||
matrix[:] = matrix[::-1]
|
matrix[:] = matrix[::-1]
|
||||||
return matrix
|
return matrix
|
||||||
|
|
||||||
|
|
||||||
def reverse_column(matrix: [[]]) -> [[]]:
|
def reverse_column(matrix: list[list]) -> list[list]:
|
||||||
matrix[:] = [x[::-1] for x in matrix]
|
matrix[:] = [x[::-1] for x in matrix]
|
||||||
return matrix
|
return matrix
|
||||||
|
|
||||||
|
|
||||||
def print_matrix(matrix: [[]]) -> [[]]:
|
def print_matrix(matrix: list[list]) -> None:
|
||||||
for i in matrix:
|
for i in matrix:
|
||||||
print(*i)
|
print(*i)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import logging
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pytest
|
import pytest # type: ignore
|
||||||
|
|
||||||
# Custom/local libraries
|
# Custom/local libraries
|
||||||
from matrix import matrix_operation as matop
|
from matrix import matrix_operation as matop
|
||||||
|
Loading…
Reference in New Issue
Block a user