1 2 3 4 5 6 7 8 9 10 |
>>> from unittest.mock import MagicMock >>> foo = MagicMock(attr1='spam', attr2='ham', attr3='eggs') >>> foo <MagicMock id='3059559436'> >>> foo.attr1 'spam' >>> foo.attr2 'ham' >>> foo.attr3 'eggs' |
CheckIO: Repeating decimals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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) if not remain: # doesn't repeat if sum(decimals): return '{}.{}'.format(integer, to_str(decimals)) else: return '{}.'.format(integer) if remain in remains: # repeat found repeat_start = remains.index(remain) non_repeat, repeat = decimals[:repeat_start], decimals[repeat_start:] return '{}.{}'.format( integer, to_str(non_repeat) + '({})'.format(to_str(repeat)) ) else: remains.append(remain) if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert convert(1, 3) == "0.(3)", "1/3 Classic" assert convert(5, 3) == "1.(6)", "5/3 The same, but bigger" assert convert(3, 8) == "0.375", "3/8 without repeating part" assert convert(7, 11) == "0.(63)", "7/11 prime/prime" assert convert(29, 12) == "2.41(6)", "29/12 not and repeating part" assert convert(11, 7) == "1.(571428)", "11/7 six digits" assert convert(0, 117) == "0.", "Zero" |
https:/…
CheckIO: The shortest Knight’s path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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 calc_reachable = lambda a, b: list( filter(restrict, it.chain( ((a + x, b + y) for x in (-2, 2) for y in (-1, 1)), ((a + x, b + y) for y in (-2, 2) for x in (-1, 1)), ))) to_num = lambda x: alpha.index(x) + 1 if x.isalpha() else int(x) start, goal = (tuple(map(to_num, x)) for x in cells.split('-')) reachable = (start,) for count in it.count(1): reachable = set(it.chain.from_iterable( calc_reachable(*c) for c in reachable )) if goal in reachable: return count if __name__ == "__main__": #These "asserts" using only for self-checking and not necessary for auto-testing assert checkio("b1-d5") == 2, "1st example" assert checkio("a6-b8") == 1, "2nd example" assert checkio("h1-g2") == 4, "3rd example" assert checkio("h8-d7") == 3, "4th example" assert checkio("a1-h8") == 6, "5th example" |
https:/…
CheckIO: The Good Radix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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 > MAX_K: return 0 else: decimal = sum((k ** i) * x for i, x in enumerate(reversed(digits))) if not decimal % (k - 1): return k if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert checkio("18") == 10, "Simple decimal" assert checkio("1010101011") == 2, "Any number is divisible by 1" assert checkio("222") == 3, "3rd test" assert checkio("A23B") == 14, "It's not a hex" assert checkio("IDDQD") == 0, "k is not exist" print('Local tests done') |
http://…
CheckIO: Broken Clock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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)) / (int(c) * to_sec(d)) time_diff_sec = lambda x, y: sum((int(k) - int(j)) * (60 ** i) for i, (j, k) in enumerate( reversed( list(zip(x.split(':'), y.split(':'))) ))) elapsed_sec = int(time_diff_sec(starting_time, wrong_time)) / (1 + err_per_sec) return datetime.strftime( datetime.strptime(starting_time, '%H:%M:%S') + timedelta(seconds=elapsed_sec), '%H:%M:%S' ) #These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == "__main__": assert broken_clock('00:00:00', '00:00:15', '+5 seconds at 10 seconds') == '00:00:10', "First example" assert broken_clock('06:10:00', '06:10:15', '-5 seconds at 10 seconds') == '06:10:30', 'Second example' assert broken_clock('13:00:00', '14:01:00', '+1 second at 1 minute') == '14:00:00', 'Third example' assert broken_clock('01:05:05', '04:05:05', '-1 hour at 2 hours') == '07:05:05', 'Fourth example' assert broken_clock('00:00:00', '00:00:30', '+2 seconds at 6 seconds') == '00:00:22', 'Fifth example' |
https:/…
CheckIO: Pattern Recognition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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) in scan()): for x, y in scan(): image[i + x][j + y] += 2 except IndexError: continue return image #These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': assert checkio([[1, 0], [1, 1]], [[0, 1, 0, 1, 0], [0, 1, 1, 0, 0], [1, 0, 1, 1, 0], [1, 1, 0, 1, 1], [0, 1, 1, 0, 0]]) == [[0, 3, 2, 1, 0], [0, 3, 3, 0, 0], [3, 2, 1, 3, 2], [3, 3, 0, 3, 3], [0, 1, 1, 0, 0]] assert checkio([[1, 1], [1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]]) == [[3, 3, 1], [3, 3, 1], [1, 1, 1]] assert checkio([[0, 1, 0], [1, 1, 1]], [[0, 0, 1, 0, 0, 0, 0, 0, 1, 0], [0, 1, 1, 1, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 1, 0, 1, 0], [1, 1, 1, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) == [[0, 2, 3, 2, 0, 0, 0, 2, 3, 2], [0, 3, 3, 3, 0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 2, 3, 2, 0, 0, 0], [2, 3, 2, 0, 3, 3, 3, 0, 1, 0], [3, 3, 3, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 2, 3, 2, 0], [0, 1, 1, 0, 0, 0, 3, 3, 3, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] |
https:/…
CheckIO: Determinant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 = lambda i: -1 if i % 2 else 1 return sum( matrix[j][0] * checkio(cofactor(matrix, 0, j)) * sign(i) for i, j in enumerate(range(len(matrix))) ) #These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': assert checkio([[4, 3], [6, 3]]) == -6, 'First example' assert checkio([[1, 3, 2], [1, 1, 4], [2, 2, 1]]) == 14, 'Second example' |
https:/…
CheckIO: Shooting range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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) * d3 - (a - c) * d4) / (d1 * d4 - d2 * d3) return (a + d1 * k, b + d2 * k), k def calc_equation(crd1, crd2): a = (crd1[1] - crd2[1]) / (crd1[0] - crd2[0]) b = crd1[1] - a * crd1[0] return lambda x: a * x + b def shot(wall1, wall2, shot_point, later_point): avg = lambda x, y: (x + y) / 2 direction = lambda crd1, crd2: tuple((x[0] - x[1]) for x in zip(crd1, crd2)) distance = lambda crd1, crd2: math.sqrt(sum((x[0] - x[1]) ** 2 for x in zip(crd1, crd2))) ballistic, wall = direction(later_point, shot_point), direction(wall2, wall1) hit_point, k = calc_hitpoint(shot_point, ballistic, wall1, wall) if k and k > 0 and all( sorted(w)[0] <= hit_point[i] <= sorted(w)[1] for i, w in enumerate(zip(wall1, wall2)) ): center = tuple(avg(c1, c2) for c1, c2 in zip(wall1, wall2)) score = calc_equation((0, 100), (distance(wall1, wall2) / 2, 0)) return round(score(distance(center, hit_point))) else: return -1 if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert shot((2, 2), (5, 7), (11, 2), (8, 3)) == 100, "1st case" assert shot((2, 2), (5, 7), (11, 2), (7, 2)) == 0, "2nd case" assert shot((2, 2), (5, 7), (11, 2), (8, 4)) == 29, "3th case" assert shot((2, 2), (5, 7), (11, 2), (9, 5)) == -1, "4th case" assert shot((2, 2), (5, 7), (11, 2), (10.5, 3)) == -1, "4th case again" |
http://…
CheckIO: Counting tiles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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 + 1 ] # p: lower left coordinate # q: upper right coordinate solid = sum(1 for p, q in crds if hypot(*q) <= radius) * 4 partial = sum(1 for p, q in crds if hypot(*p) < radius < hypot(*q)) * 4 return [solid, partial] # These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': assert checkio(2) == [4, 12], "N=2" assert checkio(3) == [16, 20], "N=3" assert checkio(2.1) == [4, 20], "N=2.1" assert checkio(2.5) == [12, 20], "N=2.5" |
http://…
CheckIO: CheckSum
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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 x in L if x.isalpha() or x.isdigit()] total = sum( x if i % 2 else map_point(x) for i, x in enumerate(map(charseq, reversed(clean(data)))) ) mod = total % 10 final = 10 - mod if mod else 0 return [str(final), total] # These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': assert (checkio("799 273 9871") == ["3", 67]), "First Test" assert (checkio("139-MT") == ["8", 52]), "Second Test" assert (checkio("123") == ["0", 10]), "Test for zero" assert (checkio("999_999") == ["6", 54]), "Third Test" assert (checkio("+61 820 9231 55") == ["3", 37]), "Fourth Test" assert (checkio("VQ/WEWF/NY/8U") == ["9", 201]), "Fifth Test" |
http://…