4clojure problem solving

14 题以前答案一般没啥区别,略过……

#15

1
#(* % 2)

adereth’s solution:

1
* 2

#16

1
format "Hello, %s!"

adereth’s solution:

1
#(str "Hello, " % "!")

#17

1
'(6 7 8)

adereth’s solution:

1
[6 7 8]

#18

1
'(6 7)

adereth’s solution:

1
'(6 7)

#19

1
#(peek (vec %))

adereth’s solution:

1
(comp first reverse)

#20

1
#(nth (reverse %) 1)

adereth’s solution:

1
(comp second reverse)

#21

1
2
3
4
(fn getn [ary n]
(if (= n 0)
(first ary)
(getn (rest ary) (dec n))))

adereth’s solution:

1
#(first (drop %2 %1))

#22

1
#(reduce (fn [a b] (inc a)) 0 %)

adereth’s solution:

1
(fn [sequence] (reduce (fn [acc v] (inc acc)) 0 sequence))

#23

1
#(reduce (fn [rs ls] (cons ls rs)) [] %)

adereth’s solution:

1
2
3
4
5
6
(fn [s]
(loop [result []
s s]
(if (seq s)
(recur (concat [(first s)] result) (rest s))
result)))

#24

1
#(reduce + %)

adereth’s solution:

1
apply +

#25

1
#(filter odd? %)

adereth’s solution:

1
filter odd?

#26

1
2
#(take % ((fn fib [a b]
(lazy-seq (cons a (fib b (+ b a))))) 1 1))

adereth’s solution:

1
2
3
4
5
#(take %
(map first
(iterate (fn [[i1 i2]]
[i2 (+ i1 i2)])
[1 1])))

#29

1
#(clojure.string/replace % #"[^A-Z]" "")

adereth’s solution:

1
(fn [s] (reduce str (filter #(Character/isUpperCase %) s)))

#27

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

#28

1
2
3
4
5
6
7
8
9
(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

1
2
3
4
5
6
7
#((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 [])
1
2
3
4
5
6
#((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:

1
#(map first (partition-by identity %))

#32

1
#(mapcat (fn [s] [s s]) %)

#34

1
2
3
4
#((fn rng [s n r]
(if (< s n)
(rng (inc s) n (conj r s))
r)) %1 %2 [])

adereth’s solution:

1
2
3
4
(fn [start end] (take-while #(< % end)
(iterate inc start)
)
)

#35

1
7

#37

1
"ABC"

#38

1
(fn [& s] (reduce (fn mx [a b] (if (> a b) a b)) s))

adereth’s solution:

1
2
(fn [ & xs ]
(reduce #(if (> %1 %2) %1 %2) xs))

#39

1
2
3
4
(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:

1
2
(fn [s1 s2]
(flatten (map list s1 s2)))

#40

1
(fn [x ys] (butlast (flatten (map #(list % x) ys))))

adereth’s solution:

1
#(butlast (interleave %2 (repeat %1)))

#41

1
2
(fn [ls n]
(keep-indexed #(if (> (mod (inc %1) n) 0) %2) ls))

adereth’s solution:

1
2
3
4
(fn [coll n]
(->> (partition-all n coll)
(map (partial take (dec n)))
(flatten)))

#42

1
2
#((fn factorial [n r]
(if (= n 1) r (factorial (dec n) (* n r)))) % 1)

adereth’s solution:

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

#48

1
6

#52

1
[c e]

#57

1
'(5 4 3 2 1)

#62

1
(fn fb ([f b] (lazy-seq (cons b (fb f (f b))))))

aceeca1’s solution:

1
(fn [f x] (reductions #(%2 %1) x (repeat f)))

注:从中学习和理解到:

  1. fn 给命名 name? 后也可以递归;
  2. lazy-seq 适合做无限循环递归的懒序列;
  3. reductions 可以将一个数组通过某个方法做 reduce 结果为一个懒序列;

#64

1
+

#66

1
2
3
4
5
6
7
8
9
10
(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:

1
2
3
4
5
(fn
[x y]
(if (= 0 y)
x
(recur y (mod x y))))

注:从中学习和理解到:

  1. recur 可以递归 fn 这样的匿名函数;
  2. (mod x y) 当 x < y 时,返回 x ,也就是说 x/y 得 0 余 x;
  3. 最大公约数 w 一定也可以除尽 (mod x y) 的余数;

#68

1
'(7 6 5 4 3)

adereth’s solution:

1
[7 6 5 4 3]

#71

1
last

#72

1
apply +

adereth’s solution:

1
#(reduce + %)

#81

1
(fn [a b] (reduce #(if (contains? a %2) (conj %1 %2) %1) #{} b))

aceeca1’s solution:

1
(comp set filter)
  • 学到的经验:#{} set 是可以当做 contains 方法来 filter 的。

#99

1
(fn [a b] (map #(Integer/parseInt(str %)) (str (* a b))))

aceeca1’s solution:

1
(comp (partial map #(- (int %) 48)) str *)
  • 学到的经验:可以组合几个方法,或者用 ->> 语法串联方法

#99

#107

1
(fn [n] #(reduce * (repeat n %)))

#134

1
#(= nil (get %2 %1 0))

adereth’s solution:

1
#(nil? (get %2 % true))

#145

1
[1 5 9 13 17 21 25 29 33 37]

#156

1
#(apply merge (map (fn [k] {k %1}) %2))
1
#(apply array-map (mapcat (fn [k] [k %1]) %2))

adereth’s solution:

1
(fn [default values] (reduce #(assoc %1 %2 default) {} values))

#161

1
#{1 2}

#162

1
1

Another solution

Solutions to 4Clojure Easy Problems

#107

1
(fn [n] #(reduce * (repeat n %)))

#134

1
#(= nil (get %2 %1 0))

adereth’s solution:

1
#(nil? (get %2 % true))

#145

1
[1 5 9 13 17 21 25 29 33 37]

#156

1
#(apply merge (map (fn [k] {k %1}) %2))
1
#(apply array-map (mapcat (fn [k] [k %1]) %2))

adereth’s solution:

1
(fn [default values] (reduce #(assoc %1 %2 default) {} values))

#161

1
#{1 2}

#162

1
1

Another solution

Solutions to 4Clojure Easy Problems

赞赏留名,相识相惜 ~