Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Third solution in Clear category for Building Visibility by gyahun_dash
class Building:
def __init__(self, w, s, e, n, height): #north: unused
self.west, self.south, self.east, self.height = w, s, e, height
@property
def range(self): return slice(self.west, self.east)
def seen_behind(silhouette):
return lambda bldg: any(s < bldg.height for s in silhouette[bldg.range])
def checkio(list_of_buildings):
if list_of_buildings == []: return 0
gen_bldgs = (Building(*data) for data in list_of_buildings)
buildings = sorted(gen_bldgs, key = lambda b: (b.south, b.west))
silhouette = [0] * max(bldg.east for bldg in buildings)
for count, b in enumerate(filter(seen_behind(silhouette), buildings), 1):
silhouette[b.range] = [max(s, b.height) for s in silhouette[b.range]]
return count
April 16, 2014
Comments: