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

Submission #622

Open
wants to merge 1 commit 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
60 changes: 52 additions & 8 deletions src/looping_is_recursion.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
(ns looping-is-recursion)

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


(defn last-element [a-seq]
":(")
(let [last-element-helper
(fn [a-seq last]
(if a-seq
(recur (next a-seq) (first a-seq))
last))]
(last-element-helper a-seq nil)))

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

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

(defn avg [a-seq]
-1)
(loop [acc 0
cnt 0
seq a-seq]
(if (empty? seq)
(/ acc cnt)
(recur (+ (first seq) acc) (inc cnt) (rest seq)))))

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

(defn fast-fibo [n]
":(")
(loop [last 0
this 1
count 2]
(cond
(< n 2) n
(= n count) (+ this last)
:else (recur this (+ this last) (inc count)))))


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