Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Find Sequence by lukasz.bogaczynski
def checkio(matrix):
vertical_counts = [0] * len(matrix[0])
for r_index, row in enumerate(matrix):
horizontal_count = 0
horizontal_element = row[0]
for c_index, column in enumerate(row):
if column == matrix[r_index - 1][c_index]:
vertical_counts[c_index] += 1
if vertical_counts[c_index] == 4:
return True
else:
vertical_counts[c_index] = 1
if column == horizontal_element:
horizontal_count += 1
if horizontal_count == 4:
return True
else:
horizontal_count = 1
horizontal_element = column
i = 1
d_count = 1
while c_index - i > -1 and r_index + i < len(matrix[0]):
if matrix[r_index + i][c_index - i] == column:
d_count += 1
i += 1
if d_count == 4:
return True
else:
break
i = 1
d_count = 1
while c_index + i < len(matrix[0]) and r_index + i < len(matrix[0]):
if matrix[r_index + i][c_index + i] == column:
d_count += 1
i += 1
if d_count == 4:
return True
else:
break
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"
Nov. 18, 2017