## CheckIO: Repeating decimals

def convert(numerator, denominator): to_str = lambda L: ”.join(map(str, L)) decimals, remains = [], [] integer, remain = numerator // denominator, numerator % denominator remains.append(remain) while True: quotient, remain = remain * 10 // denominator, remain * 10 % denominator decimals.append(quotient)…

## CheckIO: The shortest Knight’s path

import itertools as it from string import ascii_lowercase as alpha def checkio(cells): “””str -> int Number of moves in the shortest path of knight “”” restrict = lambda c: 0 < c[0] < 9 and 0 < c[1] < 9…

from string import ascii_uppercase as alpha import itertools MAX_K = 37 def checkio(number): to_int = lambda x: int(x) if x.isdigit() else alpha.index(x) + 10 digits = list(map(to_int, number)) k = max(digits) + 1 for k in itertools.count(k): if k >…

## CheckIO: Broken Clock

from datetime import datetime, timedelta import re def to_sec(unit): dct = {‘second’: 1, ‘minute’: 60, ‘hour’: 3600} m = re.compile(‘^(second|minute|hour)’) return dct[m.match(unit).group()] def broken_clock(starting_time, wrong_time, error_description): a, b, c, d = error_description.replace(‘at ‘, ”).split() err_per_sec = (int(a) * to_sec(b)) /…

## CheckIO: Pattern Recognition

import itertools as it def checkio(pattern, image): pattern_height, pattern_width = len(pattern), len(list(zip(*pattern))) image_height, image_width = len(image), len(list(zip(*image))) scan = lambda: it.product(range(pattern_height), range(pattern_width)) for i, j in it.product(range(image_height), range(image_width)): try: if all(image[i + x][j + y] == pattern[x][y] for (x, y)…

## CheckIO: Determinant

def cofactor(matrix, row_num, col_num): cols = [col for i, col in enumerate(matrix) if i != col_num] rows = [row for i, row in enumerate(zip(*cols)) if i != row_num] return list(zip(*rows)) def checkio(matrix): if len(matrix) == 1: return matrix[0][0] else: sign…

## CheckIO: Shooting range

import math def calc_hitpoint(crd1, direction1, crd2, direction2): (a, b), (c, d) = crd1, crd2 (d1, d2), (d3, d4) = direction1, direction2 if d1 * d4 == d2 * d3: return None, None # parralel lines k = ((b – d)…

## CheckIO: Counting tiles

import itertools as it from math import ceil, hypot def checkio(radius): “””count tiles””” crds = [ ((x1, y1), (x2, y2)) for x1, y1, x2, y2 in it.product(range(ceil(radius + 1)), repeat=4) if x2 == x1 + 1 and y2 == y1…

## CheckIO: CheckSum

import functools def map_point(val): double = val * 2 if double < 10: return double else: return functools.reduce(lambda x, y: x + y, map(int, str(double))) def checkio(data): charseq = lambda x: ord(x) – 48 clean = lambda L: [x for…

## CheckIO: Vigenere Cipher

from string import ascii_uppercase as alpha import itertools as it def decrypt(enc_alpha, key_alpha): if alpha.index(enc_alpha) >= alpha.index(key_alpha): return alpha[alpha.index(enc_alpha) – alpha.index(key_alpha)] else: return alpha[len(alpha) + alpha.index(enc_alpha) – alpha.index(key_alpha)] def find_key(repeated): length = 1 while length <= len(repeated): check, iter_key =…