Skip to content

Commit

Permalink
Add q21
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Jul 21, 2024
1 parent a58365c commit f07dd8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ex/q021.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; https://4clojure.oxal.org/#/problem/021

(ns ex.q021)

(defn ans
"Problem 21, Nth Element. restriction: nth"
[coll index]
(.get coll index))

(defn ans2
"another take, using `drop`"
[coll index]
(->> coll (drop index) first))
19 changes: 19 additions & 0 deletions test/ex/q021_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
;; https://4clojure.oxal.org/#/problem/021

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

(t/deftest test-q21-a
(t/testing "Problem 21, Nth Element"
(t/is (= 6 (sut/ans '(4 5 6 7) 2)))
(t/is (= :a (sut/ans [:a :b :c] 0)))
(t/is (= 2 (sut/ans [1 2 3 4] 1)))
(t/is (= [5 6] (sut/ans '([1 2] [3 4] [5 6]) 2)))))

(t/deftest test-q21-b
(t/testing "Problem 21, Nth Element"
(t/is (= 6 (sut/ans2 '(4 5 6 7) 2)))
(t/is (= :a (sut/ans2 [:a :b :c] 0)))
(t/is (= 2 (sut/ans2 [1 2 3 4] 1)))
(t/is (= [5 6] (sut/ans2 '([1 2] [3 4] [5 6]) 2)))))

0 comments on commit f07dd8e

Please sign in to comment.