Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"Spaceship Landing Strip" solution in Clear category for Spaceship Landing Strip by Vitaly_Titkov
# 2. a function that checks if all elements of a given area are ones
def rectangle_area(area, r, c, width, height):
for ro in range(r, height + r):
for co in range(c, width + c):
if area[ro][co] == 0:
return False
return True
def checkio(area: list) -> int:
"""
1. matrix transformation from letters to zeros and ones
2. rectangle_area() - a function that checks if all elements of a given area
are ones
3. there is a simple iteration of each element of our rectangular matrix.
also for each element we iterate over the
border of the rectangular area, which can be our desired rectangular
landing area
:param area:
:return:
"""
# 1. matrix transformation
for index2, row in enumerate(area):
temp_list = list(row)
for index, letter in enumerate(temp_list):
if letter == 'G' or letter == 'S':
temp_list[index] = 1
else:
temp_list[index] = 0
area[index2] = temp_list
# 3. simple iteration of each element
rows = len(area)
cols = len(area[0])
maxarea = 0
for r in range(rows):
for c in range(cols):
for height in range(1, rows - r + 1):
for width in range(1, cols - c + 1):
valid = rectangle_area(area, r, c, width, height)
if valid:
maxarea = max(maxarea, width * height)
return maxarea
if __name__ == '__main__':
print("Example:")
print(checkio(['GT', 'GG']))
# These "asserts" are used for self-checking and not for an auto-testing
assert checkio(['G']) == 1, "first"
assert checkio(['GS', 'GS']) == 4, "second"
assert checkio(['GT', 'GG']) == 2, "and other"
assert checkio(['GGTGG',
'TGGGG',
'GSSGT',
'GGGGT',
'GWGGG',
'RGTRT',
'RTGWT',
'WTWGR']) == 9
assert checkio(['GSWGG', 'SGRST', 'WGSRS', 'RSSST']) == 4
assert checkio(['GSWGG',
'SGRST',
'WGSRS',
'RSSST',
'RGTWW']) == 5
assert checkio(['GSGSS',
'GWSRS',
'SWSTG',
'GGSGG',
'GGSGG']) == 10
assert checkio(['GWGWS',
'RSRGR',
'TTTTT',
'TGGGT',
'TGGGT']) == 6
assert checkio(['GGGGGG',
'GGGGGG',
'SSSSSS',
'SSSSSS',
'GGGGGG',
'GGGGGG']) == 36
assert checkio(['RRRRRRR',
'RGGGGGR',
'RGSSSGR',
'RGSSSGR',
'RGSSSGR',
'RGGGGGR',
'RRRRRRR']) == 25
print("Coding complete? Click 'Check' to earn cool rewards!")
April 7, 2021
Comments: