Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
nested_2 solution in Uncategorized category for Pattern Recognition by SlimeT
def checkio(pattern,matrix):
n,m = len(pattern),len(pattern[0])
def mark(x,y):
for k in range(n):
for l in range(m):
if pattern[k][l]!=matrix[k+x][l+y]:
return
for k in range(n):
for l in range(m):
matrix[k+x][l+y] = matrix[k+x][l+y]+2
for i in range(len(matrix)-n+1):
for j in range(len(matrix[0])-m+1):
mark(i,j)
return matrix
#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]]
Feb. 23, 2014