Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Ground for the House by Plorchat
def house(plan):
# strategy :
# 1) find how many lines are involved (height of rectangle)
# 2) find the left and right borders (width of rectangle)
# 3) area = width*height
#
# 1) a. splitting the plan into lists of 1string/line
# 1) b. find min and max index of lines containing "#"
# 1) c. max - min +1 = height of rectangle
#
# 2) a. find min and max index of a "#" in any element in clean_list
# 2) b. max - min +1 = width of rectangle
list_string = [l for l in plan.split('\n') if len(l) > 0]
lines_index = []
hash_index = []
height, width = 0, 0
for i in range(len(list_string)):
if "#" in list_string[i]:
lines_index.append(i)
hash_index.append(list_string[i].find("#"))
hash_index.append(list_string[i].rfind("#"))
height = max(lines_index) - min(lines_index) +1
width = max(hash_index) -min(hash_index) +1
return height*width
if __name__ == '__main__':
print("Example:")
print(house('''
0000000
##00##0
######0
##00##0
#0000#0
'''))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert house('''
0000000
##00##0
######0
##00##0
#0000#0
''') == 24
assert house('''0000000000
#000##000#
##########
##000000##
0000000000
''') == 30
assert house('''0000
0000
#000
''') == 1
assert house('''0000
0000
''') == 0
assert house('''
0##0
0000
#00#
''') == 12
print("Coding complete? Click 'Check' to earn cool rewards!")
April 7, 2019
Comments: