-
Notifications
You must be signed in to change notification settings - Fork 0
/
completed-exs.clj
4213 lines (3745 loc) · 151 KB
/
completed-exs.clj
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
;; alanforr's solution to Nothing but the Truth
;; https://4clojure.com/problem/1
;; (= __ true)
true
;; alanforr's solution to Simple Math
;; https://4clojure.com/problem/2
;; (= (- 10 (* 2 3)) __)
4
;; alanforr's solution to Intro to Strings
;; https://4clojure.com/problem/3
;; (= __ (.toUpperCase "hello world"))
"HELLO WORLD"
;; alanforr's solution to Intro to Lists
;; https://4clojure.com/problem/4
;; Lists can be constructed with either a function or a quoted form.
;;
;; (= (list __) '(:a :b :c))
;; (= (list __) '(:a :b :c))
:a :b :c
;; alanforr's solution to Lists: conj
;; https://4clojure.com/problem/5
;; When operating on a list, the conj function will return a new list with one or more items "added" to the front.
;; Note that there are two test cases, but you are expected to supply only one answer, which will cause all the tests to pass.
;;
;; (= __ (conj '(2 3 4) 1))
;;
;; (= __ (conj '(3 4) 2 1))
'(1 2 3 4)
;; alanforr's solution to Intro to Vectors
;; https://4clojure.com/problem/6
;; Vectors can be constructed several ways. You can compare them with lists.
;; Note: the brackets [] surrounding the blanks __ are part of the test case.
;;
;; (= [__] (list :a :b :c) (vec '(:a :b :c)) (vector :a :b :c))
:a :b :c
;; alanforr's solution to Vectors: conj
;; https://4clojure.com/problem/7
;; When operating on a Vector, the conj function will return a new vector with one or more items "added" to the end.
;;
;; (= __ (conj [1 2 3] 4))
;;
;; (= __ (conj [1 2] 3 4))
[1 2 3 4]
;; alanforr's solution to Intro to Sets
;; https://4clojure.com/problem/8
;; Sets are collections of unique values.
;;
;; (= __ (set '(:a :a :b :c :c :c :c :d :d)))
;;
;; (= __ (clojure.set/union #{:a :b :c} #{:b :c :d}))
#{:a :b :c :d}
;; alanforr's solution to Sets: conj
;; https://4clojure.com/problem/9
;; When operating on a set, the conj function returns a new set with one or more keys "added".
;;
;; (= #{1 2 3 4} (conj #{1 4 3} __))
2
;; alanforr's solution to Intro to Maps
;; https://4clojure.com/problem/10
;; 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.
;;
;; (= __ ((hash-map :a 10, :b 20, :c 30) :b))
;;
;; (= __ (:b {:a 10, :b 20, :c 30}))
20
;; alanforr's solution to Maps: conj
;; https://4clojure.com/problem/11
;; When operating on a map, the conj function returns a new map with one or more key-value pairs "added".
;;
;; (= {:a 1, :b 2, :c 3} (conj {:a 1} __ [:c 3]))
[:b 2]
;; alanforr's solution to Intro to Sequences
;; https://4clojure.com/problem/12
;; All Clojure collections support sequencing. You can operate on sequences with functions like first, second, and last.
;;
;; (= __ (first '(3 2 1)))
;;
;; (= __ (second [2 3 4]))
;;
;; (= __ (last (list 1 2 3)))
3
;; alanforr's solution to Sequences: rest
;; https://4clojure.com/problem/13
;; The rest function will return all the items of a sequence except the first.
;;
;; (= __ (rest [10 20 30 40]))
[20 30 40]
;; alanforr's solution to Intro to Functions
;; https://4clojure.com/problem/14
;; Clojure has many different ways to create functions.
;;
;; (= __ ((fn add-five [x] (+ x 5)) 3))
;;
;; (= __ ((fn [x] (+ x 5)) 3))
;;
;; (= __ (#(+ % 5) 3))
;;
;; (= __ ((partial + 5) 3))
8
;; alanforr's solution to Double Down
;; https://4clojure.com/problem/15
;; Write a function which doubles a number.
;;
;; (= (__ 2) 4)
;;
;; (= (__ 3) 6)
;;
;; (= (__ 11) 22)
;;
;; (= (__ 7) 14)
(fn [x] (* 2 x))
;; alanforr's solution to Hello World
;; https://4clojure.com/problem/16
;; Write a function which returns a personalized greeting.
;;
;; (= (__ "Dave") "Hello, Dave!")
;;
;; (= (__ "Jenn") "Hello, Jenn!")
;;
;; (= (__ "Rhea") "Hello, Rhea!")
(fn [x] (str "Hello, " x "!"))
;; alanforr's solution to Sequences: map
;; https://4clojure.com/problem/17
;; Write a function which returns a personalized greeting.
;;
;; (= (__ "Dave") "Hello, Dave!")
;;
;; (= (__ "Jenn") "Hello, Jenn!")
;;
;; (= (__ "Rhea") "Hello, Rhea!")
'(6 7 8)
;; alanforr's solution to Sequences: filter
;; https://4clojure.com/problem/18
;; 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.
;;
;; (= __ (filter #(> % 5) '(3 4 5 6 7)))
'(6 7)
;; alanforr's solution to Local bindings
;; https://4clojure.com/problem/35
;; Clojure lets you give local names to values using the special let-form.
;;
;; (= __ (let [x 5] (+ 2 x)))
;;
;; (= __ (let [x 3, y 10] (- y x)))
;;
;; (= __ (let [x 21] (let [y 3] (/ x y))))
7
;; alanforr's solution to Let it Be
;; https://4clojure.com/problem/36
;; Can you bind x, y, and z so that these are all true?
;;
;; (= 10 (let __ (+ x y)))
;;
;; (= 4 (let __ (+ y z)))
;;
;; (= 1 (let __ z))
[x 7 y 3 z 1]
;; alanforr's solution to Regular Expressions
;; https://4clojure.com/problem/37
;; Regex patterns are supported with a special reader macro.
;;
;; (= __ (apply str (re-seq #"[A-Z]+" "bA1B3Ce ")))
"ABC"
;; alanforr's solution to Intro to Reduce
;; https://4clojure.com/problem/64
;; 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.
;;
;; (= 15 (reduce __ [1 2 3 4 5]))
;;
;; (= 0 (reduce __ []))
;;
;; (= 6 (reduce __ 1 [2 3]))
+
;; alanforr's solution to Simple Recursion
;; https://4clojure.com/problem/57
;; A recursive function is a function which calls itself. This is one of the fundamental techniques used in functional programming.
;;
;; (= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5))
'(5 4 3 2 1)
;; alanforr's solution to Rearranging Code: ->
;; https://4clojure.com/problem/71
;; The -> macro threads an expression x through a variable number of forms. First, x is inserted as the second item in the first form, making a list of it if it is not a list already. Then the first form is inserted as the second item in the second form, making a list of that form if necessary. This process continues for all the forms. Using -> can sometimes make your code more readable.
;;
;; (= (__ (sort (rest (reverse [2 5 4 1 3 6]))))
;; (-> [2 5 4 1 3 6] (reverse) (rest) (sort) (__))
;; 5)
last
;; alanforr's solution to Recurring Theme
;; https://4clojure.com/problem/68
;; Clojure only has one non-stack-consuming looping construct: recur. Either a function or a loop can be used as the recursion point. Either way, recur rebinds the bindings of the recursion point to the values it is passed. Recur must be called from the tail-position, and calling it elsewhere will result in an error.
;;
;; (= __
;; (loop [x 5
;; result []]
;; (if (> x 0)
;; (recur (dec x) (conj result (+ 2 x)))
;; result)))
'(7 6 5 4 3)
;; alanforr's solution to Rearranging Code: ->>
;; https://4clojure.com/problem/72
;; The ->> macro threads an expression x through a variable number of forms. First, x is inserted as the last item in the first form, making a list of it if it is not a list already. Then the first form is inserted as the last item in the second form, making a list of that form if necessary. This process continues for all the forms. Using ->> can sometimes make your code more readable.
;;
;; (= (__ (map inc (take 3 (drop 2 [2 5 4 1 3 6]))))
;; (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (__))
;; 11)
apply +
;; alanforr's solution to A nil key
;; https://4clojure.com/problem/134
;; Write a function which, given a key and map, returns true iff the map contains an entry with that key and its value is nil.
;;
;; (true? (__ :a {:a nil :b 2}))
;;
;; (false? (__ :b {:a nil :b 2}))
;;
;; (false? (__ :c {:a nil :b 2}))
;; (false? (__ :c {:a nil :b 2}))
(fn [x y] (and (contains? y x) (nil? (y x))))
;; alanforr's solution to For the win
;; https://4clojure.com/problem/145
;; Clojure's for macro is a tremendously versatile mechanism for producing a sequence based on some other sequence(s). It can take some time to understand how to use it properly, but that investment will be paid back with clear, concise sequence-wrangling later. With that in mind, read over these for expressions and try to see how each of them produces the same result.
;;
;; (= __ (for [x (range 40)
;; :when (= 1 (rem x 4))]
;; x))
;;
;; (= __ (for [x (iterate #(+ 4 %) 0)
;; :let [z (inc x)]
;; :while (< z 40)]
;; z))
;;
;; (= __ (for [[x y] (partition 2 (range 20))]
;; (+ x y)))
'(1 5 9 13 17 21 25 29 33 37)
;; alanforr's solution to Logical falsity and truth
;; https://4clojure.com/problem/162
;; In Clojure, only nil and false represent the values of logical falsity in conditional tests - anything else is logical truth.
;;
;; (= __ (if-not false 1 0))
;;
;; (= __ (if-not nil 1 0))
;;
;; (= __ (if true 1 0))
;;
;; (= __ (if [] 1 0))
;;
;; (= __ (if [0] 1 0))
;;
;; (= __ (if 0 1 0))
;;
;; (= __ (if 1 1 0))
1
;; alanforr's solution to Subset and Superset
;; https://4clojure.com/problem/161
;; Set A is a subset of set B, or equivalently B is a superset of A, if A is "contained" inside B. A and B may coincide.
;;
;; (clojure.set/superset? __ #{2})
;;
;; (clojure.set/subset? #{1} __)
;;
;; (clojure.set/superset? __ #{1 2})
;;
;; (clojure.set/subset? #{1 2} __)
#{1 2}
;; alanforr's solution to Map Defaults
;; https://4clojure.com/problem/156
;; When retrieving values from a map, you can specify default values in case the key is not found:
;; (= 2 (:foo {:bar 0, :baz 1} 2))
;; However, what if you want the map itself to contain the default values? Write a function which takes a default value and a sequence of keys and constructs a map.
;;
;; (= (__ 0 [:a :b :c]) {:a 0 :b 0 :c 0})
;;
;; (= (__ "x" [1 2 3]) {1 "x" 2 "x" 3 "x"})
;;
;; (= (__ [:a :b] [:foo :bar]) {:foo [:a :b] :bar [:a :b]})
(fn [x y] (let [xs (repeat (count y) x)
z (map vector y xs)]
(into {} z)))
;; alanforr's solution to Last Element
;; https://4clojure.com/problem/19
;; Special Restrictions
;; last
;; Write a function which returns the last element in a sequence.
;;
;; (= (__ [1 2 3 4 5]) 5)
;;
;; (= (__ '(5 4 3)) 3)
;;
;; (= (__ ["b" "c" "d"]) "d")
(fn [x] (nth x (- (count x) 1)))
;; alanforr's solution to Penultimate Element
;; https://4clojure.com/problem/20
;; Write a function which returns the second to last element from a sequence.
;;
;; (= (__ (list 1 2 3 4 5)) 4)
;;
;; (= (__ ["a" "b" "c"]) "b")
;;
;; (= (__ [[1 2] [3 4]]) [1 2])
(fn [x] (nth x (- (count x) 2)))
;; alanforr's solution to Nth Element
;; https://4clojure.com/problem/21
;; Special Restrictions
;; nth
;; Write a function which returns the Nth element from a sequence.
;;
;; (= (__ '(4 5 6 7) 2) 6)
;;
;; (= (__ [:a :b :c] 0) :a)
;;
;; (= (__ [1 2 3 4] 1) 2)
;;
;; (= (__ '([1 2] [3 4] [5 6]) 2) [5 6])
(fn [x n] (first (drop n x)))
;; alanforr's solution to Count a Sequence
;; https://4clojure.com/problem/22
;; Special Restrictions
;; count
;; Write a function which returns the total number of elements in a sequence.
;;
;; (= (__ '(1 2 3 3 1)) 5)
;;
;; (= (__ "Hello World") 11)
;;
;; (= (__ [[1 2] [3 4] [5 6]]) 3)
;;
;; (= (__ '(13)) 1)
;;
;; (= (__ '(:a :b :c)) 3)
(fn [x]
(loop [ct 0 thing x]
(if (empty? thing)
ct
(recur (inc ct) (rest thing)))))
;; alanforr's solution to Sum It All Up
;; https://4clojure.com/problem/24
;; Write a function which returns the sum of a sequence of numbers.
;;
;; (= (__ [1 2 3]) 6)
;;
;; (= (__ (list 0 -2 5 5)) 8)
;;
;; (= (__ #{4 2 1}) 7)
;;
;; (= (__ '(0 0 -1)) -1)
;;
;; (= (__ '(1 10 3)) 14)
reduce +
;; alanforr's solution to Find the odd numbers
;; https://4clojure.com/problem/25
;; Write a function which returns only the odd numbers from a sequence.
;;
;; (= (__ #{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))
filter odd?
;; alanforr's solution to Reverse a Sequence
;; https://4clojure.com/problem/23
;; Special Restrictions
;; reverse
;; rseq
;; Write a function which reverses a sequence.
;;
;; (= (__ [1 2 3 4 5]) [5 4 3 2 1])
;;
;; (= (__ (sorted-set 5 7 2 7)) '(7 5 2))
;;
;; (= (__ [[1 2][3 4][5 6]]) [[5 6][3 4][1 2]])
(fn [x]
(let [revable (into [] x)
cx (count x)
ln (dec' cx)]
(map #(nth revable (-' ln %)) (range cx))))
;; alanforr's solution to Palindrome Detector
;; https://4clojure.com/problem/27
;; Write a function which returns true if the given sequence is a palindrome.
;; Hint: "racecar" does not equal '(\r \a \c \e \c \a \r)
;;
;; (false? (__ '(1 2 3 4 5)))
;;
;; (true? (__ "racecar"))
;;
;; (true? (__ [:foo :bar :foo]))
;;
;; (true? (__ '(1 1 3 3 1 1)))
;;
;; (false? (__ '(:a :b :c)))
(fn [x] (= (clojure.string/join x) (clojure.string/join (reverse x))))
;; alanforr's solution to Fibonacci Sequence
;; https://4clojure.com/problem/26
;; Write a function which returns the first X fibonacci numbers.
;;
;; (= (__ 3) '(1 1 2))
;;
;; (= (__ 6) '(1 1 2 3 5 8))
;;
;; (= (__ 8) '(1 1 2 3 5 8 13 21))
(fn [n]
(letfn [(fib [a b] (cons a (lazy-seq (fib b (+ b a)))))
(fib-n [a b n] (take n (fib a b)))]
(fib-n 1 1 n)))
;; alanforr's solution to Maximum value
;; https://4clojure.com/problem/38
;; Special Restrictions
;; max
;; max-key
;; Write a function which takes a variable number of parameters and returns the maximum value.
;;
;; (= (__ 1 8 3 4) 8)
;;
;; (= (__ 30 20) 30)
;;
;; (= (__ 45 67 11) 67)
(fn [& x]
(loop [cnt 0 thing (first x)]
(if (= cnt (count x))
thing
(recur (inc cnt) (if (> (nth x cnt) thing) (nth x cnt) thing)))))
;; alanforr's solution to Get the Caps
;; https://4clojure.com/problem/29
;; Write a function which takes a string and returns a new string containing only the capital letters.
;;
;; (= (__ "HeLlO, WoRlD!") "HLOWRD")
;;
;; (empty? (__ "nothing"))
;;
;; (= (__ "$#A(*&987Zf") "AZ")
(fn [x] (clojure.string/join (re-seq #"[A-Z]" x)))
;; alanforr's solution to Duplicate a Sequence
;; https://4clojure.com/problem/32
;; Write a function which duplicates each element of a sequence.
;;
;; (= (__ [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]))
(fn [x] (interleave x x))
;; alanforr's solution to Intro to some
;; https://4clojure.com/problem/48
;; 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.
;;
;; (= __ (some #{2 7 6} [5 6 7 8]))
;;
;; (= __ (some #(when (even? %) %) [5 6 7 8]))
6
;; alanforr's solution to Implement range
;; https://4clojure.com/problem/34
;; Special Restrictions
;; range
;; Write a function which creates a list of all integers in a given range.
;;
;; (= (__ 1 4) '(1 2 3))
;;
;; (= (__ -2 2) '(-2 -1 0 1))
;;
;; (= (__ 5 8) '(5 6 7))
(fn [x y] (take (- y x) (iterate inc x)))
;; alanforr's solution to Flatten a Sequence
;; https://4clojure.com/problem/28
;; Special Restrictions
;; flatten
;; Write a function which flattens a sequence.
;;
;; (= (__ '((1 2) 3 [4 [5 6]])) '(1 2 3 4 5 6))
;;
;; (= (__ ["a" ["b"] "c"]) '("a" "b" "c"))
;;
;; (= (__ '((((:a))))) '(:a))
(fn [s]
(loop [res [] left s]
(cond
(empty? left) res
(coll? (first left)) (recur res (concat (first left) (rest left)))
:else (recur (conj res (first left)) (rest left)))))
;; alanforr's solution to Interleave Two Seqs
;; https://4clojure.com/problem/39
;; Special Restrictions
;; interleave
;; Write a function which takes two sequences and returns the first item from each, then the second item from each, then the third, etc.
;;
;; (= (__ [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])
(fn [s1 s2] (mapcat #(vector %1 %2) s1 s2))
;; alanforr's solution to Factorial Fun
;; https://4clojure.com/problem/42
;; Write a function which calculates factorials.
;;
;; (= (__ 1) 1)
;;
;; (= (__ 3) 6)
;;
;; (= (__ 5) 120)
;;
;; (= (__ 8) 40320)
(fn [n] (reduce * (range 1 (+ n 1))))
;; alanforr's solution to Compress a Sequence
;; https://4clojure.com/problem/30
;; Write a function which removes consecutive duplicates from a sequence.
;;
;; (= (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]))
(fn [s]
(loop [res [(first s)] left (rest s)]
(cond
(empty? left) res
(= (last res) (first left)) (recur res (rest left))
:else (recur (conj res (first left)) (rest left)))))
;; alanforr's solution to Contain Yourself
;; https://4clojure.com/problem/47
;; 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.
;;
;; (contains? #{4 5 6} __)
;;
;; (contains? [1 1 1 1 1] __)
;;
;; (contains? {4 :a 2 :b} __)
;;
;; (not (contains? [1 2 4] __))
4
;; alanforr's solution to Replicate a Sequence
;; https://4clojure.com/problem/33
;; Write a function which replicates each element of a sequence a variable number of times.
;;
;; (= (__ [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])
(fn [s n] (mapcat #(repeat n %) s))
;; alanforr's solution to Intro to Iterate
;; https://4clojure.com/problem/45
;; The iterate function can be used to produce an infinite lazy sequence.
;;
;; (= __ (take 5 (iterate #(+ 3 %) 1)))
'(1 4 7 10 13)
;; alanforr's solution to Interpose a Seq
;; https://4clojure.com/problem/40
;; Special Restrictions
;; interpose
;; Write a function which separates the items of a sequence by an arbitrary value.
;;
;; (= (__ 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])
(fn [el s]
(rest
(interleave
(repeat (inc (count s)) el)
s)))
;; alanforr's solution to Pack a Sequence
;; https://4clojure.com/problem/31
;; Write a function which packs consecutive duplicates into sub-lists.
;;
;; (= (__ [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])))
(fn [s]
(loop [res [] left s]
(if
(empty? left) res
(recur (concat res [(take-while #(= (first left) %) left)])
(drop-while #(= (first left) %) left)))))
;; alanforr's solution to Drop Every Nth Item
;; https://4clojure.com/problem/41
;; Write a function which drops every Nth item from a sequence.
;;
;; (= (__ [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])
(fn [x n]
(let
[indices (remove #(= (- n 1) (mod % n)) (range 0 (count x)))]
(map #(nth x %) indices)))
;; alanforr's solution to Intro to Destructuring
;; https://4clojure.com/problem/52
;; Let bindings and function parameter lists support destructuring.
;;
;; (= [2 4] (let [[a b c d e] [0 1 2 3 4]] __))
(list c e)
;; alanforr's solution to Split a sequence
;; https://4clojure.com/problem/49
;; Special Restrictions
;; split-at
;; Write a function which will split a sequence into two parts.
;;
;; (= (__ 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]]])
(fn [n s] [(take n s) (drop n s)])
;; alanforr's solution to Advanced Destructuring
;; https://4clojure.com/problem/51
;; 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] __] [a b c d]))
[1 2 3 4 5]
;; alanforr's solution to A Half-Truth
;; https://4clojure.com/problem/83
;; Write a function which takes a variable number of booleans. Your function should return true if some of the parameters are true, but not all of the parameters are true. Otherwise your function should return false.
;;
;; (= false (__ false false))
;;
;; (= true (__ true false))
;;
;; (= false (__ true))
;;
;; (= true (__ false true false))
;;
;; (= false (__ true true true))
;;
;; (= true (__ true true true false))
(fn [& x]
(let [ctruex (count (filter true? x))
cx (count x)]
(cond
(= ctruex cx) false
(zero? ctruex) false
:else true)))
;; alanforr's solution to Map Construction
;; https://4clojure.com/problem/61
;; Special Restrictions
;; zipmap
;; Write a function which takes a vector of keys and a vector of values and constructs a map from them.
;;
;; (= (__ [: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"})
(fn [c1 c2]
(let [kvv (map vector c1 c2)
kvm (map #(apply hash-map %) kvv)]
(apply merge kvm)))
;; alanforr's solution to Greatest Common Divisor
;; https://4clojure.com/problem/66
;; Given two integers, write a function which returns the greatest common divisor.
;;
;; (= (__ 2 4) 2)
;;
;; (= (__ 10 5) 5)
;;
;; (= (__ 5 7) 1)
;;
;; (= (__ 1023 858) 33)
(fn [a b]
(loop [c a d b]
(cond
(= d 0) c
:else (recur d (rem c d)))))
;; alanforr's solution to Comparisons
;; https://4clojure.com/problem/166
;; For any orderable data type it's possible to derive all of the basic comparison operations (<, ≤, =, ≠, ≥, and >) from a single operation (any operator but = or ≠ will work). Write a function that takes three arguments, a less than operator for the data and two items to compare. The function should return a keyword describing the relationship between the two items. The keywords for the relationship between x and y are as follows:
;; x = y → :eq
;; x > y → :gt
;; x < y → :lt
;;
;; (= :gt (__ < 5 1))
;;
;; (= :eq (__ (fn [x y] (< (count x) (count y))) "pear" "plum"))
;;
;; (= :lt (__ (fn [x y] (< (mod x 5) (mod y 5))) 21 3))
;;
;; (= :gt (__ > 0 2))
(fn [compy x y]
(cond
(compy x y) :lt
(and (not (compy x y)) (not (compy y x))) :eq
:else :gt))
;; alanforr's solution to Set Intersection
;; https://4clojure.com/problem/81
;; Special Restrictions
;; intersection
;; Write a function which returns the intersection of two sets. The intersection is the sub-set of items that each set has in common.
;;
;; (= (__ #{0 1 2 3} #{2 3 4 5}) #{2 3})
;;
;; (= (__ #{0 1 2} #{3 4 5}) #{})
;;
;; (= (__ #{:a :b :c :d} #{:c :e :a :f :d}) #{:a :c :d})
(fn [s1 s2]
(clojure.set/difference s1 (clojure.set/difference s1 s2)))
;; alanforr's solution to Re-implement Iterate
;; https://4clojure.com/problem/62
;; Special Restrictions
;; 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.
;;
;; (= (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])))
(fn [f x]
(letfn [(iter [g y] (cons y (lazy-seq (iter g (g y)))))]
(iter f x)))
;; alanforr's solution to Simple closures
;; https://4clojure.com/problem/107
;; Lexical scope and first-class functions are two of the most basic building blocks of a functional language like Clojure. When you combine the two together, you get something very powerful called lexical closures. With these, you can exercise a great deal of control over the lifetime of your local bindings, saving their values for use later, long after the code you're running now has finished.
;; It can be hard to follow in the abstract, so let's build a simple closure. Given a positive integer n, return a function (f x) which computes xn. Observe that the effect of this is to preserve the value of n for use outside the scope in which it is defined.
;;
;; (= 256 ((__ 2) 16),
;; ((__ 8) 2))
;;
;; (= [1 8 27 64] (map (__ 3) [1 2 3 4]))
;;
;; (= [1 2 4 8 16] (map #((__ %) 2) [0 1 2 3 4]))
(fn [b]
(letfn [(expon [n x] (reduce * (repeat n x)))]
(partial expon b) ))
;; alanforr's solution to Product Digits
;; https://4clojure.com/problem/99
;; Write a function which multiplies two numbers and returns the result as a sequence of its digits.
;;
;; (= (__ 1 1) [1])
;;
;; (= (__ 99 9) [8 9 1])
;;
;; (= (__ 999 99) [9 8 9 0 1])
(fn [x y]
(let [ds (str (* x y))
dstrs (map str ds)]
(map read-string dstrs)))
;; alanforr's solution to Cartesian Product
;; https://4clojure.com/problem/90
;; Write a function which calculates the Cartesian product of two sets.
;;
;; (= (__ #{"ace" "king" "queen"} #{"♠" "♥" "♦" "♣"})
;; #{["ace" "♠"] ["ace" "♥"] ["ace" "♦"] ["ace" "♣"]
;; ["king" "♠"] ["king" "♥"] ["king" "♦"] ["king" "♣"]
;; ["queen" "♠"] ["queen" "♥"] ["queen" "♦"] ["queen" "♣"]})
;;
;; (= (__ #{1 2 3} #{4 5})
;; #{[1 4] [2 4] [3 4] [1 5] [2 5] [3 5]})
;;
;; (= 300 (count (__ (into #{} (range 10))
;; (into #{} (range 30)))))
(fn [xs ys]
(set (for [x xs y ys] [x y])))
;; alanforr's solution to Group a Sequence
;; https://4clojure.com/problem/63
;; Special Restrictions
;; group-by
;; 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.
;;
;; (= (__ #(> % 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]]})
(fn [f s]
(let [vs (distinct (map f s))
mv (fn [v] {v (filter #(= v (f %)) s)})
ms (map mv vs)]
(apply merge ms)))
;; alanforr's solution to Read a binary number
;; https://4clojure.com/problem/122
;; Convert a binary number, provided in the form of a string, to its numerical value.
;;
;; (= 0 (__ "0"))
;;
;; (= 7 (__ "111"))
;;
;; (= 8 (__ "1000"))
;;
;; (= 9 (__ "1001"))
;;
;; (= 255 (__ "11111111"))
;;
;; (= 1365 (__ "10101010101"))
;;
;; (= 65535 (__ "1111111111111111"))
(fn [a]
(letfn [(rds [s] (map #(read-string (str %)) s))
(expon [b n] (if (zero? n) 1 (reduce *' (repeat n b))))
(convertfrombin [s] (reduce +' (map-indexed (fn [i d] (*' d (expon 2 i))) (reverse (rds s)))))]
(convertfrombin a)))
;; alanforr's solution to Symmetric Difference
;; https://4clojure.com/problem/88
;; Write a function which returns the symmetric difference of two sets. The symmetric difference is the set of items belonging to one but not both of the two sets.
;;
;; (= (__ #{1 2 3 4 5 6} #{1 3 5 7}) #{2 4 6 7})
;;
;; (= (__ #{:a :b :c} #{}) #{:a :b :c})
;;
;; (= (__ #{} #{4 5 6}) #{4 5 6})
;;
;; (= (__ #{[1 2] [2 3]} #{[2 3] [3 4]}) #{[1 2] [3 4]})
(fn [x y] (clojure.set/union (clojure.set/difference x y) (clojure.set/difference y x)))
;; alanforr's solution to dot product
;; https://4clojure.com/problem/143
;; Create a function that computes the dot product of two sequences. You may assume that the vectors will have the same length.
;;
;; (= 0 (__ [0 1 0] [1 0 0]))
;;
;; (= 3 (__ [1 1 1] [1 1 1]))
;;
;; (= 32 (__ [1 2 3] [4 5 6]))
;;
;; (= 256 (__ [2 5 6] [100 10 1]))
(fn [x y]
(reduce +
(map #(* (nth x %) (nth y %)) (range (count x)))))
;; alanforr's solution to Through the Looking Class
;; https://4clojure.com/problem/126
;; Enter a value which satisfies the following:
;;
;; (let [x __]
;; (and (= (class x) x) x))
Class
;; alanforr's solution to Infix Calculator
;; https://4clojure.com/problem/135
;; Your friend Joe is always whining about Lisps using the prefix notation for math. Show him how you could easily write a function that does math using the infix notation. Is your favorite language that flexible, Joe? Write a function that accepts a variable length mathematical expression consisting of numbers and the operations +, -, *, and /. Assume a simple calculator that does not do precedence and instead just calculates left to right.
;;
;; (= 7 (__ 2 + 5))
;;
;; (= 42 (__ 38 + 48 - 2 / 2))
;;