Water collected between towers: Difference between revisions

Content added Content deleted
(added Tcl)
(added Scheme example)
Line 1,060: Line 1,060:
0
0
</pre>
</pre>

=={{header|Scheme}}==

<lang scheme>
(import (scheme base)
(scheme write))

(define (total-collected chart)
(define (highest-right vals)
(if (null? (cdr vals))
'(0)
(cons (apply max (cdr vals))
(highest-right (cdr vals)))))
(define (highest-left vals)
(reverse (highest-right (reverse vals))))
;
(if (< (length chart) 3) ; catch the end cases
0
(apply +
(map (lambda (l c r)
(if (or (<= l c)
(<= r c))
0
(- (min l r) c)))
(highest-left chart)
chart
(highest-right chart)))))

(for-each
(lambda (chart)
(display chart) (display " -> ") (display (total-collected chart)) (newline))
'((1 5 3 7 2)
(5 3 7 2 6 4 5 9 1 2)
(2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1)
(5 5 5 5)
(5 6 7 8)
(8 7 7 6)
(6 7 10 7 6)))
</lang>

{{out}}
<pre>
(1 5 3 7 2) -> 2
(5 3 7 2 6 4 5 9 1 2) -> 14
(2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1) -> 35
(5 5 5 5) -> 0
(5 6 7 8) -> 0
(8 7 7 6) -> 0
(6 7 10 7 6) -> 0
(3 1 2) -> 1
(1) -> 0
() -> 0
(1 2) -> 0
</pre>

=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func max_l(Array a, m = a[0]) {
<lang ruby>func max_l(Array a, m = a[0]) {