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 _…
CheckIO: Feed Pigeons
def iter_pigeons(): i = 1 while True: yield sum(range(1, i + 1)) i += 1 def checkio(portions): pigeons = iter_pigeons() feeded = 0 while True: hungry = next(pigeons) if portions <= hungry: return portions if portions > feeded else feeded…
CheckIO: Pawn Brotherhood
import itertools def safe_pawns(pawns): safe_squares = itertools.chain(*[calc_safe_squares(p) for p in pawns]) return len(pawns & set(safe_squares)) def calc_safe_squares(pos): return (chr(ord(pos[0]) – 1) + str(int(pos[1]) + 1), chr(ord(pos[0]) + 1) + str(int(pos[1]) + 1)) if __name__ == ‘__main__’: #These “asserts” using only…
CheckIO: Speech Module
FIRST_TEN = [“one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”, “nine”] SECOND_TEN = [“ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “fifteen”, “sixteen”, “seventeen”, “eighteen”, “nineteen”] OTHER_TENS = [“twenty”, “thirty”, “forty”, “fifty”, “sixty”, “seventy”, “eighty”, “ninety”] HUNDRED = “hundred” def checkio(number): second_tens =…
Python: Exchange rows and columns
# ABC # DEF # GHI >>> rows = [‘ABC’, ‘DEF’, ‘GHI’] >>> cols = [”.join(x) for x in zip(*rows)] >>> cols [‘ADG’, ‘BEH’, ‘CFI’]