-
Notifications
You must be signed in to change notification settings - Fork 1
/
rotate-matrix.py
executable file
·41 lines (30 loc) · 1.02 KB
/
rotate-matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# Author: github.com/danielhoherd
# License: MIT
"""Take a space delimited matrix on stdin and rotate or flip it diagonally."""
from enum import Enum
from sys import argv, stdin
import typer
class Direction(str, Enum):
left = "left"
right = "right"
diag = "diag"
def rotate_matrix(direction: Direction = typer.Option(Direction.left, "--direction", "-d", help="Direction to rotate.")):
"""Rotate a matrix left or right."""
if stdin.isatty():
print(f"Description: {__doc__}")
print(f"Usage: <some_command> | {argv[0]}")
raise SystemExit(1)
matrix = [line.split() for line in stdin]
match direction:
case "left":
rotated = list(zip(*matrix))[::-1]
case "right":
rotated = list(zip(*matrix[::-1]))
case "diag":
rotated = list(zip(*matrix))
case _:
raise SystemExit(1)
print("\n".join(" ".join(line) for line in rotated))
if __name__ == "__main__":
typer.run(rotate_matrix)