Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
numpy.rot90 solution in 3rd party category for Identify Block by kurosawa4434
from numpy import array, rot90
BLOCKS = [
('T', [(0, 0), (0, 1), (0, 2), (1, 1)], 4),
('I', [(0, 0), (1, 0), (2, 0), (3, 0)], 2),
('O', [(0, 0), (0, 1), (1, 0), (1, 1)], 1),
('L', [(0, 0), (1, 0), (2, 0), (2, 1)], 4),
('J', [(0, 1), (1, 1), (2, 0), (2, 1)], 4),
('S', [(0, 1), (0, 2), (1, 0), (1, 1)], 2),
('Z', [(0, 0), (0, 1), (1, 1), (1, 2)], 2),
]
def identify_block(numbers):
def co(grid):
return [(y, x) for y, row in enumerate(grid)
for x, v in enumerate(row) if v]
grid = array([[int(n in numbers)
for n in range(r*4+1, r*4+5)] for r in range(4)])
yxs = [co(rot90(grid, r)) for r in range(4)]
for kind, block, dirs in BLOCKS:
for d in range(dirs):
if len({(c1[0]-c2[0], c1[1]-c2[1])
for c1, c2 in zip(yxs[d], block)}) == 1:
return kind
return None
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert identify_block({10, 13, 14, 15}) == 'T', 'T'
assert identify_block({1, 5, 9, 6}) == 'T', 'T'
assert identify_block({2, 3, 7, 11}) == 'L', 'L'
assert identify_block({4, 8, 12, 16}) == 'I', 'I'
assert identify_block({3, 1, 5, 8}) == None, 'None'
print('"Run" is good. How is "Check"?')
Sept. 6, 2017
Comments: