mirror of
https://github.com/janet-lang/janet
synced 2024-12-25 16:00:27 +00:00
73ead5c2de
and put more emphasis on indexed data-structure combinators instead of iterators.
18 lines
440 B
Plaintext
18 lines
440 B
Plaintext
|
|
(import "examples/iterators.dst")
|
|
|
|
(defn sum3
|
|
"Solve the 3SUM problem O(n^2) time."
|
|
[s]
|
|
(def tab @{})
|
|
(def solutions @{})
|
|
(def len (length s))
|
|
(for [k 0 len]
|
|
(put tab (get s k) k))
|
|
(for [i 0 len]
|
|
(for [j 0 len]
|
|
(def k (get tab (- 0 (get s i) (get s j))))
|
|
(when (and k (not= k i) (not= k j) (not= i j))
|
|
(put solutions {i true j true k true} true))))
|
|
(iter2array (map (fn [x] (iter2array (keys x))) (keys solutions))))
|