From 33a9f3e7f7b7231e04ede170a488f1162e28ae17 Mon Sep 17 00:00:00 2001 From: Joni Toivonen Date: Sat, 21 Oct 2017 16:29:31 +0300 Subject: [PATCH] Fix code so that tests pass --- .travis.yml | 4 +- src/one_function_to_rule_them_all.clj | 80 ++++++++++++++++++++------- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/.travis.yml b/.travis.yml index 455f3c0..45c29f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: clojure -lein: lein2 -script: lein2 midje :config .midje-grading-config.clj +lein: lein +script: lein midje :config .midje-grading-config.clj jdk: - openjdk7 notifications: diff --git a/src/one_function_to_rule_them_all.clj b/src/one_function_to_rule_them_all.clj index 6844d99..bd125f8 100644 --- a/src/one_function_to_rule_them_all.clj +++ b/src/one_function_to_rule_them_all.clj @@ -1,43 +1,85 @@ (ns one-function-to-rule-them-all) (defn concat-elements [a-seq] - :-) + (reduce concat '() a-seq)) (defn str-cat [a-seq] - :-) + (if (empty? a-seq) + "" + (reduce (fn [word1 word2] + (str word1 " " word2)) + a-seq))) (defn my-interpose [x a-seq] - [:-]) + (if (empty? a-seq) + '() + (reduce (fn [acc elem] + (conj acc x elem)) + (conj [] (first a-seq)) + (rest a-seq)))) (defn my-count [a-seq] - :-) + (reduce (fn [accumulator element] (+ accumulator 1)) 0 a-seq)) (defn my-reverse [a-seq] - [:-]) + (reduce (fn [accumulator element] (conj accumulator element)) '() a-seq)) (defn min-max-element [a-seq] - [:-]) + (reduce (fn [[acc-min acc-max] element] + (vector (min acc-min element) + (max acc-max element))) + (vector (first a-seq) (first a-seq)) + (rest a-seq))) (defn insert [sorted-seq n] - [:-]) + (let [helper (loop [counter 0] + (if (or (= counter (count sorted-seq)) + (< n (nth sorted-seq counter))) + counter + (recur (inc counter))))] + (concat (take helper sorted-seq) + (conj (drop helper sorted-seq) n)))) (defn insertion-sort [a-seq] - [:-]) + (let [insertion (fn [sorted-seq + elem] + (insert sorted-seq elem))] + (reduce insertion [] a-seq))) (defn parity [a-seq] - [:-]) + (let [seq-frequencies (frequencies a-seq) + odd-keys (fn [odd-seq elem] + (if (odd? (val elem)) + (conj odd-seq (key elem))))] + (set (reduce odd-keys [] seq-frequencies)))) -(defn minus [x] - :-) +(defn minus + ([x] (- 0 x)) + ([x y] (- x y))) -(defn count-params [x] - :-) +(defn count-params [& more] + (my-count more)) -(defn my-* [x] - :-) +(defn my-* [& more] + (reduce * 1 more)) -(defn pred-and [x] - (fn [x] :-)) +(defn pred-and + ([] (fn [x] true)) + ([pred1?] (pred1?)) + ([pred1? pred2?] (fn [x] (and (pred1? x) + (pred2? x)))) + ([pred1? pred2? & more] (reduce pred-and (pred-and pred1? pred2?) more))) -(defn my-map [f a-seq] - [:-]) \ No newline at end of file +(defn my-map + ([f a-seq] + (reduce (fn [processed elem] + (conj processed (f elem))) + [] + a-seq)) + ([f a-seq & rest-seqs] + (let [seqs (concat [a-seq] rest-seqs)] + (reduce (fn [processed index] + (conj processed + (apply f (my-map (fn [elem] (nth elem index)) seqs)))) + [] + (range (apply min (my-map count seqs)))))))