From b4195baae3b36bd997a97d24fd5e0ab38dcd5595 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 12 Dec 2021 06:56:03 +0100 Subject: Add libaoc --- libaoc.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 libaoc.py (limited to 'libaoc.py') diff --git a/libaoc.py b/libaoc.py new file mode 100644 index 0000000..1316691 --- /dev/null +++ b/libaoc.py @@ -0,0 +1,26 @@ +from io import TextIOWrapper +from typing import Any, Callable, Iterable, Iterator, Optional, TypeVar + +S = TypeVar("S") +T = TypeVar("T") +matrix = list[list[T]] + + +def flatten(iter: Iterable[Any]) -> Iterator[Any]: + if hasattr(iter, "__iter__") and type(iter) != str: + for i in iter: + yield from flatten(i) + else: + yield iter + + +def read_int_matrix(f: TextIOWrapper) -> matrix[int]: + return list(map(lambda l: [int(n) for n in l.strip()], f.readlines())) + + +def map2d( + func: Callable[[Any], Any], + iter: Iterable[Iterable[Any]], + const: Optional[Callable[[T], S]] = lambda x: x, +) -> map: + return map(lambda i: const(map(func, i)), iter) -- cgit v1.2.3