Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pattern Recognition by cimarronm
from itertools import product
def checkio(pattern, image):
imgh, imgw = len(image), len(image[0])
patth, pattw = len(pattern), len(pattern[0])
for y, x in product(range(imgh - patth + 1), range(imgw - pattw + 1)):
if all(img[x:x+pattw]==pat for pat, img in zip(pattern, image[y:])):
for dy, dx in product(range(patth), range(pattw)):
image[y+dy][x+dx] += 2
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]]
July 11, 2016
Comments: