Advertisement
CLooker

Untitled

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