mirror of
https://github.com/janet-lang/janet
synced 2024-12-25 16:00:27 +00:00
1110267c9d
add extremes functions (max, min, order-max, order-min, extreme)
25 lines
453 B
Plaintext
25 lines
453 B
Plaintext
# Find the maximum path from the top (root)
|
|
# of the triangle to the leaves of the triangle.
|
|
|
|
(defn myfold [xs ys]
|
|
(def xs1 (tuple-prepend xs 0))
|
|
(def xs2 (tuple-append xs 0))
|
|
(def m1 (map + xs1 ys))
|
|
(def m2 (map + xs2 ys))
|
|
(map max m1 m2))
|
|
|
|
(defn maxpath [t]
|
|
(extreme > (reduce myfold () t)))
|
|
|
|
# Test it
|
|
# Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25
|
|
|
|
(def triangle @[
|
|
@[3]
|
|
@[7 10]
|
|
@[4 3 7]
|
|
@[8 9 1 3]
|
|
])
|
|
|
|
(print (maxpath triangle))
|