Reverse Interleave
Difficulty: | Medium |
Topics: | seqs |
Write a function which reverses the interleave process into x number of subsequences.
解决方案:如果简单的满足以上条件,即序列的大小刚好是间隔位置的倍数,则答案可以很简单
(fn [v n]
(apply map list (partition n v)))
但是如果序列的大小和间隔位置数不是倍数关系,则需要填充nil到原来的序列中,使得满足倍数的关系,然后再map应用函数的过程中剔除nil。
(ns for-clojure.problem43)
(defn partition-seq-with-n-element
[v n]
(apply map (fn [& x] (remove nil? x))
(let [cnt (count v)
size (int (Math/ceil (/ cnt n)))]
(partition n
(concat v (repeat (- (* n size) cnt) nil))))))
(partition-seq-with-n-element [1 2 3 4 5 6 7] 2)
这里一个为什么使用了remove 而不是drop-while因为drop-while只是drop那先返回nil的元素,而(nil? nil)为true,所以不会被删除。
remove删除的是返回true的元素,而返回nil的元素是不会被删除的,所以这里正好要使用remove。
partition-all与partition的区别 partition把序列分成等大的片段,多余的会被舍弃,而partition-all会把多余的元素保留。