Common

This is a module containing common functions shared among the different solutions. While we tried to make solutions as stand-alone as possible, we could not help but encapsulate a couple of patterns to help readability.

Provide common functionality shared among all the solutions.

Classes:

Lines(lines)

Represent a sequence of text lines.

Functions:

pairwise(iterable)

Iterate over (s0, s1, s2, ...) as ((s0, s1), (s1, s2), ...).

class Lines(lines: Sequence[str])[source]

Represent a sequence of text lines.

Methods:

__new__(cls, lines)

Ensure the properties on the lines.

__add__(other)

Concatenate two list of lines.

__getitem__()

Get the line(s) at the given index.

__len__()

Return the number of the lines.

__iter__()

Iterate over the lines.

static __new__(cls, lines: Sequence[str]) Lines[source]

Ensure the properties on the lines.

Please make sure that you transfer the “ownership” immediately to Lines and don’t modify the original list of strings any more:

##
# OK
##

lines = Lines(some_text.splitlines())

##
# Not OK
##

some_lines = some_text.splitlines()
lines = Lines(some_lines)
# ... do something assuming ``lines`` is immutable ...

some_lines[0] = "This will break \n your logic"
# ERROR! lines[0] now contains a new-line which is unexpected!
Requires
  • all('n' not in line and 'r' not in line for line in lines)

__add__(other: Lines) Lines[source]

Concatenate two list of lines.

__getitem__(index: int) str[source]
__getitem__(index: slice) Lines

Get the line(s) at the given index.

__len__() int[source]

Return the number of the lines.

__iter__() Iterator[str][source]

Iterate over the lines.

pairwise(iterable: Iterable[T]) Iterable[Tuple[T, T]][source]

Iterate over (s0, s1, s2, ...) as ((s0, s1), (s1, s2), ...).

>>> list(pairwise([]))
[]
>>> list(pairwise([1]))
[]
>>> list(pairwise([1, 2]))
[(1, 2)]
>>> list(pairwise([1, 2, 3]))
[(1, 2), (2, 3)]