4ever clojure のメモ

2021-11-25

以前、4clojure を easy まで進めていたけど、サイトが閉鎖になっていたので、https://4clojure.oxal.org/ をやってみました 。easy以上の問題のメモ。

Contents

19 Write a function which returns the last element in a sequence.(Special Restrictions : last)

(-> % reverse first)

20 Write a function which returns the second to last element from a sequence.

#(-> % reverse second)

21 Write a function which returns the Nth element from a sequence.(Special Restrictions : nth)

#(-> %1 (nthrest %2) first)

ちょっと手こずり。dropが思いつかず、「nth」を調べて似ている nthrest を使って書いたけど…。

#(first (drop %2 %1))
;という手も

22 Write a function which returns the total number of elements in a sequence.(Special Restrictions : count)

reduce (fn [a b] (inc a)) 0

23 Write a function which reverses a sequence.(Special Restrictions : reverse)

reduce #(cons %2 %1) []

「conj '()」を使っている回答のほうが多かった

consとconjの違い。
conjは、その型で一番効率のいい場所に追加する。引数と同じ型で返す。
consは、先頭に追加。clojure.lang.Consが返ってきている。

user=> (conj [1 2 3] 4)
[1 2 3 4]

user=> (type (conj [1 2 3] 4))
clojure.lang.PersistentVector

user=> (conj '(1 2 3) 4)
(4 1 2 3)
user=> (type (conj '(1 2 3) 4))
clojure.lang.PersistentList

ser=> (cons 4 [1 2 3])
(4 1 2 3)
user=> (type (cons 4 [1 2 3]))
clojure.lang.Cons

user=> (cons 4 '(1 2 3))
(4 1 2 3)
user=> (type (cons 4 '(1 2 3)))
clojure.lang.Cons
rseq
into () 
;という手も。

24 Write a function which returns the sum of a sequence of numbers.

reduce + 0

25 Write a function which returns only the odd numbers from a sequence.

filter odd?

26 Write a function which returns the first X fibonacci numbers.

ちょっと手こずり。

#(->> (iterate (fn [[a b]] [b (+ a b)]) [1 1])
     (take %)
     (map first))

最初は #(take % ((fn [[a b]] (recur [b (+ a b)])) [1 1])) しようとして、integer overflowが出た。要調査。

27 Write a function which returns true if the given sequence is a palindrome. Hint: “racecar" does not equal '(\r \a \c \e \c \a \r)

#(= (seq %) (reverse %))

28 Write a function which flattens a sequence.(Special Restrictions : flatten)

手こずった、そして、出てきた答えがスマートじゃない

(fn [xs] (if-not (some coll? xs)
            xs
            (recur (mapcat #(if (coll? %) % [%]) xs))))
(def __  (fn f [x] (if (coll? x) (mapcat f x) [x])))

(def __ #(filter (complement sequential?)
                   (rest (tree-seq sequential? seq %))))
;という手も

29 Write a function which takes a string and returns a new string containing only the capital letters.

#(apply str (re-seq #"[A-Z]" %))
(filter #(Character/isUpperCase %) s) 
(clojure.string/replace % #"[^A-Z]+" "") 
;という手も。

30 Write a function which removes consecutive duplicates from a sequence.

#(reduce (fn [xs v] (if (= v (last xs))
                      xs
                      (conj xs v))) [] %)
#(map first (partition-by identity %))
;という手も
(partition-by identity "Leeeeeerrroyyy")
=>((\L) (\e \e \e \e \e \e) (\r \r \r) (\o) (\y \y \y))

31 Write a function which packs consecutive duplicates into sub-lists.

partition-by identity

32 Write a function which duplicates each element of a sequence.

(fn [xs] (apply concat (map #(vector % %) xs)))
(interleave % %) 
mapcat #(vector % %) 
;という手も。

33 Write a function which replicates each element of a sequence a variable number of times.

(fn [xs n] (mapcat #(repeat n %) xs))
#(mapcat (partial repeat %2) %1)
;もう少し短くできる

34 Write a function which creates a list of all integers in a given range.(Special Restrictions : range)

#(take (- %2 %1) (iterate inc %1))

38 Write a function which takes a variable number of parameters and returns the maximum value.(Special Restrictions : max,max-key)

(fn [& xs] (-> xs sort last))

42 Write a function which calculates factorials.

#(apply * (range 1 (inc %)))