How do I replicate items from a list in Clojure? -
i've tried many nights i've given on myself. seems extremely simple problem, guess i'm not understanding clojure should (i partially attribute sole experience imperative languages). problem hackerrank.com
here problem:
problem statement given list repeat each element of list n times. input , output portions handled automatically grader. input format first line has integer s s number of times need repeat elements. after there x lines, each containing integer. these x elements of array. output format repeat each element of original list s times. have return list/vector/array of s*x integers. relative positions of values should same original list provided input. constraints 0<=x<=10 1<=s<=100 so, given:
2 1 2 3 output:
1 1 2 2 3 3 i've tried:
(fn list-replicate [num list] (println (reduce (fn [element seq] (dotimes [n num] (conj seq element))) [] list)) ) but gives me exception. i've tried many other solutions, , isn't 1 of better ones, quickest 1 come post here.
(defn list-replicate [num list] (mapcat (partial repeat num) list)) (doseq [x (list-replicate 2 [1 2 3])] (println x)) ;; output: 1 1 2 2 3 3
Comments
Post a Comment