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 done #628

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
77 changes: 69 additions & 8 deletions src/looping_is_recursion.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,87 @@
(ns looping-is-recursion)

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

(defn last-element [a-seq]
":(")
(let [helper (fn [a-seq]
(if (= 0 (count a-seq))
nil
(if (= 1 (count a-seq))
(first a-seq)
(recur (rest a-seq)))))]
(helper a-seq)))

(defn seq= [seq1 seq2]
":(")
(let [helper (fn [a-seq b-seq]
(if (not (= (count a-seq) (count b-seq)))
false
(if (= 0 (count a-seq))
true
(if (not (= (first a-seq) (first b-seq)))
false
(recur (rest a-seq) (rest b-seq))))))]
(helper seq1 seq2)))


(defn find-first-index [pred a-seq]
":(")
(loop [p? pred
s a-seq
idx 0]
(if (= 0 (count s))
nil
(if (p? (first s))
idx
(recur p? (rest s) (inc idx))))))

(defn avg [a-seq]
-1)
(loop [s a-seq
cnt 0
sum 0]
(if (= 0 (count s))
(/ sum cnt)
(recur (rest s) (inc cnt) (+ sum (first s))))))


;procudes result in different order than the example but doesn't seem to matter for the test
(defn parity [a-seq]
":(")
(loop [s a-seq
res (set '())] ;must be a set
(if (= 0 (count s))
res
(let [even (disj res (first s))
odd (conj res (first s))]
(if (some #(= (first s) %) res) ;kind of contains? for a list
(recur (rest s) even)
(recur (rest s) odd))))))


(defn fast-fibo [n]
":(")
(loop [ix 2
f_n 1
f_n1 0]
(cond
(= 0 n)
0
(= 1 n)
1
(= ix n)
(+ f_n f_n1);this produces the final Fn = fn-1 + fn-2
:else
(recur (inc ix) (+ f_n f_n1) f_n)))) ;calculate new fn and the prev fn will be the new fn-1


(defn cut-at-repetition [a-seq]
[":("])
(loop [s a-seq
res []]
(if (= 0 (count s))
res
(if (some #(= (first s) %) res) ;kind of contains? for a list
res
(recur (rest s) (conj res (first s)))))))