Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Identify Block by yoichi
block_types = {
('1000', '1000', '1000', '1000'): 'I',
('1111', '0000', '0000', '0000'): 'I',
('0100', '0100', '1100', '0000'): 'J',
('1000', '1110', '0000', '0000'): 'J',
('1100', '1000', '1000', '0000'): 'J',
('1110', '0010', '0000', '0000'): 'J',
('1000', '1000', '1100', '0000'): 'L',
('1110', '1000', '0000', '0000'): 'L',
('1100', '0100', '0100', '0000'): 'L',
('1110', '1000', '0000', '0000'): 'L',
('1100', '1100', '0000', '0000'): 'O',
('0110', '1100', '0000', '0000'): 'S',
('1000', '1100', '0100', '0000'): 'S',
('1110', '0100', '0000', '0000'): 'T',
('0100', '1100', '0100', '0000'): 'T',
('0100', '1110', '0000', '0000'): 'T',
('1000', '1100', '1000', '0000'): 'T',
('1100', '0110', '0000', '0000'): 'Z',
('0100', '1100', '1000', '0000'): 'Z',
}
def identify_block(numbers):
"""
grid(4x4):
+--+--+--+--+
|1 |2 |3 |4 |
+--+--+--+--+
|5 |6 |7 |8 |
+--+--+--+--+
|9 |10|11|12|
+--+--+--+--+
|13|14|15|16|
+--+--+--+--+
blocks(7 kinds):
'I' 'J' 'L' 'O' 'S' 'T' 'Z'
* * * ** ** *** **
* * * ** ** * **
* ** **
*
"""
# represent grid by list of strings
grid = []
for i in range(16):
if i % 4 == 0:
grid.append('')
grid[-1] += '1' if i+1 in numbers else '0'
# move to the upper-left corner
while all(n == '0' for n in grid[0]):
grid = grid[1:] + grid[:1]
while all(row[0] == '0' for row in grid):
grid = [row[1:] + row[:1] for row in grid]
try:
return block_types[tuple(grid)]
except:
return None
Jan. 28, 2019