-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforclojure.clj
More file actions
4380 lines (3504 loc) · 126 KB
/
forclojure.clj
File metadata and controls
4380 lines (3504 loc) · 126 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns forclojure.core)
;; 1 Nothing but the Truth
;; This is a clojure form. Enter a value which will make the form
;; evaluate to true. Don't over think it! If you are confused, see the
;; getting started page. Hint: true is equal to true.
(let [__ true]
(= __ true))
;; 2 Simple Math
;; If you are not familiar with polish notation, simple arithmetic
;; might seem confusing.
(let [__ 4]
(= (- 10 (* 2 3)) __))
;; 3. Intro to Strings
;; Clojure strings are Java strings. This means that you can use any
;; of the Java string methods on Clojure strings.
(let [__ "HELLO WORLD"]
(= __ (.toUpperCase "hello world")))
;; 4. Intro to Lists
;; Lists can be constructed with either a function or a quoted form.
(= (list :a :b :c) '(:a :b :c))
;; 5. Lists: conj
;; When operating on a list, the conj function will return a new list
;; with one or more items "added" to the front.
(let [__ '(1 2 3 4)]
(and
(= __ (conj '(2 3 4) 1))
(= __ (conj '(3 4) 2 1))))
;; 6. Intro to Vectors
;; Vectors can be constructed several ways. You can compare them with
;; lists.
(=
[:a :b :c]
(list :a :b :c)
(vec '(:a :b :c))
(vector :a :b :c))
;; 7. Vectors: conj
(let [__ [1 2 3 4]]
(and
(= __ (conj [1 2 3] 4))
(= __ (conj [1 2] 3 4))))
;; 8. Intro to Sets
(let [__ #{:a :b :c :d}]
(and
(= __ (set '(:a :a :b :c :c :c :c :d :d)))
(= __ (clojure.set/union #{:a :b :c} #{:b :c :d}))))
;; 9. Sets: conj
;; When operating on a set, the conj function returns a new set with
;; one or more keys "added".
(let [__ 2]
(= #{1 2 3 4} (conj #{1 4 3} __)))
;; 10. Intro to Maps Maps store key-value pairs.
;; Both maps and keywords can be used as lookup functions. Commas can
;; be used to make maps more readable, but they are not required.
(let [__ 20]
(and
(= __ ((hash-map :a 10, :b 20, :c 30) :b))
(= __ (:b {:a 10, :b 20, :c 30}))))
;; 11. Maps: conj
;; When operating on a map, the conj function returns a new map with
;; one or more key-value pairs "added".
(let [__ [:b 2]]
(= {:a 1, :b 2, :c 3} (conj {:a 1} __ [:c 3])))
;; 12. Intro to Sequences
;; All Clojure collections support sequencing. You can operate on
;; sequences with functions like first, second, and last.
(let [__ 3]
(and
(= __ (first '(3 2 1)))
(= __ (second [2 3 4]))
(= __ (last (list 1 2 3)))))
;; 13. Sequences: rest
;; The rest function will return all the items of a sequence except
;; the first.
(let [__ [20 30 40]]
(= __ (rest [10 20 30 40])))
;; 14. Intro to Functions
;; Clojure has many different ways to create functions.
(let [__ 8]
(and
(= __ ((fn add-five [__] (+ __ 5)) 3))
(= __ ((fn [__] (+ __ 5)) 3))
(= __ (#(+ % 5) 3))
(= __ ((partial + 5) 3))))
;; 15. Double Down
;; Write a function which doubles a number.
(let [__ (fn [x] (+ x x))]
(and
(= (__ 2) 4)
(= (__ 3) 6)
(= (__ 11) 22)
(= (__ 7) 14)))
;; 16. Hello World
;; Write a function which returns a personalized greeting.
(let [__ (fn [x] (str "Hello, " x "!"))]
(= (__ "Dave") "Hello, Dave!"))
;; 17. Sequences: map
;; The map function takes two arguments: a function (f) and a sequence
;; (s). Map returns a new sequence consisting of the result of
;; applying f to each item of s. Do not confuse the map function with
;; the map data structure.
(let [__ '(6 7 8)]
(= __ (map #(+ % 5) '(1 2 3))))
;; 18. Sequences: filter
;; The filter function takes two arguments: a predicate function (f)
;; and a sequence (s). Filter returns a new sequence consisting of all
;; the items of s for which (f item) returns true.
(let [__ '(6 7)]
(= __ (filter #(> % 5) '(3 4 5 6 7))))
;; 19. Last Element
;; Write a function which returns the last element in a
;; sequence. (Special Restrictions: last)
(let [__ (fn [x] (first (reverse x)))]
(and
(= (__ [1 2 3 4 5]) 5)
(= (__ '(5 4 3)) 3)
(= (__ ["b" "c" "d"]) "d")))
;; 20. Penultimate Element
;; Write a function which returns the second to last element from a
;; sequence.
(let [__ (fn [x] (second (reverse x)))]
(and
(= (__ (list 1 2 3 4 5)) 4)
(= (__ ["a" "b" "c"]) "b")
(= (__ [[1 2] [3 4]]) [1 2])))
;; 21. Nth element
;; Write a function which returns the Nth element from a
;; sequence. (Special restrictions: nth)
(let [__ (fn [sq ndx]
(if (= 0 ndx)
(first sq)
(recur (rest sq) (dec ndx))))]
(= (__ '(4 5 6 7) 2) 6))
;; 22. Count a sequence
;; Write a function which returns the total number of elements in a
;; sequence. Special restrictions: count
(let [__ (fn [sq]
(let [aux (fn [sq acc]
(if (empty? sq)
acc
(recur (rest sq) (inc acc))))]
(aux sq 0)))]
(= (__ '(1 2 3 3 1)) 5))
;; 23. Reverse a sequence
;; Write a function which reverses a sequence.
(let [__ (fn [sq]
(let [aux (fn [sq acc]
(if (empty? sq)
acc
(recur
(rest sq)
(cons (first sq) acc))))]
(aux sq nil)))]
(= (__ [1 2 3 4 5]) [5 4 3 2 1]))
;; 24. Sum It All Up
;; Write a function which returns the sum of a sequence of numbers.
(let [__ #(reduce + %)]
(and
(= (__ [1 2 3]) 6)
(= (__ (list 0 -2 5 5)) 8)
(= (__ #{4 2 1}) 7)
(= (__ '(0 0 -1)) -1)
(= (__ '(1 10 3)) 14)))
;; 25. Find the odd numbers
;; Write a function which returns only the odd numbers from a sequence.
(let [__ (partial filter odd?)]
(and
(= (__ #{1 2 3 4 5}) '(1 3 5))
(= (__ [4 2 1 6]) '(1))
(= (__ [2 2 4 6]) '())
(= (__ [1 1 1 3]) '(1 1 1 3))))
;; 26. Fibonacci Sequence
;; Write a function which returns the first X fibonacci numbers.
(let [__ (fn [n]
(seq
(reduce (fn [a b]
(conj a (+' (last a) (last (butlast a)))))
[1 1]
(range (dec (dec n))))))]
(and
(= (__ 3) '(1 1 2))
(= (__ 6) '(1 1 2 3 5 8))
(= (__ 8) '(1 1 2 3 5 8 13 21))))
;; 27. Palindrome Detector
;; Write a function which returns true if the given sequence is a
;; palindrome.
(let [__ (fn [s]
(if (< (count s) 2)
true
(and
(= (first s) (last s))
(recur (drop 1 (drop-last 1 s))))))]
(and
(false? (__ '(1 2 3 4 5)))
(true? (__ "racecar"))
(true? (__ [:foo :bar :foo]))
(true? (__ '(1 1 3 3 1 1)))
(false? (__ '(:a :b :c)))))
;; 28. Flatten a Sequence
;; Write a function which flattens a sequence. Special restrictions:
;; flatten
(let [__ (fn[x]
(filter (complement sequential?)
(rest (tree-seq sequential? seq x))))]
(and
(= (__ '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6))
(= (__ ["a" ["b"] "c"]) '("a" "b" "c"))
(= (__ '((((:a))))) '(:a))))
;; 29. Get the Caps
;; Write a function which takes a string and returns a new string
;; containing only the capital letters.
(let [__ (fn [val] (apply str (filter #(Character/isUpperCase %) val)))]
(and (= (__ "HeLlO, WoRlD!") "HLOWRD")
(empty? (__ "nothing"))
(= (__ "$#A(*&987Zf") "AZ")))
;; 30. Compress a Sequence
;; Write a function which removes consecutive duplicates from a
;; sequence.
(let [__ (fn [sq]
(let [aux
(fn [[head & tail] last-seen acc]
(cond
(nil? head)
acc
(= head last-seen)
(recur tail head acc)
:else
(recur tail head (conj acc head))))]
(aux sq nil [])))]
(and
(= (apply str (__ "Leeeeeerrroyyy")) "Leroy")
(= (__ [1 1 2 3 3 2 2 3]) '(1 2 3 2 3))
(= (__ [[1 2] [1 2] [3 4] [1 2]]) '([1 2] [3 4] [1 2]))))
;; 31. Pack a Sequence
;; Write a function which packs consecutive duplicates into sub-lists.
(let [__ (fn [sq]
(let [aux
(fn [[head & tail] last-seen curr acc]
(cond
(nil? head)
(conj acc curr)
(= head last-seen)
(recur tail head (conj curr head) acc)
:else
(recur tail head (list head) (conj acc curr))))]
(filter (complement empty?)
(aux sq nil (list) []))))]
(and
(= (__ [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3)))
(= (__ [:a :a :b :b :c]) '((:a :a) (:b :b) (:c)))
(= (__ [[1 2] [1 2] [3 4]]) '(([1 2] [1 2]) ([3 4])))))
;; 32. Duplicate a Sequence
;; Write a function which duplicates each element of a sequence.
(let [__ (fn [sq]
(let [aux
(fn [[head & tail] acc]
(cond
(nil? head)
acc
:else
(recur tail (conj acc head head))))]
(aux sq [])))]
(and
(= (__ [1 2 3]) '(1 1 2 2 3 3))
(= (__ [:a :a :b :b]) '(:a :a :a :a :b :b :b :b))
(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))
(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4]))))
;; 33. Replicate a Sequence
;; Write a function which replicates each element of a sequence a
;; variable number of times.
(let [__ (fn [sq n]
(let [aux
(fn [[head & tail] n acc]
(cond
(nil? head)
acc
:else
(recur tail n (apply conj acc (repeat n head)))))]
(aux sq n [])))]
(and
(= (__ [1 2 3] 2) '(1 1 2 2 3 3))
(= (__ [:a :b] 4) '(:a :a :a :a :b :b :b :b))
(= (__ [4 5 6] 1) '(4 5 6))
(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4]))
(= (__ [44 33] 2) [44 44 33 33])))
;; 34. Implement range
;; Write a function which creates a list of all integers in a given
;; range.
(let [__ (fn [from to]
(let [aux
(fn [from to acc]
(cond
(= from to)
acc
:else
(recur (inc from) to (conj acc from))))]
(aux from to [])))]
(and
(= (__ 1 4) '(1 2 3))
(= (__ -2 2) '(-2 -1 0 1))
(= (__ 5 8) '(5 6 7))))
;; 35. Local binding
;; Clojure lets you give local names to values using the special
;; let-form.
(let [__ 7]
(and
(= __ (let [x 5] (+ 2 x)))
(= __ (let [x 3, y 10] (- y x)))
(= __ (let [x 21] (let [y 3] (/ x y))))))
;; 36. Let it be
;; Can you bind x, y, and z so that these are all true?
(let [z 1 y 3 x 7]
(and
(= 10 (+ x y))
(= 4 (+ y z))
(= 1 z)))
;; 37. Regular Expressions
;; Regex patterns are supported with a special reader macro.
(let [__ "ABC"]
(= __ (apply str (re-seq #"[A-Z]+" "bA1B3Ce "))))
;; 38. Maximum value
;; Write a function which takes a variable number of parameters and
;; returns the maximum value.
(let [__ (fn [& s]
(let [aux (fn [acc s]
(if (empty? s)
acc
(let [f (first s)
n (if (<= acc f) f acc)]
(recur n (rest s)))))]
(aux 0 s)))]
(and
(= (__ 1 8 3 4) 8)
(= (__ 30 20) 30)
(= (__ 45 67 11) 67)))
;; 39. Interleave two Seqs
;; Write a function which takes two sequences and returns the first
;; item from each, then the second item from each, then the third,
;; etc.
(let [__ (fn [sq1 sq2]
(let [aux
(fn [[hd1 & tl1] [hd2 & tl2] acc]
(cond
(or
(nil? hd1)
(nil? hd2))
acc
:else
(recur tl1 tl2 (conj acc hd1 hd2))))]
(aux sq1 sq2 [])))]
(and
(= (__ [1 2 3] [:a :b :c]) '(1 :a 2 :b 3 :c))
(= (__ [1 2] [3 4 5 6]) '(1 3 2 4))
(= (__ [1 2 3 4] [5]) [1 5])
(= (__ [30 20] [25 15]) [30 25 20 15])))
;; 40. Interpose a Seq
;; Write a function which separates the items of a sequence by an
;; arbitrary value.
(let [__ (fn [sep sq]
(let [aux
(fn [[head & tail] acc]
(cond
(empty? tail)
(conj acc head)
:else
(recur tail (conj acc head sep))))]
(aux sq [])))]
(and
(= (__ 0 [1 2 3]) [1 0 2 0 3])
(= (apply str (__ ", " ["one" "two" "three"])) "one, two, three")
(= (__ :z [:a :b :c :d]) [:a :z :b :z :c :z :d])))
;; 41. Drop every Nth item
;; Write a function which drops every Nth item from a sequence.
(let [__ (fn [sq n]
(let [n- (dec n)
aux
(fn [[head & tail] idx acc]
(cond
(nil? head)
acc
(zero? idx)
(recur tail n- acc)
:else
(recur tail (dec idx) (conj acc head))))]
(aux sq n- [])))]
(and
(= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
(= (__ [:a :b :c :d :e :f] 2) [:a :c :e])
(= (__ [1 2 3 4 5 6] 4) [1 2 3 5 6])))
;; 42. Factorial fun
;; Write a function which calculates factorials.
(let [__ (fn [x]
(let [aux
(fn[n acc]
(cond
(= 1 n)
acc
:else
(recur (dec n) (* n acc))))]
(aux x 1)))]
(and
(= (__ 1) 1)
(= (__ 3) 6)
(= (__ 5) 120)
(= (__ 8) 40320)))
;; 43. Reverse Interleave
;; Write a function which reverses the interleave process into x
;; number of subsequences.
(let [__ (fn [sq n]
(letfn [(vassoc-in [m [k & ks] v]
(if ks
(assoc m k (vassoc-in (get m k []) ks v))
(assoc m k v)))
(aux [[head & tail] idx acc]
(cond
(nil? head)
acc
:else
(recur tail (mod (inc idx) n)
(vassoc-in acc [idx (count (get acc idx))] head))))]
(aux sq 0 (vec (repeat n [])))))]
(and
(= (__ [1 2 3 4 5 6] 2) '((1 3 5) (2 4 6)))
(= (__ (range 9) 3) '((0 3 6) (1 4 7) (2 5 8)))
(= (__ (range 10) 5) '((0 5) (1 6) (2 7) (3 8) (4 9)))))
;; 44. Rotate Sequence
;; Write a function which can rotate a sequence in either direction.
(let [__ (fn [n sq]
(let [v (vec sq)
len (count v)
n_ (if (<= 0 n) n (+ len n))
mid (mod n_ len)
a (subvec v mid len)
b (subvec v 0 mid)]
(concat a b)))]
(and
(= (__ 2 [1 2 3 4 5]) '(3 4 5 1 2))
(= (__ -2 [1 2 3 4 5]) '(4 5 1 2 3))
(= (__ 6 [1 2 3 4 5]) '(2 3 4 5 1))
(= (__ 1 '(:a :b :c)) '(:b :c :a))
(= (__ -4 '(:a :b :c)) '(:c :a :b))))
;; 45. Intro to Iterate
;; The iterate function can be used to produce an infinite lazy
;; sequence.
(= '(1 4 7 10 13)
(take 5 (iterate #(+ 3 %) 1)))
;; 46. Flipping out
;; Write a higher-order function which flips the order of the
;; arguments of an input function.
(let [__ (fn[f]
(fn [b a]
(f a b)))]
(and
(= 3 ((__ nth) 2 [1 2 3 4 5]))
(= true ((__ >) 7 8))
(= 4 ((__ quot) 2 8))
(= [1 2 3] ((__ take) [1 2 3 4 5] 3))))
;; 47. Contain Yourself
;; The contains? function checks if a KEY is present in a given
;; collection. This often leads beginner clojurians to use it
;; incorrectly with numerically indexed collections like vectors and
;; lists.
(let [__ 4]
(and
(contains? #{4 5 6} __)
(contains? [1 1 1 1 1] __)
(contains? {4 :a 2 :b} __)
(not (contains? [1 2 4] __))))
;; 48. Intro to some
;; The some function takes a predicate function and a collection. It
;; returns the first logical true value of (predicate x) where x is an
;; item in the collection.
(let [__ 6]
(and
(= __ (some #{2 7 6} [5 6 7 8]))
(= __ (some #(when (even? %) %) [5 6 7 8]))))
;; 49. Split a sequence
;; Write a function which will split a sequence into two parts.
(let [ __ (fn[n sq]
[(take n sq) (drop n sq)])]
(and
(= (__ 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]])
(= (__ 1 [:a :b :c :d]) [[:a] [:b :c :d]])
(= (__ 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]])))
;; 50. Split by type
;; Write a function which takes a sequence consisting of items with
;; different types and splits them up into a set of homogeneous
;; sub-sequences. The internal order of each sub-sequence should be
;; maintained, but the sub-sequences themselves can be returned in any
;; order (this is why 'set' is used in the test cases).
(let [__
(fn[coll]
(loop [[head & tail] coll res {}]
(if (nil? head)
(into #{} (vals res))
(let [t (type head)]
(recur tail
(assoc res t (conj (get res t []) head)))))))]
(and
(= (set (__ [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]})
(= (set (__ [:a "foo" "bar" :b])) #{[:a :b] ["foo" "bar"]})
(= (set (__ [[1 2] :a [3 4] 5 6 :b])) #{[[1 2] [3 4]] [:a :b] [5 6]})))
;; 51. Advanced Destructuring
;; Here is an example of some more sophisticated destructuring.
(= [1 2 [3 4 5] [1 2 3 4 5]]
(let [[a b & c :as d] [1 2 3 4 5]] [a b c d]))
;; 52. Intro to Destructuring
;; Let bindings and function parameter lists support destructuring.
(= [2 4] (let [[a b c d e] [0 1 2 3 4]] [c e]))
;; 53. Longest Increasing Sub-Seq
;; Given a vector of integers, find the longest consecutive
;; sub-sequence of increasing numbers. If two sub-sequences have the
;; same length, use the one that occurs first. An increasing
;; sub-sequence must have a length of 2 or greater to qualify.
(let [__
(fn [w]
(let [aux
(fn [[head & tail] last-seen curr res]
(cond
(nil? head)
(let [tmp (if (<=
(count curr)
(count res))
res
curr)]
(if (< 1 (count tmp))
tmp
[]))
(< last-seen head)
(recur tail head (conj curr head) res)
:else
(recur tail head [head] (if (< (count res)
(count curr))
curr
res))))]
(aux w -1 [] [])))]
(and
(= (__ [1 0 1 2 3 0 4 5]) [0 1 2 3])
(= (__ [5 6 1 3 2 7]) [5 6])
(= (__ [2 3 3 4 5]) [3 4 5])
(= (__ [7 6 5 4]) [])))
;; 54. Partition a Sequence
;; Write a function which returns a sequence of lists of x items
;; each. Lists of less than x items should not be returned. Special
;; restrictions: partition, partition-all
(let [__
(fn [n w]
(let [aux
(fn [[head & tail] curr res]
(cond
(nil? head)
(let [out (if (= n (count curr))
(conj res curr)
res)]
(seq out))
(< (count curr) n)
(recur tail (conj curr head) res)
:else
(recur tail [head] (conj res curr))))]
(aux w [] [])))]
(and
(= (__ 3 (range 9)) '((0 1 2) (3 4 5) (6 7 8)))
(= (__ 2 (range 8)) '((0 1) (2 3) (4 5) (6 7)))
(= (__ 3 (range 8)) '((0 1 2) (3 4 5)))))
;; 55. Count Occurrences
;; Write a function which returns a map containing the number of
;; occurences of each distinct item in a sequence. Special
;; restrictions: frequencies
(let [__
(fn [coll]
(reduce (fn [freqs x]
(assoc freqs x
(inc (get freqs x 0))))
{} coll))]
(and
(= (__ [1 1 2 3 2 1 1]) {1 4, 2 2, 3 1})
(= (__ [:b :a :b :a :b]) {:a 2, :b 3})
(= (__ '([1 2] [1 3] [1 3])) {[1 2] 1, [1 3] 2})))
;; 56. Write a function which removes the duplicates from a
;; sequence. Order of the items must be maintained. Special
;; restrictions: distinct.
(let [__
(fn [coll]
(let [aux
(fn step [xs seen]
(lazy-seq
((fn [[x :as xs] seen]
(when-let [s (seq xs)]
(if (contains? seen x)
(recur (rest s) seen)
(cons x (step (rest s) (conj seen x))))))
xs seen)))]
(aux coll #{})))]
(and
(= (__ [1 2 1 3 1 2 4]) [1 2 3 4])
(= (__ [:a :a :b :b :c :c]) [:a :b :c])
(= (__ '([2 4] [1 2] [1 3] [1 3])) '([2 4] [1 2] [1 3]))
(= (__ (range 50)) (range 50))))
;; 57. Simple recursion
;; A recursive function is a function which calls itself. This is one
;; of the fundamental techniques used in functional programming.
(let [__ '(5 4 3 2 1)]
(= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5)))
;; 58. Function Composition
;; Write a function which allows you to create function
;; compositions. The parameter list should take a variable number of
;; functions, and create a function that applies them from
;; right-to-left. Special restrictions: comp
(let [__
(fn rec
([] identity)
([f] f)
([f g]
(fn
([] (f (g)))
([x] (f (g x)))
([x y] (f (g x y)))
([x y z] (f (g x y z)))
([x y z & args] (f (apply g x y z args)))))
([f g & fs]
(reduce rec (list* f g fs))))]
(and
(= [3 2 1] ((__ rest reverse) [1 2 3 4]))
(= 5 ((__ (partial + 3) second) [1 2 3 4]))
(= true ((__ zero? #(mod % 8) +) 3 5 7 9))
(= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world"))))
;; 59. Juxtaposition
(let [__
(fn
([f]
(fn
([] [(f)])
([x] [(f x)])
([x y] [(f x y)])
([x y z] [(f x y z)])
([x y z & args] [(apply f x y z args)])))
([f g]
(fn
([] [(f) (g)])
([x] [(f x) (g x)])
([x y] [(f x y) (g x y)])
([x y z] [(f x y z) (g x y z)])
([x y z & args] [(apply f x y z args) (apply g x y z args)])))
([f g h]
(fn
([] [(f) (g) (h)])
([x] [(f x) (g x) (h x)])
([x y] [(f x y) (g x y) (h x y)])
([x y z] [(f x y z) (g x y z) (h x y z)])
([x y z & args] [(apply f x y z args) (apply g x y z args) (apply h x y z args)])))
([f g h & fs]
(let [fs (list* f g h fs)]
(fn
([] (reduce #(conj %1 (%2)) [] fs))
([x] (reduce #(conj %1 (%2 x)) [] fs))
([x y] (reduce #(conj %1 (%2 x y)) [] fs))
([x y z] (reduce #(conj %1 (%2 x y z)) [] fs))
([x y z & args] (reduce #(conj %1 (apply %2 x y z args)) [] fs))))))]
(and
(= [21 6 1] ((__ + max min) 2 3 5 1 6 4))
(= ["HELLO" 5] ((__ #(.toUpperCase %) count) "hello"))
(= [2 6 4] ((__ :a :c :b) {:a 2, :b 4, :c 6, :d 8 :e 10}))))
;; 60. Sequence Reductions
(let [__
(fn rec
([f coll]
(lazy-seq
(if-let [s (seq coll)]
(rec f (first s) (rest s))
(list (f)))))
([f init coll]
(cons init
(lazy-seq
(when-let [s (seq coll)]
(rec f (f init (first s)) (rest s)))))))]
(and
(= (take 5 (__ + (range))) [0 1 3 6 10])
(= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])
(= (last (__ * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120)))
;; 61. Map Construction
;; Write a function which takes a vector of keys and a vector of
;; values and constructs a map from them. Special restrictions: zipmap
(let [__
(fn[keys vals]
(loop [map {}
ks (seq keys)
vs (seq vals)]
(if (and ks vs)
(recur (assoc map (first ks) (first vs))
(next ks)
(next vs))
map)))]
(and
(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
(= (__ [1 2 3 4] ["one" "two" "three"]) {1 "one", 2 "two", 3 "three"})
(= (__ [:foo :bar] ["foo" "bar" "baz"]) {:foo "foo", :bar "bar"})))
;; 62. Re-implement iterate
;; Given a side-effect free function f and an initial value x write a
;; function which returns an infinite lazy sequence of x, (f x), (f (f
;; x)), (f (f (f x))), etc. Special restrictions: iterate
(let [__
(fn [f base]
(let [aux
(fn step [curr]
(lazy-seq
((fn[]
(cons curr (step (f curr)))))))]
(aux base)))]
(and
(= (take 5 (__ #(* 2 %) 1)) [1 2 4 8 16])
(= (take 100 (__ inc 0)) (take 100 (range)))
(= (take 9 (__ #(inc (mod % 3)) 1)) (take 9 (cycle [1 2 3])))))
;; 63. Group a Sequence
;; Given a function f and a sequence s, write a function which returns
;; a map. The keys should be the values of f applied to each item in
;; s. The value at each key should be a vector of corresponding items
;; in the order they appear in s.
(let [__
(fn [f s]
(reduce
(fn [map x]
(let [key (f x)]
(assoc map key
(conj (get map key []) x))))
{} s))]
(and
(= (__ #(> % 5) [1 3 6 8]) {false [1 3], true [6 8]})
(= (__ #(apply / %) [[1 2] [2 4] [4 6] [3 6]])
{1/2 [[1 2] [2 4] [3 6]], 2/3 [[4 6]]})
(= (__ count [[1] [1 2] [3] [1 2 3] [2 3]])
{1 [[1] [3]], 2 [[1 2] [2 3]], 3 [[1 2 3]]})))
;; 64. Intro to Reduce
;; Reduce takes a 2 argument function and an optional starting
;; value. It then applies the function to the first 2 items in the
;; sequence (or the starting value and the first element of the
;; sequence). In the next iteration the function will be called on
;; the previous return value and the next item from the sequence,
;; thus reducing the entire collection to one value. Don't worry,
;; it's not as complicated as it sounds.
(let [__ +]
(and
(= 15 (reduce __ [1 2 3 4 5]))
(= 0 (reduce __ []))
(= 6 (reduce __ 1 [2 3]))))
;; 65. Black Box Testing
;; Clojure has many sequence types, which act in subtly different
;; ways. The core functions typically convert them into a
;; uniform "sequence" type and work with them that way, but it can be
;; important to understand the behavioral and performance differences
;; so that you know which kind is appropriate for your application.
;; Write a function which takes a collection and returns one
;; of :map, :set, :list, or :vector - describing the type of
;; collection it was given.
;; You won't be allowed to inspect their class or use the built-in
;; predicates like list? - the point is to poke at them and understand
;; their behavior.
;; Special Restrictions: class type Class vector? sequential? list?
;; seq? map? set? instance? getClass
(let [__
(fn[coll]
(let [tmp (conj (empty coll)
[:key :vector] [:key :list] [:key :list])
cnt (count tmp)]
(cond
(= 1 cnt)
:map
(= 2 cnt)
:set
:else
(-> tmp first second))))]
(and
(= :map (__ {:a 1, :b 2}))
(= :list (__ (range (rand-int 20))))
(= :vector (__ [1 2 3 4 5 6]))
(= :set (__ #{10 (rand-int 5)}))
(= [:map :set :vector :list] (map __ [{} #{} [] ()]))))
;; 66. Greatest Common Divisor
;; Given two integers, write a function which returns the greatest
;; common divisor.
(let [__
(fn[a b]
(cond
(< a b)
(recur a (- b a))
(< b a)
(recur (- a b) b)
:else
a))]
(and
(= (__ 2 4) 2)
(= (__ 10 5) 5)
(= (__ 5 7) 1)
(= (__ 1023 858) 33)))
;; 67. Prime Numbers
;; Write a function which returns the first x number of prime numbers.
(let [__
(fn[cnt]
(let [prime? (fn[n]
(let [candidates (range 2 n)]
(not (some identity
(map #(zero? (mod n %)) candidates)))))]
(take cnt (filter prime? (iterate inc 2)))))]