Rename seq abstraction to iterator. Add random functions.

This commit is contained in:
Calvin Rose
2018-03-15 17:19:31 -04:00
parent 5f0bd1e082
commit 5738f6c8b1
5 changed files with 64 additions and 55 deletions
+3 -3
View File
@@ -2,7 +2,7 @@
"Solve the 3SUM problem O(n^2) time."
[s]
(def tab @{})
(def solutions [])
(def solutions @{})
(def len (length s))
(for [k 0 len]
(put tab (get s k) k))
@@ -10,5 +10,5 @@
(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))
(array-push solutions [i j k]))))
solutions)
(put solutions {i true j true k true} true))))
(iter2array (map (fn [x] (iter2array (keys x))) (keys solutions))))
+3 -4
View File
@@ -4,8 +4,7 @@
"Get the number of occurences of each value in a sequence."
[s]
(let [freqs @{}
fop (fn [x]
(let [n (get freqs x)]
(put freqs x (if n (+ 1 n) 1))))
_ (domap fop s)]
_ (foreach s (fn [x]
(let [n (get freqs x)]
(put freqs x (if n (+ 1 n) 1)))))]
freqs))