import re def checkio(expression): pattern = ‘({}|\[\]|\(\))’ matcher = re.compile(pattern) brackets = ”.join(x for x in expression if x in ‘{}[]()’) while True: m = matcher.search(brackets) if not m: break brackets = brackets.replace(m.group(), ”) return False if brackets else True…
CheckIO: Network Attack (2)
from itertools import count from collections import Counter def capture(matrix): security = Counter({i: row[i] for i, row in enumerate(matrix)}) connected = lambda i: {j for j, x in enumerate(matrix[i]) if x and i != j} reached = {0} for time…
CheckIO: Network Attack
def capture(matrix): reached, injected = {0}, {0} security_levels = [row[i] for i, row in enumerate(matrix)] connected = [[j for j, x in enumerate(row) if x and i != j] for i, row in enumerate(matrix)] time = 0 while True: reached.update(x…
CheckIO: Break Rings
import itertools def break_rings(connections): remain_rings = lambda L: set(itertools.chain(*L)) remain_links = lambda r, L: [x for x in L if r not in x] rings = remain_rings(connections) count = 0 while True: rings_after_break, links_after_break = len(rings), len(connections) next_break = None…
CheckIO: Moore Neighbourhood
import itertools def count_neighbours(grid, row, col): is_neighbour = lambda x, y: True if abs(x – y) <= 1 else False rows = [x for i, x in enumerate(grid) if is_neighbour(row, i)] neighbours = [[y for i, y in enumerate(x) if…
CheckIO: Morse Clock
def checkio(time_string): ON, OFF = ‘.’, ‘-‘ hour, minitue, second = [‘{:0>2}’.format(x) for x in time_string.split(‘:’)] convert = lambda x: x.replace(‘0’, ON).replace(‘1’, OFF) to_bin = lambda x: bin(int(x))[2:] binary = [to_bin(x) for x in hour + minitue + second] morse…
CheckIO: Min and Max
import functools def abstract(*args, **kwargs): compare = kwargs.get(‘compare’, lambda x,y: x > y) key = kwargs.get(‘key’, lambda x: x) values = iter(args[0]) if len(args) == 1 else iter(args) result = next(values) for i in values: if compare(key(i), key(result)): result =…
CheckIO: Roman numerals
import collections import functools Symbols = collections.namedtuple( ‘Symbols’, [‘unus’, ‘quinque’, ‘decem’, ‘quinquaginta’, ‘centum’, ‘quingenti’, ‘mille’] ) symbol = Symbols(‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’) def to_roman(digit, one, five, ten): if 0 < digit < 4: return ”.join([one for _…