Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
A bit long 'if' with dictionary solution in Clear category for Find Sequence by swagg010164
def checkio(matrix):
n = len(matrix)
d = {i: [] for i in range(1, 11)}
for i in range(n):
for j in range(n):
d[matrix[i][j]].append((i, j))
for k in d:
for i, j in d[k]:
if ((i, j + 1) in d[k] and (i, j + 2) in d[k] and (i, j + 3) in d[k]) or \
((i + 1, j) in d[k] and (i + 2, j) in d[k] and (i + 3, j) in d[k])or \
((i + 1, j + 1) in d[k] and (i + 2, j + 2) in d[k] and (i + 3, j + 3) in d[k]) or \
((i + 1, j - 1) in d[k] and (i + 2, j - 2) in d[k] and (i + 3, j - 3) in d[k]):
return True
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([
[1, 2, 1, 1],
[1, 1, 4, 1],
[1, 3, 1, 6],
[1, 7, 2, 5]
]) == True, "Vertical"
assert checkio([
[7, 1, 4, 1],
[1, 2, 5, 2],
[3, 4, 1, 3],
[1, 1, 8, 1]
]) == False, "Nothing here"
assert checkio([
[2, 1, 1, 6, 1],
[1, 3, 2, 1, 1],
[4, 1, 1, 3, 1],
[5, 5, 5, 5, 5],
[1, 1, 3, 1, 1]
]) == True, "Long Horizontal"
assert checkio([
[7, 1, 1, 8, 1, 1],
[1, 1, 7, 3, 1, 5],
[2, 3, 1, 2, 5, 1],
[1, 1, 1, 5, 1, 4],
[4, 6, 5, 1, 3, 1],
[1, 1, 9, 1, 2, 1]
]) == True, "Diagonal"
Sept. 21, 2018
Comments: