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

looping-is-recursion Done... #611

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
47 changes: 39 additions & 8 deletions src/looping_is_recursion.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
(ns looping-is-recursion)

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

(defn last-element [a-seq]
":(")
(when-let [[x & xs] (seq a-seq)]
(if xs (recur xs) x)))

(defn seq= [seq1 seq2]
":(")
(cond (and (empty? seq1) (empty? seq2)) true
(empty? seq1) false
(empty? seq2) false
(= (first seq1) (first seq2)) (recur (rest seq1) (rest seq2))
:else false
))

(defn find-first-index [pred a-seq]
":(")
(loop [ix 0, a-seq (seq a-seq)]
(when a-seq
(if (pred (first a-seq))
ix
(recur (inc ix) (next a-seq))
))))

(defn avg [a-seq]
-1)
(loop [sum 0, n 0, a-seq a-seq]
(cond
(empty? a-seq) (if (> n 0) (/ sum n) 0)
:else (recur (+ sum (first a-seq)) (inc n) (rest a-seq))
)))

(defn parity [a-seq]
":(")
(loop [odd-set #{}, a-seq a-seq]
(cond (empty? a-seq) odd-set
(contains? odd-set (first a-seq)) (recur (disj odd-set (first a-seq)) (rest a-seq))
:else (recur (conj odd-set (first a-seq)) (rest a-seq)))))

(defn fast-fibo [n]
":(")
(loop [a 0, b 1, n n]
(if (zero? n)
a
(recur b (+ a b) (dec n)))))

(defn cut-at-repetition [a-seq]
[":("])
(loop [seen #{}, result [], a-seq a-seq]
(cond (empty? a-seq) result
(contains? seen (first a-seq)) result
:else (recur (conj seen (first a-seq))
(conj result (first a-seq))
(rest a-seq)))))