-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |