Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First (commented) solution in Clear category for Ground for the House by Elemenope
def house(plan):
rl, any_ = plan.split(), False # turn string into list of rows, var to detect if there are any # in plan
n, s, w, e = len(rl), 0, len(rl[0]), 0 # indicies of north, south, west, east end of house
for i in range(0,len(rl)): ### all rows of the plan
if '#' in rl[i]:
any_ = True # True if there is at least 1 # in plan
if i < n: # north end of house
n = i
if i > s: # south end of house
s = i
for j in range(0,len(rl[i])): ### single rows
if rl[i][j] == '#':
if j < w: # west end of house
w = j
if j > e:
e = j # east end of house
if any_ == True:
return (s-n+1)*(e-w+1) # return depth(n/s) * width(e/w)
else:
return 0 # return 0 if there are no # in the plan
if __name__ == '__main__':
print("Example:")
print(house('''
0000000000
#00000000#
##########
##00000000
#000000000
'''))
#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!")
Dec. 26, 2018
Comments: