-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path4clojure.clj
More file actions
1337 lines (1210 loc) · 51.7 KB
/
Copy path4clojure.clj
File metadata and controls
1337 lines (1210 loc) · 51.7 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
;;
;; 4clojure problem solving http://www.4clojure.com/
;;
;; Username: life0fun
;; Rank: 162 out of 14099
;; Problems Solved: 135
;; Rank: 171 out of 13992
;; Problems Solved: 134
;; Rank: 175 out of 13915
;; Problems Solved: 133
;;
;; find indices of a val in a vector
;; for string array, use string array's .indexOf method.
;;
(use '[clojure.contrib.seq-utils :only (positions)])
(positions #{99} [0 99 3334 53 2 5 99 2 55 63])
(def v ["one" "two" "three"])
(.indexOf v "two")
;; use java.lang.String to process strings.
(defn parse-line [line]
(let [tokens (.split (.toLowerCase line) " ")]
(map #(vector % 1) tokens)))
(parse-line "Twas brillig and the slithy toves")
(use 'clojure.contrib.io')
(read-line "/Users/e51141/tmp/x")
;;
;; compress 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 [l]
(loop [x l ret []]
(if (nil? (seq x))
ret
(let [z (conj ret (first x))]
(if-not (= (first x) (last ret))
(recur (rest x) z)
(recur (rest x) ret))))))
;;
;; pack a sequence.
;;
(= (__ [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 [l]
(reduce (fn [ret this]
(let [l (last ret)]
(if-not (= this (last l))
(conj ret [this])
(-> ret (pop) (conj (conj l this)) )))) [] l))
;;
;; Duplicate a Sequence
;; two ways of list comprehension, map/reduce, or loop with destructuring.
;;
(= (__ [1 2 3]) '(1 1 2 2 3 3)')
(= (__ [[1 2] [3 4]]) '([1 2] [1 2] [3 4] [3 4])')
(fn [l]
(loop [[hd & body] l ret []]
(if (nil? hd)
ret
(recur body (conj ret hd hd)))))
;;
;; Replicate a Sequence
;;
(= (__ [[1 2] [3 4]] 2) '([1 2] [1 2] [3 4] [3 4])')
(= (__ [44 33] 2) [44 44 33 33])
(fn [l n]
(loop [[hd & body] l ret []]
(if (nil? hd)
ret
(recur body (reduce (fn [r c] (conj r c)) ret (repeat n hd))))))
;;
;; interpose
;; (= (apply str (__ ", " ["one" "two" "three"])) "one, two, three")
;;
(fn [sep col]
(drop-last (reduce (fn [ret this]
(conj ret (first this) (second this)))
[] (map (fn [e] [e sep]) col) )))
;;
;; drop every nth
;; (= (__ [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
;;
(fn [col step]
(loop [i 1 c col ret []]
(if (nil? (seq c))
ret
(do
(if-not (= 0 (mod i step))
(recur (inc i) (rest c) (conj ret (first c)))
(recur (inc i) (rest c) ret) )))))
(fn [col step]
(keep-indexed
(fn [idx item]
(if-not (= 0 (mod (+ idx 1) step)) item)) col))
;;
;; Flipping out, ret a fn that with arg order reversed.
;; (= 3 ((__ nth) 2 [1 2 3 4 5]))
(fn [origfn]
(fn [ & args ]
(apply origfn (reverse args))))
;;
;; re-impl map with recursion
;; pattern: build list from leaf where empty [] reted for parents to conj.
;; lazy-seq cons head results to the result of self recursion on the rest .
;;
(fn mymap [f xs]
(if (empty? xs)
[]
(lazy-seq (cons (f (first xs)) (mymap f (rest xs))))))
(= [3 4 5 6 7]
(__ inc [2 3 4 5 6]))
;;
;; Infix Calulator
;; first pass transform by consolidate * /, then left to right cal.
;;
(fn [ & infix]
(letfn [ (rm-timediv [infix]
(loop [ infix infix operator [] operand []]
(let [hd (first infix) hdtype (type hd)]
(if (nil? hd)
[operator operand]
(if (= hdtype java.lang.Integer)
(recur (next infix) operator (conj operand hd))
(if (or (= hdtype clojure.core$_STAR_)
(= hdtype clojure.core$_SLASH_))
(recur (next (next infix)) operator (conj (vec (butlast operand)) (hd (last operand) (first (next infix)))))
(recur (next infix) (conj (vec operator) hd) operand)))))))]
(let [[operator operand] (rm-timediv infix)]
(prn operator operand)
(loop [op operator opd (rest operand) tot (first operand) ]
(if (empty? op)
tot
(recur (rest op) (rest opd) ((first op) tot (first opd))))))))
;;
;; valid whether a tree is btree
(defn tree [tree]
(letfn [(btree [root]
(let [ t (type root) ]
(if (or (= t clojure.lang.Keyword)
(= t java.lang.Integer)
(= t java.lang.Long)
(= t java.lang.Boolean)
(= t nil))
true
(if (and (= 3 (count root))
(btree (first root))
(btree (second root))
(btree (nth root 2)))
true
false))))
;;
;; symmetry
;; if the left half of the tree is the mirror image of the right half of the tree
;;
(fn symmetry [xs]
(letfn [(seqns? [xs]
(if (or (= (type xs) clojure.lang.PersistentVector)
(= (type xs) clojure.lang.PersistentList))
true
false))
(mirror [xs]
;; ret a seq of mirror-ed btree
(if-not (seqns? xs)
xs
(vector (first xs) (mirror (last xs)) (mirror (second xs)))))]
(let [root (first xs) lc (second xs) rc (last xs)]
(if (= (mirror (second xs))
(last xs))
true
false))))
(= (__ [1 [2 nil [3 [4 [5 nil nil] [6 nil nil]] nil]]
[2 [3 nil [4 [6 nil nil] [5 nil nil]]] nil]])
true)
;;
;; split a seq by type, reduce to a map and get the value.
;; (= (set (__ [1 :a 2 :b 3 :c])) #{[1 2 3] [:a :b :c]})
;; (= (set (__ [:a "foo" "bar" :b])) #{[:a :b] ["foo" "bar"]})
;;
(fn [col]
(vals
(reduce (fn [ret this]
(condp = (type this)
java.lang.String (assoc ret :string ((fnil conj []) (get ret :string) this))
java.lang.Integer (assoc ret :number ((fnil conj []) (get ret :number) this))
java.lang.Long (assoc ret :number ((fnil conj []) (get ret :number) this))
clojure.lang.Keyword (assoc ret :keyword ((fnil conj []) (get ret :keyword) this))
clojure.lang.PersistentList (assoc ret :list ((fnil conj []) (get ret :list) this))
clojure.lang.PersistentVector (assoc ret :vector ((fnil conj []) (get ret :vector) this))
)) {} col)))
;;
;; Longest Increasing Sub-Seq, consecutive sub-sequence of increasing numbers
;; thinking functionly, enum all increasing list from each pos, or break list into sublists with each sublist
;; an increasing sublist that matches the requirement. And reduce on the sublist.
;; optimze to O(n)
;; (= (__ [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])
;;
(fn [col]
(let [reslt
(loop [[hd & rst] col ret []]
(if (nil? hd)
ret
(do
(let [t (last (last ret))]
(if-not (nil? t)
(if (= 1 (- hd t))
(recur rst (conj (pop ret) (conj (last ret) hd)))
(recur rst (conj ret [hd])))
(recur rst (conj ret [hd])) ) ))))]
(reduce (fn [ret this]
(if (and (> (count this) (count ret)) (> (count this) 1))
this
ret)) [] reslt)))
;;
;; partition a seq
;; (= (__ 3 (range 8)) '((0 1 2) (3 4 5))')
;;
(fn [neach col]
(filter #(= (count %) neach)
(reduce (fn [ret this]
(let [t (last ret)]
(if (nil? t)
(conj ret [this])
(if (< (count t) neach)
(conj (pop ret) (conj t this))
(conj ret [this]) ))))
[] col)))
;;
;; find distinct items
;;
(fn [col]
(loop [[hd & rst] col m {} out []]
(if (nil? hd)
out
(if (contains? m hd)
(recur rst m out)
(recur rst (assoc m hd 1) (conj out hd)) ))))
;;
;; comp
;; (= 5 ((__ (partial + 3) second) [1 2 3 4]))
;; (= true ((__ zero? #(mod % 8) +) 3 5 7 9))
;;
(fn
([f1 f2 ]
(fn [& args]
(f1 (apply f2 args))))
([f g & fs]
(fn [& args]
(let [fs (reverse (list* f g fs))]
(loop [ret (apply (first fs) args) fs (next fs)] ;; binding eval is left -> right, in order.
(if fs
(recur ((first fs) ret) (next fs)) ;; use next, not rest, as next is strict than rest(lazy)
ret))))))
;;
;; juxtaposition
;;
(fn [f & fns]
(fn [& args]
(let [fs (list* f fns) ret []]
(loop [nxt (next fs) ret (conj ret (apply (first fs) args))]
(if nxt
(recur (next nxt) (conj ret (apply (first nxt) args)))
ret)))))
;;
;; reductions
;; carry the interim result inside recur bindings.
;; when loop condition not met, can ret the interim from recur binding directly.
;; when using loop, not a lazy seq.
;; (= (__ conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])
;;
(fn reduction
([ f col ]
(reduction f (first col) (rest col)))
([f init col]
(loop [c col reduceval init interim (conj [] reduceval)] ;; carry partial result in recur bindings.
(if c
(let [ resl (f reduceval (first c))]
(recur (rest c) resl (conj interim resl))) ;; carry interim inside bindings.
interim ))))
;;
;; lazy reductions
;; lazy seq can not use loop, use recursive call, carry partial result as fn arguments.
;; init actually is the intermediate result at each step. If you need it, then cons it to return seq.
;; (= (take 5 (__ + (range))) [0 1 3 6 10])
;;
(fn reduction
([ f col ]
(lazy-seq
(reduction f (first col) (rest col))))
([f init col]
(lazy-seq ;; lazy-seq to wrap result seq, can put inside cons expr also.
(if-not (seq col)
[init]
(let [rslt (f init (first col))] ;; carry partial result as recursion arguments.
(cons init (reduction f rslt (rest col))))))))
;;
;; my own iterate (x f(x) f(f(x)))
;; use lazy-seq to wrap the result. Like use lazy-seq to wrap the rabbitmq stream.
;;
(fn myiter [f init]
(let [rslt (f init)]
(cons init (lazy-seq (myiter f rslt)))))
;;
;; group-by
;; use update-in and (fnil conj []) to create the ret map and loop carry interim result.
;;
(fn [f col]
(loop [c col grp {}] ;; carry partial result inside recur binding.
(if c
(recur (next c) (update-in grp [(f (first c))] (fnil conj []) (first c)))
grp)))
;;
;; Black Box testing of sequence.
;; (= :map (__ {:}))
;; (= [:map :set :vector :list] (map __ [{} #{} [] ()]))
;;
(fn mytest-type [col]
(if (or (= 2 (count (flatten (vector (last col))))) ;; use flatten to convert list.
(and (empty? col)
(= (into col {:test 1}) {:test 1}))) ;; insert empty map eqs itself.
:map
(if (= (count (conj col :test :test)) (+ 1 (count col)))
:set
(if (= (first (conj col :test1 :test2)) :test2)
:list
:vector))))
;;
;; sieve of prime number
;; all are lazy seq, the magic is that seq needs to starts from 2, not 1.
;;
(fn sieve
([n]
(sieve n (iterate inc 2))) ;; iter to gen a lazy list starting from 2, [2 3 4 ...]
([n l]
(let [hd (first l) bd (rest l)]
(if (zero? n)
[] ;; ret empty [] from bottom for parent to cons result recursion bottom up.
(take n
(lazy-seq ;; lazy-seq is cons head on the recursive self call result
(cons hd (sieve (- n 1) (filter #(not (zero? (mod % hd))) bd))) )))))) ;; filter out all head's multipliers
;;
;; prime sandwich, whether a prime which is also the mean of prev and next prime.
;; memoize recursion fn.
;;
(fn prime-sandwich [p]
(let [siftlist-cache {}] ;; cache intermediate dynamic table
(letfn [(sieve [n l] ;; ret a sifted out list without head's multiplies to get prime
(let [hd (first l) bd (rest l)]
(if (zero? n) ;; bottom situation, zero n
[] ;; ret empty [] to built-up from bottom to upper parent root.
(take n (lazy-seq
(cons hd (sieve (dec n) (filter #(not (zero? (rem % hd))) bd))))))))
(prime-idx [primes p]
(if-not (some #{p} primes)
-1 ; not in primes set, not a prime, ret -1
;; find out the index of prime in seq so to get prev and next
(loop [start 0 end (dec p)]
(let [mid (quot (+ start end) 2)]
(if (= p (nth primes mid))
mid
(if (> p (nth primes mid))
(recur (inc mid) end)
(recur start (dec mid))))))))
(prime? [n]
;; celebrates clojure's java interop, using BigInteger isProbablePrime with 5% certainty
(.isProbablePrime (BigInteger/valueOf n) 5))
(siftlist [n]
(if (= 2 n) ;; bottom, build-up with inf list
(let [ret (filter #(not (zero? (rem % n))) (iterate inc 2))]
(assoc siftlist-cache n ret)
ret)
(if (contains? siftlist-cache n)
(siftlist-cache n)
(let [xs (siftlist (dec n))
hd (first xs)
bd (rest xs)
ret (filter #(not (zero? (rem % hd))) bd)]
(assoc siftlist-cache n ret)
ret))))
(siftlist-yb []
(let [dp (fn [mem-dp n] ;; def dp fn to take memoized boxed dp as first arg
;; inside recursion body, impl logic without worrying dp tab, unbox memoized fn
(let [dp (fn [n] (mem-dp mem-dp n))] ; calling mem-dp, passing itself as the first arg.
(if (= 2 n)
(filter #(not (zero? (rem % n))) (iterate inc 2))
(let [xs (dp (dec n))
hd (first xs)
bd (rest xs)
ret (filter #(not (zero? (rem % hd))) bd)]
ret))))
mem-dp (memoize dp)] ;; memoize a fn, get a memoized fn
(partial mem-dp mem-dp))) ;; pass mem-dp as the second arg to memoized dp
(primelist [n]
(loop [ idx 2 rslt [2]]
;;(let [ sl (siftlist idx) hd (first sl)]
(let [ sl ((siftlist-yb) idx) hd (first sl)]
(if (= hd n)
(conj rslt hd)
(if (> hd n)
rslt
(recur (inc idx) (conj rslt hd)))))))]
(if (and (> p 2)
(even? p))
false
; (let [primes (sieve p (iterate inc 2))
; idx (prime-idx primes p)]
; (if (< idx 1)
; false
; (let [pre (nth primes (dec idx))
; nxt (nth primes (inc idx))]
; (if (= p (/ (+ pre nxt) 2))
; true
; false)))))))
; (let [pl (primelist p) idx (count pl)]
; (if (or (not= (last pl) p)
; (<= idx 2))
; false
; (let [pre (last (butlast pl)) nxt (first ((siftlist-yb) (inc (count pl))))]
; (prn pre p nxt)
; (if (= p (/ (+ pre nxt) 2))
; true
; false))))))))
(if (or (not (prime? p))
(< p 5))
false
(letfn [(prep [n]
(loop [v (dec n)]
(if (prime? v)
v
(recur (dec v)))))
(nxtp [n]
(loop [v (inc n)]
(if (prime? v)
v
(recur (inc v)))))]
(if (= p (/ (+ (prep p) (nxtp p)) 2))
true
false)))) )))
;;
;; merge-with
;;
(fn [f & maps]
(loop [[m & cdr] maps ret {}]
(if (nil? (seq m))
ret
(recur cdr (reduce (fn [ret cur]
(if (contains? ret (first cur))
(update-in ret [(first cur)] f (second cur))
(assoc ret (first cur) (second cur)))) ret m) ))))
;;
;; tic tac
;; create lists using nth nth list logic and interleave.
;; if-let as if else for intermediate value
;;
(fn [col]
(letfn [(check [col]
(reduce (fn [ret c]
(let [[x y z] c]
(if (and (= x y z)
(or (= x :x )
(= x :o )))
x ret)))
nil col))
(intlv [col]
(partition 3 (apply interleave col)))
(diag [col]
(for [x [0 1 2]] (nth (nth col x) x)))
(rdiag [col]
(for [x [0 1 2]] (nth (nth col x) (- 2 x))))
]
(if-let [ret (check col)]
ret
(if-let [ret (check (intlv col))]
ret
(if-let [ret (check (vector (diag col)))]
ret
(if-let [ret (check (vector (rdiag col)))]
ret
nil))))))
;;
;; totient
;;
(fn [n]
(letfn [(gcd [larger smaller]
(loop [l larger s smaller]
(if (not= 0 s)
(recur s (mod l s))
l)))]
(count (filter (fn [i] (= 1 (gcd i n))) (range 1 (inc n))))))
;;
;; fib, iterate gen lazy seq by keeping apply fn to the intermediate results.
;;
(defn fibo [] (map first (iterate (fn [[a b]] [b (+ a b)]) [0N 1N])))
;;
;; intervals is a vec of two int, start end that all int between are contained.
;;
(fn intervals [col]
(let [scol (sort col)]
(reduce (fn [ret n]
(if (= (last (last ret)) (dec n))
(conj (vec (butlast ret)) (vector (first (last ret)) n))
(if (or (nil? (last (last ret)))
(> n (last (last ret))))
(conj ret (vector n n))
ret)))
[] scol)))
(= (__ [10 9 8 1 2 3]) [[1 3] [8 10]])
(= (__ [19 4 17 1 3 10 2 13 13 2 16 4 2 15 13 9 6 14 2 11])
[[1 4] [6 6] [9 11] [13 17] [19 19]])
;;
;; trampoline [fn]
;; you return a function that does the work instead of doing it directly and then
;; call a trampoline function that repeatedly calls its result until it turnes into a real value instead of a function
;; use loop [ret (f)] and invoke the function during recur on loop.
;; use let [ret (f)] and pass the value to recur on the fn recursive call.
;;
(fn mytrampoline
([f]
(loop [ret (f)] ;; or (let [ret (f)] ;; use let, recur on fn call
(if (fn? ret) ;; (if (fn? ret)
(recur (ret)) ;; (recur ret)
ret))) ;; ret))
([f & args]
(mytrampoline #(apply f args))))
;;
;; powerset.
;; to expand a seq, do NOT map, use reduce, as input is a seq, output is a single seq.
;; pass partial result as arg to fn, recur on fn to top-down.
;;
(fn powerset
([coll]
(powerset coll #{}))
([coll ret]
(if (empty? coll)
(conj ret #{})
(recur (rest coll) (reduce (fn [ret this]
(conj ret
(conj this (first coll))))
(conj ret (hash-set (first coll))) ret)))))
(defn powerset [coll]
(if (= 1 (count coll))
(vector (conj #{} (first coll)))
(let [head (first coll)
pret (powerset (rest coll))]
; need to wrap head set into a vector, as concat will peel off the vec,
; and take only the content from it, and form the final seq.
(concat pret
(map (fn [col] (conj col head)) pret)
(vector (conj #{} head))))))
;;
;; k-comb, powerset filter at len k.
;; trans fn taking partial result as args, and top-down built final result based on partial result step by step.
;; (= (__ 2 #{[1 2 3] :a "abc" "efg"}) #{#{[1 2 3] :a} #{[1 2 3] "abc"} #{[1 2 3] "efg"}
;; #{:a "abc"} #{:a "efg"} #{"abc" "efg"}})
;;
(fn kcomb
([k col]
(kcomb k col #{}))
([k col pret]
(if (empty? col)
(into #{} (filter (fn [e] (= (count e) k)) pret))
(recur k (rest col)
(reduce (fn [ret this]
(conj ret
(conj this (first col))))
(conj pret (hash-set (first col))) pret)))))
;; partial flatten sequence
;; always look at head, cons partial result to the ret value of recursive rest body to form tot solution.
;; no need to carry partial result during recursion
;;
;; (= (__ [[[[:a :b]]] [[:c :d]] [:e :f]])
;; [[:a :b] [:c :d] [:e :f]])
;; (= (__ '((1 2)((3 4)((((5 6))))))))
;; '((1 2)(3 4)(5 6))'
(fn myfltn
([col]
(myfltn col []))
([col init] ;; no need to carry partial result during recursion.
(if (and (coll? col)
(not (empty? col)))
(if (coll? (first col))
(concat (myfltn (first col)) (myfltn (rest col)))
(conj [] col) )))) ;; when first of col is not collection, one level nested. can ret.
;; decurry, accepts a curried fn of unknown arity n, ret an equivalent fn of n arguments
;;
(fn decurry [f]
(fn [ & xs]
(loop [ argv xs partFn f]
(if (= 1 (count argv))
(partFn (first argv))
(recur (next argv) (partFn (first argv)))))))
(= 10 ((__ (fn [a]
(fn [b]
(fn [c]
(fn [d]
(+ a b c d))))))
1 2 3 4))
;;
;; pascal triangle.
;; list transform, take the relationship between neighbor elements.
;; traditional map etc only take individual items.
;; create a new list by shifting the current list, then apply op on a list of vectors.
;;
(fn pascal
([n]
(if (= n 1)
[1]
(if (= n 2)
[1 1]
(let [xs (pascal (dec n)) ys (rest xs)]
(cons 1 (conj (vec (map + (drop-last xs) ys)) 1)))))))
;;
;; lazy search the smallest item that appears in all sorted sequence.
;;
(fn smallest
[& colv]
(let [veccols (vec colv)
hd (first (apply map vector colv))
minhd (apply min-key second (map-indexed vector hd))
smallestidx (first minhd)
smallestv (second minhd)
]
(if (= (count hd) (count (filter #(= % smallestv) hd)))
smallestv
(recur (concat (take smallestidx veccols)
(drop (inc smallestidx) veccols)
(vector (rest (veccols smallestidx))) )))))
(= 64 (__ (map #(* % % %) (range)) ;; perfect cubes
(filter #(zero? (bit-and % (dec %))) (range)) ;; powers of 2
(iterate inc 20))) ;; at least as large as 20
;; take a nest collection, and a sub collection of it that sum to certain number. maintain nested structure.
;; For/loop comprehents flatten list. Nested collection, need explict loop or reduce and carry partial result along.
;; for list comprehen
;;
(fn SequsHorribilis
([tot xs]
(sequs tot xs []))
([tot xs partResult] ;; xs must be a seq when calling.
(loop [ remain tot
xs xs
partResult partResult]
(if (empty? xs) ;; break out loop when empty list
partResult
(let [ hd (first xs)
body (rest xs)
t (type hd) ]
(if (or (= t clojure.lang.PersistentVector)
(= t clojure.lang.PersistentList))
;;
;; if header is a collection, call this fn recursively to get result for header,
;; and continue loop the rest of the list with the result from head conjed to partial result.
;;
(let [headrslt (sequs remain hd []) ;; call myself to get result for head collection.
headtot (apply + (flatten headrslt))]
(recur (- remain headtot) body (conj partResult headrslt))) ;; loop the rest with head's result conjed to partial result.
(if (>= remain hd)
(recur (- remain hd) body (conj partResult hd))
partResult)))))))
(= (__ 10 [1 2 [3 [4 5] 6] 7]) '(1 2 (3 (4))))
(= (__ 30 [1 2 [3 [4 [5 [6 [7 8]] 9]] 10] 11]) '(1 2 (3 (4 (5 (6 (7)))))))
(= (__ 9 (range)) '(0 1 2 3)
(= (__ 1 [[[[[1]]]]]) '(((((1))))))
(= (__ 0 [1 2 [3 [4 5] 6] 7]) '())
(= (__ 0 [0 0 [0 [0]]]) '(0 0 (0 (0))))
(= (__ 1 [-10 [1 [2 3 [4 5 [6 7 [8]]]]]]) '(-10 (1 (2 3 (4)))))
;;
;; Read Roman numerals with subtractive principle.
;; just cover the following condition: IV 4 IX 9 XL 40 XC 90 CD 400 CM 900
;; look ahead for each item, if matches one of the above, consume both.
(fn roman-num [numstr]
(let [vmap { "I" 1 "V" 5 "X" 10 "L" 50 "C" 100 "D" 500 "M" 1000 "IV" 4 "IX" 9 "XL" 40 "XC" 90 "CD" 400 "CM" 900 }]
(loop [numstr numstr tot 0]
(if (clojure.string/blank? numstr)
tot
(if (<= (count numstr) 1)
(+ tot (vmap (subs numstr 0 1)))
(let [hd (subs numstr 0 1) hdpair (subs numstr 0 2)]
(if (nil? (vmap hdpair))
(recur (subs numstr 1) (+ tot (vmap hd)))
(recur (subs numstr 2) (+ tot (vmap hdpair))))))))))
(= 3999 (__ "MMMCMXCIX"))
(= 827 (__ "DCCCXXVII"))
;;
;; lazy seq of pronunciations
;; lazy-seq : replace recursive with laziness.
;; wrap the recursive part of a function body with lazy-seq
;; to replace recursion with laziness.
;; recursive part of fn body : (cons this_result (recursive-call (next iteration)))
;;
(fn lazy-pron
([xs]
(lazy-pron xs nil []))
([xs prev result]
(letfn [(stepHd [xs prev result] ;; carry prev val to this iteration of head processing.
(if (empty? xs)
result
(if (= (first xs) prev)
(recur (rest xs) prev (conj (vec (drop-last 2 result)) (inc (first (take-last 2 result))) prev))
(recur (rest xs) (first xs) (conj result 1 (first xs)) ))))]
(let [curpron (stepHd xs prev result)]
(lazy-seq (cons curpron (lazy-pron curpron))))))) ;; wrap recursion body to lazy-seq
;; solution 2, recur loop, not recur stepHd fn itself.
(fn lazy-pron [xs]
(letfn [(stepHd [xs]
(loop [xs xs
prev nil ;; carry prev val to this iteration head processing.
result []]
(if (empty? xs)
result
(if (= (first xs) prev)
(recur (rest xs) prev (conj (vec (drop-last 2 result)) (inc (first (take-last 2 result))) prev))
(recur (rest xs) (first xs) (conj result 1 (first xs)))))))]
(let [curpron (stepHd xs)]
(lazy-seq (cons curpron (lazy-pron curpron))) )))
;;
;; Insert between two items, returns a new collection where the value is inserted between every two items
;; first, tranform to ary of each pair dup, map add predicate each pair, remove the prev tail to cur head dup.
;; The principle is used in count consecutive headers, or gen fib sequence.
(defn fibo [] (map first (iterate (fn [[a b]] [b (+ a b)]) [0N 1N])))
(fn [pred v coll]
(letfn [(take-pair [c] (if (next c) (take 2 c) c))
(by-pair [c]
(let [pair (seq (take-pair c))]
(when pair
(lazy-seq (cons pair (by-pair (rest c)))))))]
(let [ matched-pairs
(map (fn [e]
(if (pred (first e) (last e))
(vector (first e) v (last e))
e)) (by-pair coll)) ]
(reduce (fn [ret this] (apply conj ret (rest this))) (vec (first matched-pairs)) (rest matched-pairs)))))
(= [0 1 :x 2 :x 3 :x 4] (__ #(and (pos? %) (< % %2)) :x (range 5)))
;;
;; reduce not work for lazy seq, we need to lazy-seq con result from this iteration on top of the result of rest.
;; many ways for recursion, recur fn, recur loop, or lazy-seq con stepHd.
;; - recur loop, (loop [lst l curk nil partRslt {} ] (recur ...))
;; - fn self recursion with recur, (stepHd [xs prev partialResult] ... (recur ...)
;; - stepHd co-recursion. ret a seq formed by processing hd, recursion on the further data gened by head.
;; loop (let [curpron (stepHd xs)] (lazy-seq (cons curpron (lazy-seq (stepHd curpron)))) )))
;;
(fn intrapol
([pred v coll]
(if (or (empty? coll)
(< (count (take 2 coll)) 2))
coll
(intrapol pred v coll [])))
([pred v coll partRslt] ;; partRslt is not used here, this is recursive fn call.
(let [ hd (first coll)
nxthd (first (rest coll))]
(if (nil? nxthd)
(vector hd)
(if (pred hd nxthd)
(lazy-seq (cons hd (cons v (intrapol pred v (rest coll) partRslt)))) ;; cant use self recur call, as recur not in tail
(lazy-seq (cons hd (intrapol pred v (rest coll) partRslt))))))))
(= [0 1 :same 1 2 3 :same 5 8 13 :same 21]
(take 12 (->> [0 1]
(iterate (fn [[a b]] [b (+ a b)]))
(map first) ; fibonacci numbers
(__ (fn [a b] ; both even or both odd
(= (mod a 2) (mod b 2)))
:same))))
;;
;; take-while but stop only when n items satisfied, not whenever pred is false.
;;
(fn take-while-n [n pred seqns]
(let [hd (first seqns) bd (rest seqns) pred? (pred hd)]
(if (or (zero? n) ;; should check header.
(and (= 1 n)
pred?))
[] ;; ret empty seq at leaf so parent can conj its result on top of it to bottom up.
(if pred?
(lazy-seq (cons hd (take-while-n (dec n) pred bd)))
(lazy-seq (cons hd (take-while-n n pred bd)))))))
(= ["this" "is" "a" "sentence"]
(__ 3 #(some #{\i} %)
["this" "is" "a" "sentence" "i" "wrote"]))
;;
;; create a map such that each key in the map is a keyword, and the value is a sequence of all the numbers (if any)
;; between it and the next keyword in the sequence.
;;
(fn keyvals [ l ]
(loop [lst l curk nil partRslt {} ]
(let [hd (first lst) body (rest lst)]
(if (nil? hd)
partRslt
(if (= clojure.lang.Keyword (type hd))
(recur body hd (assoc partRslt hd []))
(recur body curk (update-in partRslt [curk] (fnil conj []) hd)))))))
(= {:a [1 2 3], :b [], :c [4]} (__ [:a 1 2 3 :b :c 4]))
;;
;; oscillating iterate: a function that takes an initial value and a variable number of functions.
;; stepHd rets a lazy seq gened by processing then head item. We then lazy-cons head onto it forms the ret lazy-seq.
;;
(fn oscilrate [v & fns ]
(let [ cycledfns (cycle fns) ]
(letfn [ (stepHd [v & fns]
(let [ hdfn (first fns)
nextv (hdfn v) ]
(lazy-seq (cons nextv (apply stepHd nextv (rest fns)))))) ]
(cons v (apply stepHd v cycledfns)))))
(= (take 12 (__ 0 inc dec inc dec inc)) [0 1 0 1 0 1 2 1 2 1 2 3])
;;
;; universal compute engine, take a prefix math form, and param map, compute the value.
;; compute form closure to wrap the form and val map. resolve symb closure recursively call computer form closure.
;;
(fn compute-engine [form]
(fn [vmap]
(letfn [(numb? [x]
(let [t (type x)]
(if (or (= java.lang.Integer t)
(= java.lang.Long t))
true
false)))
(symb? [x]
(let [t (type x)]
(if (= clojure.lang.Symbol t)
true
false)))
(compute-form [form vmap]
(letfn [ (symb-val [x] ;; a fn closure to resolve each symbol. recursively.
(if (numb? x)
x
(if (symb? x)
(vmap x)
(compute-form x vmap))))]
(let [op (first form)]
(condp = op
'* (apply * (map symb-val (rest form)))
'/ (apply / (map symb-val (rest form)))
'+ (apply + (map symb-val (rest form)))
'- (apply - (map symb-val (rest form)))))))]
(compute-form form vmap))))
(= [6 0 -4]
(map (__ '(* (+ 2 a)
(- 10 b)))
'[{a 1 b 8}
{b 5 a -2}
{a 2 b 11}]))
;;
;; levenshtein distance.
;; http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm
;;
(fn levenshtein [src tgt]
(let [srclen (count src) tgtlen (count tgt) rowsz (inc tgtlen)]
(if (= src tgt)
0
(if (or (= (count src) 0)
(= (count tgt) 0))
(max (count src) (count tgt))
(loop [srcidx 0 tgtidx 0 preRow (range 0 rowsz) curRow (conj [] (inc srcidx))] ;; curRow[0]=srcidx+1
(let [srclt (nth src srcidx)
tgtlt (nth tgt tgtidx)
nxtsrcidx (inc srcidx)
nxttgtidx (inc tgtidx)
leftv (nth preRow nxttgtidx)
leftupperv (nth preRow tgtidx)
upperv (nth curRow tgtidx)
cost (fn [slt dlt] (if (= slt dlt) 0 1))
mincurv (min (inc leftv) (inc upperv) (+ leftupperv (cost srclt tgtlt)))]
;; does cur row iteration done ?
;;(prn srclt tgtlt nxtsrcidx preRow curRow)
(if (= nxttgtidx tgtlen) ;; done one iteration of tgt row
(if (= nxtsrcidx srclen)
mincurv ;; the result is in last of cur-row after iterating all.
(recur nxtsrcidx 0 (conj curRow mincurv) (conj [] (inc nxtsrcidx)))) ;; next src letter
(recur srcidx nxttgtidx preRow (conj curRow mincurv)))))))))
(= (__ "ttttattttctg" "tcaaccctaccat") 10)
;;
;; word chain, get Levenshtein distance 1 neighors and TSP visit all nodes.
;; TSP, recu loop each children, self-recursion travelsman fn. one of them will succ or all will fail.
;; remove cur from map to avoid cycle. bottom up build the path from succ leaf.
(fn word-chain [wdset]
(letfn [(levenshtein [src tgt]
(let [srclen (count src) tgtlen (count tgt) rowsz (inc tgtlen)]
(if (= src tgt)
0
(loop [srcidx 0 tgtidx 0 preRow (range 0 rowsz) curRow (conj [] (inc srcidx))] ;; curRow[0]=srcidx+1
(let [srclt (nth src srcidx)
tgtlt (nth tgt tgtidx)
nxtsrcidx (inc srcidx)
nxttgtidx (inc tgtidx)
leftv (nth preRow nxttgtidx)
leftupperv (nth preRow tgtidx)
upperv (nth curRow tgtidx)
cost (fn [slt dlt] (if (= slt dlt) 0 1))
mincurv (min (inc leftv) (inc upperv) (+ leftupperv (cost srclt tgtlt)))]
;; does cur row iteration done ?
;;(prn srclt tgtlt nxtsrcidx preRow curRow)
(if (= nxttgtidx tgtlen) ;; done one iteration of tgt row
(if (= nxtsrcidx srclen)
mincurv ;; the result is in last of cur-row after iterating all.
(recur nxtsrcidx 0 (conj curRow mincurv) (conj [] (inc nxtsrcidx)))) ;; next src letter
(recur srcidx nxttgtidx preRow (conj curRow mincurv))))))))
(nbmap [wdset]
(reduce (fn [ret this]
(assoc ret this (filter #(= 1 (levenshtein this %)) wdset))) {} wdset))
;; This is travelling salesman problem. Not suitable for dfs or bfs.
(dfs [cur nbmap]
(loop [partmap nbmap stack [cur] discovered #{cur} partRslt []]
(if (= (count stack) (count (keys nbmap)))
stack
(if (empty? stack) ;; dfs stack from cur node done. return all explored nodes
nil
(let [topnod (peek stack)