[ create a new paste ] login | about

Link: http://codepad.org/0YYexYt9    [ raw code | output | fork ]

programmingpraxis - Scheme, pasted on Sep 4:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
; skyline puzzle

(define (heights buildings)
  (let* ((len (apply max (map caddr buildings)))
         (hites (make-vector (+ len 1) 0)))
    (do ((buildings buildings (cdr buildings)))
        ((null? buildings) (vector->list hites))
      (let ((building (car buildings)))
        (do ((x (car building) (+ x 1))) ((= x (caddr building)))
          (vector-set! hites x
            (max (vector-ref hites x) (cadr building))))))))

(define (skyline buildings)
  (let loop ((x 0) (prev 0) (hites (heights buildings)) (skys (list)))
    (cond ((null? hites) (reverse skys))
          ((= (car hites) prev) (loop (+ x 1) prev (cdr hites) skys))
          (else (loop (+ x 1) (car hites) (cdr hites) (cons (car hites) (cons x skys)))))))

(display (skyline '((1 11 5) (2 6 7) (3 13 9) (12 7 16) (14 3 25) (19 18 22) (23 13 29) (24 4 28))))


Output:
1
(1 11 3 13 9 0 12 7 16 3 19 18 22 3 23 13 29 0)


Create a new paste based on this one


Comments: