(partial * 2) 16.Hello World (#(str "Hello, " % "!")
17.Sequences map '(6 7 8)
18.Sequences filter '(6 7)
19.Last Element #(first(reverse %))
20.Penultimate Element #(first(rest(reverse %)))
21.Nth Element #(first (drop %2 %1))
22.Count a Sequence
one for example:
#(loop [result 0 c %]
(if(empty? c) result
(recur (inc result) (rest c))))
others' excellent anser
#(reduce (fn [x y] (inc x)) 0 %)
23.Reverse a Sequence #(into () %)
24.Sum It All Up #(apply + %)
25.Find the odd numbers #(filter odd? %)
26.Fibonacci Sequence #(take % (map first(iterate (fn [[x y]] [y (+ x y)]) [ 1 1]))) or #(take % (map last(iterate (fn [[x y]] [y (+ x y)]) [ 0 1])))
http://www.iteye.com/topic/624085
27.Palindrome Detector
#(= (seq %) (reverse %))
;;(seq "abc") result in (\a \b \c)
;; reverse will not evaluated as string ether.
28.Flatten a Sequence
#(filter (complement sequential?) (tree-seq sequential? identity %))
;; complement
;; tree-seq
29.Get the Caps #(apply str (re-seq #"[A-Z]" %))
re-seq 寻找序列中包含的#“。。。” %是序列 任意序列 (re-seq #"[A-Z]" %)得到的是单个的序列 str转换为str类型 apply将单词组合起来
30.Compress a Sequence
one for example
(fn [input]
(loop [i input res []]
(if (empty? i)
res
(if (= (last res) (first i))
(recur (rest i) res)
(recur (rest i) (conj res (first i))))
)))
another answer for example
;; use partition-by
#(map first (partition-by identity %)) 其中partition-by是将序列相同的为一组,关键字identity,
( partition-by identity "Leeeeeerrroyyy" ) |
|
( ( \L ) ( \e \e \e \e \e \e ) ( \r \r \r ) ( \o ) ( \y \y \y ) ) |
用map取first或者last
31.Pack a Sequence
#(partition-by identity %)
类似于上一题
e.g.
(partition-by identity [1 1 1 2 2 3 3 1 1 2 3])
((1 1 1) (2 2) (3 3) (1 1) (2) (3))
32.Duplicate a Sequence #(interleave % %)
32.Pack a Sequence
【1】#(apply concat (map (fn [input](repeat %2 input)) %1))
【2】#(mapcat (fn [input](repeat %2 input)) %1)
34. Implement range #(take (- %2 %1) (iterate inc %)) (- %2 %1)是个数 (iterate inc %)是从%这个数值开始递增
http://clojuredocs.org/clojure_core/clojure.core/iterate
35.Local bindings 7
36.Let it Be [x 7,y 3 ,z 1]
37.Regular Expressions "ABC"
38.Maximum value
39.Interleave Two Seqs mapcat vector
40.Interpose a Seq #(-> (interleave %2 (repeat %1)) drop-last vec)
41Drop Every Nth Item
(fn [vec n]
(mapcat #(take (dec n) %) (partition-all n vec))
)
42.Factorial Fun #(reduce * (range 1 (inc %))) 阶乘
43. Reverse Interleave
(fn rev-int [v n]
(apply map vector (partition n v)))
44. Rotate Sequence
(fn rotate [n v]
(->> (concat v v)
(drop (mod n (count v)))
(take (count v))
)
)
n为个数,v为verctor, concat连接两个集合,drop去掉前(mod n (count v))个元素,take取前(take (count v)个元素
eg.(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))
(concat v v)[1 2 3 4 5 1 2 3 4 5 ],(count v )为5,(mod -2 5)为3,(drop 3 )为[4 5 1 2 3 4 5 ],(take 5)为[4 5 1 2 3 ]
45.Intro to Iterate '(1 4 7 10 13)
46. Flipping out
#(fn [a b] (% b a))
47.Contain Yourself
4
48.Intro to some
6
49.Split a sequence
#(vector
(vec (take %1 %2))
(vec (drop %1 %2)))
50. Split by Type
(fn split-by-type [v]
(for [[key value] (group-by class v)]
value))
51.Advanced Destructuring
[1 2 3 4 5]
54. Partition a Sequence
(fn my-partition [n v]
(if (>= (count v) n)
(cons (take n v) (my-partition n (drop n v)))))
55. Count Occurrences
#(into {}
(map (fn [[k v]] [k (count v)]) (group-by identity %)))
56. Find Distinct Items
reduce (fn [s e]
(if (some #(= % e) s)
s
(conj s e)))
[]
58. Function Composition
(fn comb [& funcs]
(fn [& args]
(first
(reduce #(vector (apply %2 %1)) args (reverse funcs )))))
59. Juxtaposition
(fn my-juxt [& funcs]
(fn [& args]
(map #(apply % args) funcs )))
60. Sequence Reductions
(fn my-reduce
([op input] (my-reduce op (first input) (rest input)))
([op result input]
(lazy-seq
(if (empty? input) (list result)
(cons result
(my-reduce op
(op result (first input))
(rest input)))))))
61.Map Construction
#(into {} (map vector %1 %2))
62.Re-implement Iterate
(fn it [f x]
(lazy-seq (cons x (it f (f x)))))
63.Group a Sequence
(fn grp-seq [func vals]
(into {}
(map #(vector (func (first % )) (vec %))
(partition-by func (sort vals)))))
64.Intro to Reduce
+
65. Black Box Testing
#({{} :map #{} :set} (empty %) (if (reversible? %) :vector :list))
66.Greatest Common Divisor
(fn [a b]
(let [get-divisor (fn [n] (into #{}
(filter #(zero? (rem n %))
(range 1 (inc n)))
))
a-divisor (get-divisor a)
b-divisor (get-divisor b)
commom-divisor (clojure.set/intersection a-divisor b-divisor)]
(apply max commom-divisor)))
67. Prime Numbers
(fn [n]
(take n(filter
(fn is-prime [n]
(nil?
(some
#(zero? (mod n %))
(range 2 n))))
(range 2 1000))))
68.Recurring Theme
'(7 6 5 4 3)
69. Merge with a Function
(fn [f & args]
(reduce (fn[map1 map2]
(reduce (fn [m [k v]]
(if-let [vv (m k)]
(assoc m k (f vv v))
(assoc m k v)))
map1 map2))
args))
70. Word Sorting
#(sort-by (fn [v](.toLowerCase v)) (re-seq #"\w+" %))
71.Rearranging Code
last
72.Rearranging Code-->>
reduce +
74. Filter Perfect Squares
(fn perf-square [s]
(let [nums (map #(Integer/valueOf %) (clojure.string/split s #","))
all-perf-sq (set (map #(* % %) (range 100)))
sq-nums (filter all-perf-sq nums)]
(apply str (interpose "," sq-nums))))
76. Intro to Trampoline
[1 3 5 7 9 11]
77.Anagram Finder
(fn [v]
(into #{}
(map set
(filter #(> (count %) 1)
(map val (group-by sort v))))))
78. Reimplement Trampoline
(fn
[f & args]
(loop [res (apply f args)]
(if (fn? res)
(recur (res))
res)))
80. Perfect Numbers
(fn is-perf-num [n]
(= n
(apply + (filter #(zero? (mod n %)) (range 1 n)))))
81.Set Intersection
(fn doset [sa sb]
(set (filter #(sa %) sb))
)
83.A Half-Truth
#(true?
(and
(some true? %&)
(some false? %&)))
86. Happy Numbers
(letfn [(num->digits [num]
(loop [n num res []]
(if (zero? n)
res
(recur (long (/ n 10)) (cons (mod n 10) res)))))
(change [n]
(apply + (map #(* % %) (num->digits n))))]
(fn [init]
(loop [curr init results #{}]
(println curr " - " results)
(cond
(= 1 curr) true
(results curr) false
:else (let [new-n (change curr)]
(println curr new-n)
(recur new-n (into results [curr])))
)))
)
88.Symmetric Difference
(fn set-diff [sa sb]
(set
(concat
(filter (complement sb) sa)
(filter (complement sa) sb))))
90.Cartesian Product
(fn [a b]
(set (for [x a y b]
[x y])))
95.To Tree, or not to Tree
(fn istree? [root]
(or (nil? root)
(and (sequential? root)
(= 3 (count root))
(every? istree? (rest root)))))
96. Beauty is Symmetry
(fn symmetry [[root left right]]
(let [mirror? (fn mirror? [a b]
(cond
(not= (sequential? a) (sequential? b)) false
(sequential? a) (let [[ra La Ra] a
[rb Lb Rb] b]
(and (= ra rb) (mirror? La Rb) (mirror? Lb Ra)))
:else (= a b)))]
(mirror? left right)))
97.Pascal's Triangle
(fn pascal-tri [n]
(condp = n
1 [1]
(let [last (pascal-tri (dec n))
last-a (concat [0] last)
last-b (concat last [0])
]
(vec(map + last-a last-b)))))
99.Product Digits
;; the math way
(fn [a b]
(drop-while zero?
(reverse
(map #(mod (int(/ (* a b) %)) 10)
(take 10 (iterate #(* 10 %) 1))))))
;; the trick way
(fn [a b]
(map #(Integer/parseInt (str %)) (str (* a b))))
100. Least Common Multiple.clj
(fn [& args]
(letfn [(gcd [x y]
(let [a (max x y)
b (min x y)
m (mod a b)]
(if (zero? m)
b
(recur b m))))
(lcm [a b]
(/ (* a b) (gcd a b)))]
(reduce lcm args)
))