Skip to content

Commit

Permalink
Add q26
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Jul 24, 2024
1 parent 3d320bc commit 164e681
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ex/q026.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
;; https://4clojure.oxal.org/#/problem/026

(ns ex.q026)

(defn -penultimate
"Return penultimate element or 0 if not enough elements"
[coll]
(if (> (count coll) 1)
(->> coll reverse second)
0))

(defn ans
"Problem 26, Fibonacci Sequence"
[n]
(loop [cnt 1 fib [1]]
(if (>= cnt n)
fib
(recur (inc cnt) (conj fib (+ (last fib) (-penultimate fib)))))))
11 changes: 11 additions & 0 deletions test/ex/q026_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; https://4clojure.oxal.org/#/problem/026

(ns ex.q026-test
(:require [clojure.test :as t]
[ex.q026 :as sut]))

(t/deftest test-q26
(t/testing "Problem 26, Fibonacci Sequence"
(t/is (= '(1 1 2) (sut/ans 3)))
(t/is (= '(1 1 2 3 5 8) (sut/ans 6)))
(t/is (= '(1 1 2 3 5 8 13 21) (sut/ans 8)))))

0 comments on commit 164e681

Please sign in to comment.