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]] |
CheckIO: Pattern Recognition
- CheckIO: Determinant
- CheckIO: Broken Clock