I just stumbled upon this little problem at UVA Online Judge and thought it might be a good candidate for a little code golf.
Problem:
You must develop a program that helps the architect draw the city skyline based on the location of the buildings in the city. To make the problem acceptable, all buildings have a rectangular shape and have a common bottom (the city in which they are built is very flat). The city is also regarded as two-dimensional. The structure is defined by an ordered triple (Li, Hi, Ri) , where Li and Ri are the left and right coordinates of the building, and Hi is the height of the building.

In the diagram below, the buildings are shown on the left with triples.
(1,11,5), (2,6,7), (3,13,9), (12,7,16), (14,3,25), (19,18,22), (23,13,29), (24,4,28)
and the horizon shown on the right is represented by the sequence:
1, 11, 3, 13, 9, 0, 12, 7, 16, 3, 19, 18, 22, 3, 23, 13, 29, 0
The output should consist of a vector that describes the horizon line, as shown in the example above. In the vector horizon (v1, v2, v3, ... vn) vi , so that I an even number represents a horizontal line (height). vi so i am an odd number is a vertical line (x-coordinate). The horizon vector should be a “path” taken, for example, by an error starting at the minimum x coordinate and moving horizontally and vertically along all the lines that define the horizon. Thus, the last record in the horizon vector must be 0. The coordinates must be separated by a space.
Unless I read the declaration of the provided (test) buildings and including all spaces and tabs, my solution in Python lasts 223 .
Here is the compressed version:
B=[[1,11,5],[2,6,7],[3,13,9],[12,7,16],[14,3,25],[19,18,22],[23,13,29],[24,4,28]] # Solution. R=range v=[0 for e in R(max([y[2] for y in B])+1)] for b in B: for x in R(b[0], b[2]): if b[1]>v[x]: v[x]=b[1] p=1 k=0 for x in R(len(v)): V=v[x] if p and V==0: continue elif V!=k: p=0 print "%s %s" % (str(x), str(V)), k=V
I think I was not mistaken, but if so, feel free to criticize me.
I don’t have a great reputation, so I’ll pay only 100 for generosity - I’m curious if anyone can try to solve this in less than ... say 80 characters. The solution submitted by cobbal is 101 characters long and is currently the best.
I thought that 80 characters is a sore point for this problem. cobbal , with its 46-character solution, struck me, although I have to admit that I read his explanation for a while before I partially understood what he wrote.