Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
camelCase solution in Clear category for Spaceship Landing Strip by bryukh
from itertools import takewhile
AVAILABLE = "GS"
UNAVAILABLE = "RWT"
def convert_map(inmap):
"""
convert map from list of strings to matrix with 1 and 0
"""
return [[1 if ch in AVAILABLE else 0 for ch in line] for line in inmap]
def rectangles_in_histogram(histogram):
"""
Return max squares of rectangles in histogram
"""
max_square = 0
for i, col in enumerate(histogram):
for j in range(1, col+1):
size = j * len(list(takewhile(lambda x: x >= j, histogram[i:])))
max_square = max(max_square, size)
return max_square
def checkio(map):
adapted_map = convert_map(map)
max_size = 0
if not adapted_map:
return 0
hist = [0 for _ in range(len(adapted_map[0]))]
for row in adapted_map:
hist = [(hist[i] + 1) if row[i] else 0 for i in range(len(hist))]
max_size = max(max_size, rectangles_in_histogram(hist))
return max_size
if __name__ == '__main__':
assert checkio(['G']) == 1, 'First'
assert checkio(['GS', 'GS']) == 4, 'Second'
assert checkio(['GT', 'GG']) == 2, 'Third'
assert checkio(['GGTGG',
'TGGGG',
'GSSGT',
'GGGGT',
'GWGGG',
'RGTRT',
'RTGWT',
'WTWGR']) == 9, 'Fourth'
print('All is ok')
Oct. 5, 2012
Comments: