From a5bf35580404dc598082d618c3b8060c53907d56 Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 17:59:35 +0300 Subject: [PATCH 1/8] power, last-element --- .gitignore | 2 ++ .nrepl-port | 1 + looping-is-recursion-1.iml | 69 ++++++++++++++++++++++++++++++++++++ looping-is-recursion.iml | 11 ++++++ src/looping_is_recursion.clj | 18 ++++++++-- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 .nrepl-port create mode 100644 looping-is-recursion-1.iml create mode 100644 looping-is-recursion.iml diff --git a/.gitignore b/.gitignore index ee508f7..98a2a4b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ pom.xml .lein-deps-sum .lein-failures .lein-plugins +/.idea +.lein-repl-history diff --git a/.nrepl-port b/.nrepl-port new file mode 100644 index 0000000..c48cdde --- /dev/null +++ b/.nrepl-port @@ -0,0 +1 @@ +42923 \ No newline at end of file diff --git a/looping-is-recursion-1.iml b/looping-is-recursion-1.iml new file mode 100644 index 0000000..9ac976c --- /dev/null +++ b/looping-is-recursion-1.iml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/looping-is-recursion.iml b/looping-is-recursion.iml new file mode 100644 index 0000000..d5ac004 --- /dev/null +++ b/looping-is-recursion.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index 199a5ce..b1286d0 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -1,10 +1,24 @@ (ns looping-is-recursion) (defn power [base exp] - ":(") + (cond + (zero? base) + 0 + (= exp 0) + 1 + :else + (let [helper (fn [acc n] + (if (= n 1) + acc + (recur (* acc base) (dec n))))] + (helper base exp)))) (defn last-element [a-seq] - ":(") + (let [helper (fn [acc the-seq] + (if (empty? the-seq) + acc + (recur (first the-seq) (rest the-seq))))] + (helper (first a-seq) (rest a-seq)))) (defn seq= [seq1 seq2] ":(") From b0cd8396c12ba841a24b4a2e666381316fbce005 Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 19:27:52 +0300 Subject: [PATCH 2/8] seq= --- src/looping_is_recursion.clj | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index b1286d0..9bd6f72 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -21,7 +21,15 @@ (helper (first a-seq) (rest a-seq)))) (defn seq= [seq1 seq2] - ":(") + (let [helper (fn [acc new-seq1 new-seq2] + (cond + (and (empty? new-seq1) (empty? new-seq2)) + acc + (or (empty? new-seq1) (empty? new-seq2)) + false + :else + (recur (= (first new-seq1) (first new-seq2)) (rest new-seq1) (rest new-seq2))))] + (helper true seq1 seq2))) (defn find-first-index [pred a-seq] ":(") From 08d65e9c1fb00bd3b80cb85b68a1c6820e2f496f Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 20:08:36 +0300 Subject: [PATCH 3/8] find-first-index --- src/looping_is_recursion.clj | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index 9bd6f72..f26b735 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -32,7 +32,17 @@ (helper true seq1 seq2))) (defn find-first-index [pred a-seq] - ":(") + (loop [index 0 + the-pred pred + the-seq a-seq + ] + (cond + (empty? the-seq) + nil + (the-pred (first the-seq)) + index + :else + (recur (inc index) the-pred (rest the-seq))))) (defn avg [a-seq] -1) From 8e972c31e8069de75fe9fc283d97ed7126cd8a91 Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 20:19:26 +0300 Subject: [PATCH 4/8] avg --- src/looping_is_recursion.clj | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index f26b735..17a6e61 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -45,7 +45,15 @@ (recur (inc index) the-pred (rest the-seq))))) (defn avg [a-seq] - -1) + (loop [the-sum 0 + the-count 0 + the-seq a-seq + ] + (if (empty? the-seq) + (if (zero? the-count) + nil + (/ the-sum the-count)) + (recur (+ the-sum (first the-seq)) (inc the-count) (rest the-seq))))) (defn parity [a-seq] ":(") From 56e60bfffa39365b8faff090954b6793dcd4d24c Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 20:57:48 +0300 Subject: [PATCH 5/8] parity --- src/looping_is_recursion.clj | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index 17a6e61..70deb4c 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -55,8 +55,19 @@ (/ the-sum the-count)) (recur (+ the-sum (first the-seq)) (inc the-count) (rest the-seq))))) +(defn toggle [a-set elem] + (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem))) + (defn parity [a-seq] - ":(") + (loop [result-set #{} + rest-of-set a-seq + ] + (if (empty? rest-of-set) + result-set + (recur (toggle result-set (first rest-of-set)) + (rest rest-of-set))))) (defn fast-fibo [n] ":(") From b0b78a2ea1ef35e914cf5bba4d28507b3f21e8c1 Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 21:42:59 +0300 Subject: [PATCH 6/8] fast-fibo --- src/looping_is_recursion.clj | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index 70deb4c..a1a5101 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -62,15 +62,21 @@ (defn parity [a-seq] (loop [result-set #{} - rest-of-set a-seq - ] + rest-of-set a-seq] (if (empty? rest-of-set) result-set (recur (toggle result-set (first rest-of-set)) (rest rest-of-set))))) (defn fast-fibo [n] - ":(") + (if (< n 2) + n + (loop [index 2 + f-n-1 1 + f-n 1] + (if (= index n) + f-n + (recur (inc index) f-n (+ f-n-1 f-n)))))) (defn cut-at-repetition [a-seq] [":("]) From 6cff0e540b60aa70375c5c55e5621648e9da32be Mon Sep 17 00:00:00 2001 From: epicharri Date: Fri, 21 Jul 2017 23:51:36 +0300 Subject: [PATCH 7/8] cut-at-repetition --- src/looping_is_recursion.clj | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index a1a5101..a36f6a5 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -79,5 +79,13 @@ (recur (inc index) f-n (+ f-n-1 f-n)))))) (defn cut-at-repetition [a-seq] - [":("]) + (loop [result-seq [] + remaining-seq a-seq] + (cond + (empty? remaining-seq) + result-seq + (true? (some #(= (first remaining-seq) %) result-seq)) + result-seq + :else + (recur (conj result-seq (first remaining-seq)) (rest remaining-seq))))) From acf0931af34920ae6331da95e63e46ba1dfbb6d2 Mon Sep 17 00:00:00 2001 From: epicharri Date: Sat, 22 Jul 2017 00:08:55 +0300 Subject: [PATCH 8/8] looping-is-recursion complete --- src/looping_is_recursion.clj | 142 +++++++++++++++++------------------ 1 file changed, 71 insertions(+), 71 deletions(-) diff --git a/src/looping_is_recursion.clj b/src/looping_is_recursion.clj index a36f6a5..023297d 100644 --- a/src/looping_is_recursion.clj +++ b/src/looping_is_recursion.clj @@ -1,91 +1,91 @@ (ns looping-is-recursion) (defn power [base exp] - (cond - (zero? base) - 0 - (= exp 0) - 1 - :else - (let [helper (fn [acc n] - (if (= n 1) - acc - (recur (* acc base) (dec n))))] - (helper base exp)))) + (cond + (zero? base) + 0 + (= exp 0) + 1 + :else + (let [helper (fn [acc n] + (if (= n 1) + acc + (recur (* acc base) (dec n))))] + (helper base exp)))) (defn last-element [a-seq] - (let [helper (fn [acc the-seq] - (if (empty? the-seq) - acc - (recur (first the-seq) (rest the-seq))))] - (helper (first a-seq) (rest a-seq)))) + (let [helper (fn [acc the-seq] + (if (empty? the-seq) + acc + (recur (first the-seq) (rest the-seq))))] + (helper (first a-seq) (rest a-seq)))) (defn seq= [seq1 seq2] - (let [helper (fn [acc new-seq1 new-seq2] - (cond - (and (empty? new-seq1) (empty? new-seq2)) - acc - (or (empty? new-seq1) (empty? new-seq2)) - false - :else - (recur (= (first new-seq1) (first new-seq2)) (rest new-seq1) (rest new-seq2))))] - (helper true seq1 seq2))) + (let [helper (fn [acc new-seq1 new-seq2] + (cond + (and (empty? new-seq1) (empty? new-seq2)) + acc + (or (empty? new-seq1) (empty? new-seq2)) + false + :else + (recur (= (first new-seq1) (first new-seq2)) (rest new-seq1) (rest new-seq2))))] + (helper true seq1 seq2))) (defn find-first-index [pred a-seq] - (loop [index 0 - the-pred pred - the-seq a-seq - ] - (cond - (empty? the-seq) - nil - (the-pred (first the-seq)) - index - :else - (recur (inc index) the-pred (rest the-seq))))) + (loop [index 0 + the-pred pred + the-seq a-seq + ] + (cond + (empty? the-seq) + nil + (the-pred (first the-seq)) + index + :else + (recur (inc index) the-pred (rest the-seq))))) (defn avg [a-seq] - (loop [the-sum 0 - the-count 0 - the-seq a-seq - ] - (if (empty? the-seq) - (if (zero? the-count) - nil - (/ the-sum the-count)) - (recur (+ the-sum (first the-seq)) (inc the-count) (rest the-seq))))) + (loop [the-sum 0 + the-count 0 + the-seq a-seq + ] + (if (empty? the-seq) + (if (zero? the-count) + nil + (/ the-sum the-count)) + (recur (+ the-sum (first the-seq)) (inc the-count) (rest the-seq))))) (defn toggle [a-set elem] - (if (contains? a-set elem) - (disj a-set elem) - (conj a-set elem))) + (if (contains? a-set elem) + (disj a-set elem) + (conj a-set elem))) (defn parity [a-seq] - (loop [result-set #{} - rest-of-set a-seq] - (if (empty? rest-of-set) - result-set - (recur (toggle result-set (first rest-of-set)) - (rest rest-of-set))))) + (loop [result-set #{} + rest-of-set a-seq] + (if (empty? rest-of-set) + result-set + (recur (toggle result-set (first rest-of-set)) + (rest rest-of-set))))) (defn fast-fibo [n] - (if (< n 2) - n - (loop [index 2 - f-n-1 1 - f-n 1] - (if (= index n) - f-n - (recur (inc index) f-n (+ f-n-1 f-n)))))) + (if (< n 2) + n + (loop [index 2 + f-n-1 1 + f-n 1] + (if (= index n) + f-n + (recur (inc index) f-n (+ f-n-1 f-n)))))) (defn cut-at-repetition [a-seq] - (loop [result-seq [] - remaining-seq a-seq] - (cond - (empty? remaining-seq) - result-seq - (true? (some #(= (first remaining-seq) %) result-seq)) - result-seq - :else - (recur (conj result-seq (first remaining-seq)) (rest remaining-seq))))) + (loop [result-seq [] + remaining-seq a-seq] + (cond + (empty? remaining-seq) + result-seq + (true? (some #(= (first remaining-seq) %) result-seq)) + result-seq + :else + (recur (conj result-seq (first remaining-seq)) (rest remaining-seq)))))