4clojure problem solving
14 题以前答案一般没啥区别,略过……
#15
#(* % 2)
adereth’s solution:
* 2
#16
format "Hello, %s!"
adereth’s solution:
#(str "Hello, " % "!")
#17
'(6 7 8)
adereth’s solution:
[6 7 8]
#18
'(6 7)
adereth’s solution:
'(6 7)
#19
#(peek (vec %))
adereth’s solution:
(comp first reverse)
#20
#(nth (reverse %) 1)
adereth’s solution:
(comp second reverse)
#21
(fn getn [ary n]
(if (= n 0)
(first ary)
(getn (rest ary) (dec n))))
adereth’s solution:
#(first (drop %2 %1))
#22
#(reduce (fn [a b] (inc a)) 0 %)
adereth’s solution:
(fn [sequence] (reduce (fn [acc v] (inc acc)) 0 sequence))
#23
#(reduce (fn [rs ls] (cons ls rs)) [] %)
adereth’s solution:
(fn [s]
(loop [result []
s s]
(if (seq s)
(recur (concat [(first s)] result) (rest s))
result)))
#24
#(reduce + %)
adereth’s solution:
apply +
#25
#(filter odd? %)
adereth’s solution:
filter odd?
#26
#(take % ((fn fib [a b]
(lazy-seq (cons a (fib b (+ b a))))) 1 1))
adereth’s solution:
#(take %
(map first
(iterate (fn [[i1 i2]]
[i2 (+ i1 i2)])
[1 1])))
#29
#(clojure.string/replace % #"[^A-Z]" "")
adereth’s solution:
(fn [s] (reduce str (filter #(Character/isUpperCase %) s)))
#27
#(= (reverse %) (seq %))
#28
(defn cci [ls r]
(if (every? sequential? ls)
(concat r ls)
(apply cci r (partition-by sequential? ls))))
#(let [f (first %)]
(if (sequential? f)
)
conj [] (first %))
#30
#((fn dis [ss l r]
(if-let [f (first ss)]
(let [rss (rest ss)]
(if (= l f)
(dis rss f r)
(dis rss f (conj r f))))
r)) % nil [])
#((fn dis [ss l r]
(let [[f & s] ss]
(cond
(nil? f) r
(= l f) (dis s f r)
:else (dis s f (conj r f))))) % nil [])
adereth’s solution:
#(map first (partition-by identity %))
#32
#(mapcat (fn [s] [s s]) %)
#34
#((fn rng [s n r]
(if (< s n)
(rng (inc s) n (conj r s))
r)) %1 %2 [])
adereth’s solution:
(fn [start end] (take-while #(< % end)
(iterate inc start)
)
)
#35
7
#37
"ABC"
#38
(fn [& s] (reduce (fn mx [a b] (if (> a b) a b)) s))
adereth’s solution:
(fn [ & xs ]
(reduce #(if (> %1 %2) %1 %2) xs))
#39
(fn l [a b & r]
(if (or (nil? (first a)) (nil? (first b)))
(flatten r)
(l (rest a) (rest b) (conj (vec r) (first a) (first b)))))
adereth’s solution:
(fn [s1 s2]
(flatten (map list s1 s2)))
#40
(fn [x ys] (butlast (flatten (map #(list % x) ys))))
adereth’s solution:
#(butlast (interleave %2 (repeat %1)))
#41
(fn [ls n]
(keep-indexed #(if (> (mod (inc %1) n) 0) %2) ls))
adereth’s solution:
(fn [coll n]
(->> (partition-all n coll)
(map (partial take (dec n)))
(flatten)))
#42
#((fn factorial [n r]
(if (= n 1) r (factorial (dec n) (* n r)))) % 1)
adereth’s solution:
#(reduce * (range 1 (inc %)))
#48
6
#52
[c e]
#57
'(5 4 3 2 1)
#62
(fn fb ([f b] (lazy-seq (cons b (fb f (f b))))))
aceeca1’s solution:
(fn [f x] (reductions #(%2 %1) x (repeat f)))
注:从中学习和理解到:
- fn 给命名
name?
后也可以递归; - lazy-seq 适合做无限循环递归的懒序列;
- reductions 可以将一个数组通过某个方法做 reduce 结果为一个懒序列;
#64
+
#66
(fn [a b]
(let [m (min a b)
d (fn [a b m]
(and (= 0 (mod a m))
(= 0 (mod b m))))
r (fn o [a b m]
(if (d a b m)
m
(o a b (dec m))))]
(r a b m)))
astangl’s solution:
(fn
[x y]
(if (= 0 y)
x
(recur y (mod x y))))
注:从中学习和理解到:
- recur 可以递归 fn 这样的匿名函数;
- (mod x y) 当
x < y
时,返回 x ,也就是说 x/y 得 0 余 x; - 最大公约数 w 一定也可以除尽 (mod x y) 的余数;
#68
'(7 6 5 4 3)
adereth’s solution:
[7 6 5 4 3]
#71
last
#72
apply +
adereth’s solution:
#(reduce + %)
#81
(fn [a b] (reduce #(if (contains? a %2) (conj %1 %2) %1) #{} b))
aceeca1’s solution:
(comp set filter)
- 学到的经验:#{} set 是可以当做 contains 方法来 filter 的。
#99
(fn [a b] (map #(Integer/parseInt(str %)) (str (* a b))))
aceeca1’s solution:
(comp (partial map #(- (int %) 48)) str *)
- 学到的经验:可以组合几个方法,或者用 -» 语法串联方法
#99
#107
(fn [n] #(reduce * (repeat n %)))
#134
#(= nil (get %2 %1 0))
adereth’s solution:
#(nil? (get %2 % true))
#145
[1 5 9 13 17 21 25 29 33 37]
#156
#(apply merge (map (fn [k] {k %1}) %2))
#(apply array-map (mapcat (fn [k] [k %1]) %2))
adereth’s solution:
(fn [default values] (reduce #(assoc %1 %2 default) {} values))
#161
#{1 2}
#162
1
Another solution
Solutions to 4Clojure Easy Problems
#107
(fn [n] #(reduce * (repeat n %)))
#134
#(= nil (get %2 %1 0))
adereth’s solution:
#(nil? (get %2 % true))
#145
[1 5 9 13 17 21 25 29 33 37]
#156
#(apply merge (map (fn [k] {k %1}) %2))
#(apply array-map (mapcat (fn [k] [k %1]) %2))
adereth’s solution:
(fn [default values] (reduce #(assoc %1 %2 default) {} values))
#161
#{1 2}
#162
1