Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
deque/dropwhile solution in Clear category for Find Sequence by makoto_yamagata
from collections import deque
from itertools import dropwhile
import re
def checkio(m):
def diagonal(m):
rs = []
for n, row in enumerate(deque(x+[None]*(len(x)-1)) for x in m):
row.rotate(n)
rs.append(row)
return [[x for x in row if x] for row in zip(*rs)]
def lose(m):
find = lambda row: re.search('([0-9])\\1{3,}', ''.join(map(str, row)))
return not any(find(row) for row in m)
matrixes = (m, map(list, zip(*m)), diagonal(m), diagonal(reversed(m)))
return bool(next(dropwhile(lose, matrixes), None))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
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"
March 18, 2014