Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All completed #609

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions src/looping_is_recursion.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
(ns looping-is-recursion)

(defn power [base exp]
":(")
(let [helper (fn [acc n]
(if (zero? n)
acc
(recur (* acc base) (dec n))))]
(helper 1 exp)))

(defn last-element [a-seq]
":(")
(let [helper (fn [a-seq]
(if (empty? (rest a-seq))
(first a-seq)
(recur (rest a-seq))))]
(helper a-seq)))

(defn seq= [seq1 seq2]
":(")
(let [helper (fn [a b]
(cond
(or (empty? a) (empty? b))
(if (and (empty? a) (empty? b))
true
false)
(== (first a) (first b))
(recur (rest a) (rest b))
:else false))]
(helper seq1 seq2)))

(defn find-first-index [pred a-seq]
":(")
(loop [acc 0 a a-seq]
(cond
(empty? a)
nil
(pred (first a))
acc
:else (recur (inc acc) (rest a)))))

(defn avg [a-seq]
-1)
(loop [acc 0 size 0 new-seq a-seq]
(cond
(empty? new-seq)
(if (< 0 size) (/ acc size) 0)
:else (recur (+ acc (first new-seq)) (inc size) (rest new-seq)))))

(defn parity [a-seq]
":(")
(let [toggle (fn [a-set elem]
(if (contains? a-set elem)
(disj a-set elem)
(conj a-set elem)))]
(loop [result #{} new-seq a-seq]
(if (empty? new-seq)
result
(recur (toggle result (first new-seq)) (rest new-seq))))))

(defn fast-fibo [n]
":(")
(cond
(== 0 n)
0
(== 1 n)
1
:else (loop [fn-1 1 fn-2 0 at 2]
(if (== at n)
(+ fn-1 fn-2)
(recur (+ fn-1 fn-2) fn-1 (inc at))))))

(defn cut-at-repetition [a-seq]
[":("])

(loop [result [] checker #{} new-seq a-seq]
(cond
(empty? new-seq)
result
(contains? checker (first new-seq))
result
:else (recur (conj result (first new-seq)) (conj checker (first new-seq)) (rest new-seq)))))