Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pattern Recognition by Sillte
def checkio(pattern, image):
dps = [(ph, pw) for ph in range(len(pattern)) for pw in range(len(pattern[0]))]
for h in range(len(image) - len(pattern) + 1):
for w in range(len(image[h]) - len(pattern[0]) + 1):
if all((pattern[dp[0]][dp[1]] == image[h + dp[0]][w + dp[1]] for dp in dps)):
for dp in dps:
image[h + dp[0]][w + dp[1]] += 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]]
April 15, 2018
Comments: