Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;http://www.4clojure.com/problem/43
- ;Write a function which reverses the interleave process into x number of subsequences.
- ;(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
- ;(= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8)))
- ;(= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))
- (defn reverse-interleave
- [some-seq n]
- (take n (map-indexed
- (fn
- [index, item]
- (let [new-seq []]
- (loop [start-index index
- sub-seq []]
- (if (< (count sub-seq) (/ (count some-seq) n))
- (recur (+ start-index n) (conj sub-seq (get (into [] some-seq) start-index)))
- (flatten (conj new-seq sub-seq))))))
- some-seq)))
- (reverse-interleave [1 2 3 4 5 6] 2)
- (reverse-interleave (range 9) 3)
- (reverse-interleave (range 10) 5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement