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

from ke #619

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

(defn power [base exp]
":(")
(let [pow (fn [res n k]
(if (= k 0)
res
(recur (* res n) n (dec k))))]
(pow 1 base exp)))

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

(defn seq= [seq1 seq2]
":(")
(let [s (fn [rest-seq1 rest-seq2]
(if (not (= (count rest-seq1)(count rest-seq2)))
false
(if (and (empty? rest-seq1) (empty? rest-seq2))
true
(if (= (first rest-seq1) (first rest-seq2))
(recur (rest rest-seq1)(rest rest-seq2))
false))))]
(s seq1 seq2)))

(defn find-first-index [pred a-seq]
":(")
(loop [pre pred sq a-seq ind 0]
(if (empty? sq)
nil
(if (pre (first sq))
ind
(recur pre (rest sq) (+ ind 1))))))

(defn avg [a-seq]
-1)
(loop [sq a-seq sum 0 size (count a-seq)]
(if (empty? sq)
(/ sum size)
(recur (rest sq) (+ sum (first sq)) size))))

(defn toggle [a-set elem]
(if (contains? a-set elem)
(disj a-set elem)
(conj a-set elem)))

(defn parity [a-seq]
":(")
(loop [sq a-seq ret-set (set [])]
(if (empty? sq)
ret-set
(recur (rest sq) (toggle ret-set (first sq))))))

(defn fast-fibo [n]
":(")
(cond
(= n 0) 0
(= n 1) 1
:else
(loop [number n i 2 f-1 1 f-2 0]
(if (= number i)
(+ f-1 f-2)
(recur number (+ i 1)(+ f-1 f-2) f-1)))))


(defn cut-at-repetition [a-seq]
[":("])
(loop [sq a-seq test-set (set []) result (vec nil)]
(if (or (contains? test-set (first sq)) (empty? sq))
result
(recur (rest sq) (conj test-set (first sq)) (conj result (first sq))))))