33
loading...
This website collects cookies to deliver better user experience
str.split
method:coordinates = line.split(',')
x, y = line.split(',')
ValueError
at runtime. The split method also has a maxsplit
argument that can prevent such an error, but in my opinion input validation is a better idea in a real world scenario.def fold_horizontally(fold_row: int, dots: set[tuple[int, int]]) -> set[tuple[int, int]]:
...
def fold_vertically(fold_col: int, dots: set[tuple[int, int]]) -> set[tuple[int, int]]:
...
from functools import partial
instructions = []
while line := f.readline():
instruction, coord = line.split('=')
fold_func = fold_horizontally if instruction.endswith('y') else fold_vertically
instructions.append(partial(fold_func, int(coord)))
partial
function returns a callable object for a function and a list of arguments. The remaining arguments will be the arguments of this callable object. In my case the base function is either fold_horizontally
or fold_vertically
. Those functions have 2 arguments: the row or column to fold along and the list of dots. I fixed the first argument, the fold coordinate to create a partially applied function, and I put it to the instructions
list. These functions have one argument (the set of dots) and they can be called like this:dots = instructions[i](dots)
#
characters for the dots which is a good ASCII character for this purpose, but unicode has better options. What about a full block █ character? Or a black large circle ⬤ ? Or a hexagon ⬢ ?print('\N{FULL BLOCK}' if (col, row) in dots else ' ', end='')
██ █ ██ █ █ ██ ██ █
█ █ █
█ ███ ██ ███ █ █
██ █ █ █ █ █ █ █
█ █ █ █ █ █ █ █ █
█ █ ████ █ █ █ █
█ ███ █ █ █ █ █ ██ █
█ █ █ █ █ █ █
█ █ ██ █ █ █ █ █
█ █ █ █ █ █
█ █ █ █ █ █ █ █ █ █ █ █
██ ██ █ █ ████ █ ██ █ ██