Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
10 lines solution in Creative category for Identify Block by CDG.Axel
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'
* * * ** ** *** **
* * * ** ** * **
* ** **
*
"""
grid = {i + 1: divmod(i, 4) for i in range(16)}
blocks = {'I': [1, 2, 3, 4], 'J': [1, 2, 3, 7], 'L': [3, 5, 6, 7], 'O': [1, 2, 5, 6],
'S': [2, 3, 5, 6], 'T': [1, 2, 3, 6], 'Z': [1, 2, 6, 7]} # I, J, L is rotated
num = sorted([grid[e] for e in numbers])
for block_letter in blocks:
block = sorted([grid[e] for e in blocks[block_letter]])
for _ in range(4):
if len({(num[i][0] - x, num[i][1] - y) for i, (x, y) in enumerate(block)}) == 1:
return block_letter
block = sorted([(-y, x) for x, y in block])
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"?')
Oct. 20, 2021
Comments: