Created
March 20, 2022 22:46
-
-
Save thecaralice/138b498bcdc5092119e4c0bdbaed9b54 to your computer and use it in GitHub Desktop.
colorful checkerboard-like matrix pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TypeVar | |
from collections.abc import Sequence | |
from sys import argv, stderr | |
from itertools import cycle, islice | |
T = TypeVar("T") | |
def dist(index: int, length: int) -> int: | |
if not (0 <= index < length): | |
raise ValueError("index must be in [0; length)") | |
return min(index, length - index - 1) | |
def generate_matrix(size: int, alphabet: Sequence[T]) -> list[list[T]]: | |
return [ | |
[ | |
alphabet[abs(dist(i, size) - dist(j, size))] | |
for j in range(size) | |
] | |
for i in range(size) | |
] | |
def main(): | |
try: | |
size_arg = argv[1] | |
except IndexError: | |
size_arg = input("Enter the size: ") | |
try: | |
size = int(size_arg) | |
except ValueError: | |
print("Invalid `size` argument", file=stderr) | |
return | |
alphabet = [f"\x1b[3{i+1}m{char} " for i, char in islice(cycle(enumerate("abcdefg")), size)] | |
matrix = generate_matrix(size, alphabet) | |
for row in matrix: | |
print(*row, sep="", end="\x1b[m\n") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment