Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Identify Block by Vasily__Chibilyaev
blocks = {
'I':[1,2,3,4],
'J':[2,6,9,10],
'L':[1,5,9,10],
'O':[1,2,5,6],
'S':[2,3,5,6],
'T':[1,2,3,6],
'Z':[1,2,6,7]
}
block_to_numbers = lambda block: sorted(1+x+4*y for x, y in block)
def rotate_and_shift(block):
block = [(-y , x) for x, y in block]
shift_x = min(x for x, _ in block)
shift_y = min(y for _, y in block)
return [(x-shift_x, y-shift_y) for x, y in block]
def identify_block(numbers):
block = [((n-1)%4,(n-1)//4) for n in numbers]
options = []
for i in range(4):
block = rotate_and_shift(block)
options.append(block_to_numbers(block))
return next((k for k in blocks if blocks[k] in options), 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'
assert identify_block([6,10,11,15]) == 'S', 'S'
assert identify_block([11,14,15,16]) == 'T', 'T'
print('"Run" is good. How is "Check"?')
May 25, 2018
Comments: