Skip to content

Commit

Permalink
Add q31
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Aug 2, 2024
1 parent a6379bb commit 6f7cf60
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ A place to keep track of problems/solutions in Clojure.

The problems are from the [4clojure](https://4clojure.oxal.org).


## Notes

Handy `clojure.core` functions to keep in mind:

* [`first`](https://clojuredocs.org/clojure.core/first), [`second`](https://clojuredocs.org/clojure.core/second), [`rest`](https://clojuredocs.org/clojure.core/rest), [`nth`](https://clojuredocs.org/clojure.core/nth)
* [`partition-by`](https://clojuredocs.org/clojure.core/partition-by) and [`group-by`](https://clojuredocs.org/clojure.core/group-by)


## Workflow

1. Get [`just`](https://github.com/casey/just) (`brew install just`)
Expand Down
8 changes: 8 additions & 0 deletions src/ex/q031.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
;; https://4clojure.oxal.org/#/problem/031

(ns ex.q031)

(defn ans
"Problem 31, Pack a Sequence. (pack consecutive duplicates into sub-lists)"
[coll]
(partition-by identity coll))
11 changes: 11 additions & 0 deletions test/ex/q031_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; https://4clojure.oxal.org/#/problem/031

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

(t/deftest test-q31
(t/testing "Problem 31, Pack a Sequence"
(t/is (= '((1 1) (2) (1 1 1) (3 3)) (sut/ans [1 1 2 1 1 1 3 3])))
(t/is (= '((:a :a) (:b :b) (:c)) (sut/ans [:a :a :b :b :c])))
(t/is (= '(([1 2] [1 2]) ([3 4])) (sut/ans [[1 2] [1 2] [3 4]])))))

0 comments on commit 6f7cf60

Please sign in to comment.