-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence-of-Pronunciations.clj
26 lines (18 loc) · 1.12 KB
/
Sequence-of-Pronunciations.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
;; dkawashima's solution to Sequence of Pronunciations #110
;; https://4clojure.com/problem/110
;; Write a function that returns a lazy sequence of "pronunciations" of a sequence of numbers.
;; A pronunciation of each element in the sequence consists of the number of repeating identical
;; numbers and the number itself. For example, [1 1] is pronounced as [2 1] ("two ones"),
;; which in turn is pronounced as [1 2 1 1] ("one two, one one").
;; Your function should accept an initial sequence of numbers, and return an infinite lazy sequence of pronunciations,
;; each element being a pronunciation of the previous element.
(defn sequenceofPronunciations
[vect]
(let [resVec (vec (interleave (map count (partition-by identity vect)) (flatten (map distinct (partition-by identity vect)))))]
(cons resVec (lazy-seq (sequenceofPronunciations resVec ))))
)
;; Test Cases:
(= [[1 1] [2 1] [1 2 1 1]] (take 3 (sequenceofPronunciations [1])))
(= [3 1 2 4] (first (sequenceofPronunciations [1 1 1 4 4])))
(= [1 1 1 3 2 1 3 2 1 1] (nth (sequenceofPronunciations [1]) 6))
(= 338 (count (nth (sequenceofPronunciations [3 2]) 15)))