把每一行是纯0的去掉,就是了
def house(plan):
#replace this for solution
if '#'not in plan:return 0
str_1=plan.split()
column_1 =len(str_1)
row_1 = len(str_1[0])
column_min=100
column_max=0
row_min=100
row_max=0
# 对行进行处理
for x in range(column_1):
if '#'in str_1[x] :
if row_min>x:
row_min=x
if row_max<x:
row_max=x
# 对列进行处理
str_2=['']* row_1
for x in str_1:
for y in range(row_1):
str_2[y]+=x[y]
for x in range(row_1):
if '#'in str_2[x] :
if column_min >x:
column_min =x
if column_max<x:
column_max=x
return (column_max- column_min)*(row_max- row_min)
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!")
本文介绍了一种算法,用于计算特定布局中包含特定字符区域的面积。通过对行和列的处理,算法可以有效地找出并计算出目标区域的尺寸。
1万+

被折叠的 条评论
为什么被折叠?



