Skip to content

Instantly share code, notes, and snippets.

@jbochi
Created August 7, 2012 17:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbochi/3287392 to your computer and use it in GitHub Desktop.
Save jbochi/3287392 to your computer and use it in GitHub Desktop.
Solution for dailykata.net - Skyline problem in Python
buildings = [
[1, 11, 5],
[2, 6, 7],
[3, 13, 9],
[12, 7, 16],
[14, 3, 25],
[19, 18, 22],
[23, 13, 29],
[24, 4, 28],
]
LEFT = 0
HEIGHT = 1
RIGHT = 2
def skyline(buildings):
left = min(b[LEFT] for b in buildings)
right = max(b[RIGHT] for b in buildings)
last_height = None
output = []
for i in range(left, right + 1):
heights = [b[HEIGHT] for b in buildings if b[LEFT] <= i < b[RIGHT]]
height = max(heights) if heights else 0
if height != last_height:
output += [i, height]
last_height = height
return output
if __name__ == '__main__':
assert(skyline(buildings) == [1, 11, 3, 13, 9, 0, 12, 7, 16, 3, 19, 18, 22, 3, 23, 13, 29, 0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment