## CheckIO: What does the cow say?

COW = r”’ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||—-w | || || ”’ TOP = ‘_’ BOTTOM = ‘-‘ BORDERS = ‘/\\|’ MAX_ROW = 39 def make_rows(text): rows, num = [”], 0 words = text.split(‘ ‘) for i, w…

## CheckIO: Numbers Factory

def checkio(number): ret = [] for i in range(9, 1, -1): while not number % i: number /= i ret.append(i) if number == 1: return int(”.join(map(str, sorted(ret)))) return 0 if __name__ == ‘__main__’: #These “asserts” using only for self-checking and…

## CheckIO: Moria doors

import itertools def calc_coefficient(x, y): score = 0 if x == y: score += 10 if x[-1] == y[-1]: score += 10 score += (len(x) / len(y)) * 30 if len(x) <= len(y) else (len(y) / len(x)) * 30 score…

## CheckIO: Open Labyrinth

class Node(object): def __init__(self, index, crd): self.index = index self.crd = crd self.links = None def __sub__(self, other): return ( self.crd – other.crd, self.crd – other.crd ) def map_to_graph(maze_map): graph = [] def get_index(crd): for n in graph: if n.crd…

## CheckIO: Weak Point

def weak_point(matrix): check = lambda x: [sum(y) for y in x] rows, cols = check(matrix), check(zip(*matrix)) return [rows.index(min(rows)), cols.index(min(cols))] if __name__ == ‘__main__’: #These “asserts” using only for self-checking and not necessary for auto-testing assert isinstance(weak_point([]), (list, tuple)), “The result…

## CheckIO: Brackets

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…