Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Identify Block by mortonfox
rotate_table = {
1: 13, 2: 9, 3: 5, 4: 1,
5: 14, 6: 10, 7: 6, 8: 2,
9: 15, 10: 11, 11: 7, 12: 3,
13: 16, 14: 12, 15: 8, 16: 4
}
def rotate(numbers):
return [rotate_table[i] for i in numbers]
def shift_left(numbers):
shift_count = min((n - 1) % 4 for n in numbers)
return [n - shift_count for n in numbers]
def shift_up(numbers):
shift_count = min((n - 1) // 4 for n in numbers)
return [n - shift_count * 4 for n in numbers]
blocks = {
(1, 5, 9, 13): 'I',
(2, 6, 9, 10): 'J',
(1, 5, 9, 10): 'L',
(1, 2, 5, 6): 'O',
(2, 3, 5, 6): 'S',
(1, 2, 3, 6): 'T',
(1, 2, 6, 7): 'Z'
}
def identify(numbers):
t = tuple(sorted(numbers))
if t in blocks:
return blocks[t]
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'
* * * ** ** *** **
* * * ** ** * **
* ** **
*
"""
for _i in range(4):
result = identify(shift_left(shift_up(numbers)))
if result:
return result
numbers = rotate(numbers)
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. 7, 2017