-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercises.clj
More file actions
386 lines (304 loc) · 7.49 KB
/
exercises.clj
File metadata and controls
386 lines (304 loc) · 7.49 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
;; Answers for 4clojure.com first 100 questions.
; 1. Nothing but the Truth
true
; 2. Simple Math
4
; 3. Intro to Strings
"HELLO WORLD"
; 4. Intro to Lists
:a :b :c
; 5. Lists: conj
'(1 2 3 4)
; 6. Intro to Vectors
:a :b :c
; 7. Vectors: conj
'[1 2 3 4]
; 8. Intro to Sets
#{:a :b :c :d}
; 9. Sets: conj
1 4 3 2
; 10. Intro to Maps
20
; 11. Maps: conj
{:b 2}
; 12. Intro to Sequences
3
; 13. Sequences: rest
'(20 30 40)
; 14. Intro to Functions
8
; 15. Double Down
(partial * 2)
; 16. Hello World
(fn [name] (str "Hello, " name "!"))
; 17. Sequences: map
'(6 7 8)
; 18. Sequences: filter
'(6 7)
; 19. Local bindings
7
; 20. Let it Be
[x 7 y 3 z 1]
; 21. Regular Expressions
"ABC"
; 22. Intro to Reduce
+
; 23. Simple Recursion
'(5 4 3 2 1)
; 24. Rearranging Code
last
; 25. Recurring Theme
[7 6 5 4 3]
; 26. Rearranging Code: ->>
reduce +
; 27. A nil key
(fn [key dct] (and (contains? dct key) (not (get dct key))))
; 28. For the win
'(1 5 9 13 17 21 25 29 33 37)
; 29. Logical falsity and truth
1
; 30. Intro to Destructuring
[c e]
; 31. Subset and Superset
#{1 2}
; 32. Map Defaults
(fn [default keys] (reduce (fn [x y] (assoc x y default)) {} keys))
; 33. Last Element
(fn [x] (nth (seq x) (- (count x) 1)))
; 34. Penultimate Element
(fn [x] (nth (seq x) (- (count x) 2)))
; 35. Nth Element
(fn [x y] (last (take (+ y 1) x)))
; 36. Count a Sequence
(fn [coll] (reduce (fn [x y] (+ 1 x)) 0 coll))
; 37. Sum It All Up
(fn [x] (reduce + x))
; 38. Find the odd numbers
(fn [x] (filter odd? x))
; 39. Reverse a Sequence
(fn [coll] (reduce (fn [x y] (conj x y)) '() coll))
; 40. Palindrome Detector
(fn [coll]
(if (string? coll)
(if (= coll (reduce (fn [x y] (str y x)) "" coll)) true false)
(if (= coll (reduce (fn [x y] (conj x y)) '() coll)) true false)))
; 41. Fibonacci Sequence
(fn [x]
(loop [i 0 fib 1 fib2 1 result [1]]
(if (= i (- x 1)) (seq result)
(recur (+ i 1) fib2 (+ fib fib2) (conj result fib2)))))
; 42. Maximum value
(fn [& args]
(loop [i 0 result 0]
(if (= i (dec (count args)))
result
(recur (inc i)
(if (< result (nth args i))
(nth args i)
result)))))
; 43. Get the Caps
(fn [string]
(reduce
(fn [x y] (if (re-find #"[A-Z]" (str y)) (str x y) x))
""
string))
; 44. Duplicate a Sequence
(fn [coll]
(seq (reduce (fn [x y] (conj x y y)) [] coll)))
; 45. Intro to some
6
; 46. Compress a Sequence
(fn [x]
(loop [i 0 result []]
(if (= i (count x))
(seq result)
(recur (inc i)
(if (= i 0)
(conj result (get x i))
(if (= (get x i) (get result (dec (count result))))
result
(conj result (get x i))))))))
; 47. Implement range
(fn [start end]
(loop [i start result []]
(if (= i end)
(seq result)
(recur (inc i) (conj result i)))))
; 48. Factorial Fun
(fn [n]
(loop [i n result 1]
(if (= i 1)
result
(recur (dec i) (* result i)))))
; 49. Interleave Two Seqs
(fn [x y]
(loop [i 0 result []]
(if (or (= i (count x)) (= i (count y)))
(seq result)
(recur (inc i) (conj result (get x i) (get y i))))))
; 50. Flatten
; 51. Replicate a Sequence
(fn [coll n] (loop [i 0 result []]
(if (= i (count coll))
(seq result)
(recur (inc i)
(loop [j 0 result2 result]
(if (= j n)
result2
(recur (inc j) (conj result2 (get coll i)))))))))
; 52. Intro to Iterate
'(1 4 7 10 13)
; 53. Contain Yourself
4
; 54. Interpose a Seq
(fn [separator coll]
(loop [i 0 result []]
(if (= i (dec (count coll)))
(seq (conj result (get coll i)))
(recur (inc i) (conj result (get coll i) separator)))))
; 55. Pack a Sequence
; 56. Drop Every Nth Item
(fn [coll n]
(loop [i 0 result []]
(if (= i (count coll))
(seq result)
(recur (inc i)
(if (= (rem (inc i) n) 0)
result
(conj result (get coll i)))))))
; 57. Split a sequence
(fn [n coll]
(loop [i 0 part1 [] part2 []]
(if (= i (count coll))
(seq (conj [] part1 part2))
(recur (inc i)
(if (>= i n) part1 (conj part1 (get coll i)))
(if (>= i n) (conj part2 (get coll i)) part2)))))
; 58. Advanced Destructuring
[1 2 3 4 5]
; 59. A Half-Truth
(fn [& args]
(if (every? true? args)
false
(if (some true? args)
true
false)))
; 60. Map Construction
(fn [coll1 coll2]
(loop [i 0 result {}]
(if (or (= i (count coll1)) (= i (count coll2)))
result
(recur (inc i) (assoc result (get coll1 i) (get coll2 i))))))
; 61. Greatest Common Divisor
(fn [a b]
(loop [a a b b]
(cond
(= a 0) b
(= b 0) a
:else (recur b (rem a b)))))
; 62. Set Intersection
(fn [set1 set2]
(set (reduce
(fn [x y] (if (contains? set2 y) (conj x y) x))
[]
set1)))
; 63. Simple closures
(fn [n]
(fn [x]
(if (= n 0)
1
(loop [i n result x]
(if (= i 1) result (recur (dec i) (* result x)))))))
; 64. Re-implement Iterate
; 65. Comparisons
(fn [operator a b]
(cond
(operator a b) :lt
(operator b a) :gt
:else :eq))
; 66. Cartesian Product
(fn [set1 set2]
(set
(apply concat
(reduce
(fn [x y]
(conj x (reduce (fn [l n] (conj l [n y])) [] set1)))
#{}
set2))))
; 67. Product Digits
(fn [a b]
(reduce
(fn [x y] (conj x y))
[]
(map (fn [n] (read-string (str n))) (str (* a b)))))
; 68. Group a Sequence
(fn [f s]
(reduce
(fn [x y]
(let [result (f y)]
(if (get x result)
(assoc-in x [result] (conj (get x result) y))
(assoc x result [y]))))
{}
s))
; 69. Symmetric Difference
(fn [set1 set2]
(if (= (count set1) 0)
set2
(reduce
(fn [x y]
(if (contains? x y)
(disj x y)
(conj x y)))
set2
set1)))
; 70. dot product
(fn [coll1 coll2]
(reduce + (loop [i 0 result []]
(if (= i (count coll1))
result
(recur (inc i) (conj result (* (get coll1 i) (get coll2 i))))))))
; 71. Read a binary number
(fn [number]
(if (= 0 (read-string number))
0
(int (reduce + (loop [i 0 result [] number (reverse number)]
(if (= i (count number))
result
(if (= (str (nth number i)) "0")
(recur (inc i) result number)
(recur (inc i) (conj result (Math/pow 2 i)) number))))))))
; 72. Through the Looking Class
Class
; 73. Infix Calculator
; 74. Indexing Sequences
(fn [coll]
(loop [i 0 result []]
(if (= i (count coll))
(seq result)
(recur (inc i) (conj result [(get coll i) i])))))
; 75. Pascal's Triangle
(fn [row]
(get
(loop [i 1 result []]
(if (= i (inc row))
result
(recur (inc i)
(conj result (loop [column 0 result2 []]
(if (= column i)
result2
(recur (inc column)
(if (or (= column 0) (= column (- i 1)))
(conj result2 1)
(conj result2
(+ (get (get result (- i 2)) column)
(get (get result (- i 2)) (dec column))))))))))))
(dec row)))
; 76. Re-implement Map
(fn new-map [f coll]
(if (empty? coll)
coll
(lazy-seq (cons (f (first coll)) (new-map f (rest coll))))))
; Reverse Interleave
(fn [n coll]
(apply map vector (partition coll n)))