Skip to content

Instantly share code, notes, and snippets.

@yifu
Created August 31, 2012 13:44
Show Gist options
  • Save yifu/3552823 to your computer and use it in GitHub Desktop.
Save yifu/3552823 to your computer and use it in GitHub Desktop.
Four clojure pbs.
(ns mynamespace)
;; SEQUENCE REVERSE.
(def __ (fn seq-reverse [v]
(if (empty? v)
(empty v)
(conj (seq-reverse (butlast v)) (last v)))))
(def __ (fn seq-reverse [[a & rest]]
(if (empty? rest)
[a]
(conj (seq-reverse rest) a))))
(def myfunc (fn [[a & rest]]
(prn a "|" rest)))
( myfunc [5 6 7 8])
(and
(= (__ [1 2 3 4 5]) [5 4 3 2 1])
(= (__ (sorted-set 5 7 2 7)) '(7 5 2))
(= (__ (sorted-set 5 7 2 7)) '(7 5 2)))
;; CONTAIN YOURSELF. PROBLEM 47.
(doc contains?)
(contains? #{4 5 6} 7)
(contains? [1 1 1 1 1] 4)
(contains? {4 :a 2 :b} 4)
(not (contains? '(1 2 4) 4))
;; INTRO TO ITERATE. PROBLEM 45.
(and
(= '(1 4 7 10 13) (take 5 (iterate #(+ 3 %) 1)) ))
;; FACTORIAL FUN. PROBLEM 42.
(doc def)
(doc nth)
;;(def __ (fn [i] (first (nth (iterate (fn [[a b]] [b (+ a b)]) [1 1] ) i ))))
;;(take 5(iterate (fn [[a b]] [b (+ a b)]) [1 1]))
;;(nth (iterate (fn [[a b]] [b (+ a b)]) [1 1]) 3)
(def __ (fn [i] (apply * (range 1 (inc i)))))
(def __ #(* % (apply * (range 1 %))))
(and
(= (__ 0) 1)
(= (__ 1) 1)
(= (__ 3) 6)
(= (__ 5) 120)
(= (__ 8) 40320))
;; INTRO TO DESTRUCTURING. PROBLEM 52.
(let [[a b c d e] (range)] [c e]) ;; v)
;; DROP EVERY NTH ITEM. PROBLEM 41.
(comment (def __ (fn [v n] ;; (for [[a b c] v] [] ;;(prn a "|" b "|" c "|" n) ))
(for [x v
i (cycle [1 2 3]) :when (not= i 3)]
x
))))
(drop 3 (range 1 10))
(take 20 (cycle [1 2 3]))
(doc not)
(drop)
(def __ (fn [v n]
(keep-indexed #(if (= 0 (mod (inc %) n)) nil %2) v)))
(mod 3 3)
(keep-indexed (fn [i item] (if (= 0 (mod i 3)) nil item)) '(1 2 3 4 5 6 7 8 9 10) )
(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]))
;; SPLIT A SEQUENCE. PROBLEM 49.
( (def __ (fn [n v] [(take n v) (drop n v)])) 3 [1 2 3 4 5 6])
(take 3 [1 2 3 4 5 6])
(drop 3 [1 2 3 4 5 6])
(doc take)
(doc drop)
['( 1 2 3 ) '(4 5 6 )]
(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]]]))
;; ADVANCED DESTRUCTURING. PROBLEM 51.
(def __ (fn [k v] (apply assoc {} (interleave k v))))
(def __ #(apply assoc {} (interleave % %2)))
(into {} '(:a 1 :b 2 :c 3))
(into {} [[1 2] [3 4]])
(= (__ [:a :b :c] [1 2 3]) {:a 1, :b 2, :c 3})
(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"}))
;; GREATEST COMMON DIVISOR. PROBLEM 66.
(def __ (fn pgcd [x y]
(if (zero? y)
x
(pgcd y (mod x y)))))
(and
(= (__ 2 4) 2)
(= (__ 10 5) 5)
(= (__ 5 7) 1)
(= (__ 1023 858) 33))
;; SET INTERSECTION. PROBLEM 81.
(require 'clojure.set )
(def __ (fn [s1 s2]
(clojure.set/difference
(clojure.set/union s1 s2)
(clojure.set/difference s1 s2)
(clojure.set/difference s2 s1))))
(and
(= (__ #{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}))
;; RE-IMPLEMENT ITERATE. PROBLEM 62.
(def __ (fn iter [f x]
(lazy-seq
(cons x (iter f (f x))))))
(source take)
(source lazy-seq)
(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]))) )
;; PRODUCT DIGITS. PROBLEM 99.
(defn digits-of [x]
(if (zero? x)
[]
(conj
(digits-of (quot x 10))
(mod x 10))))
(quot 0 10 )
(def __ #((fn digits-of [x]
(if (zero? x)
[]
(conj
(digits-of (quot x 10))
(mod x 10)))) (* % %2)))
(and
(= (__ 1 1) [1])
(= (__ 99 9) [8 9 1])
(= (__ 999 99) [9 8 9 0 1]))
;; SIMPLE CLOSURES. PROBLEM 107.
(def __ (fn [n] #(apply * (repeat n %))))
(doc repeat)
(and
(= 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])))
;; CARTESIAN PRODUCT. PROBLEM 90.
(def __ (fn [s1 s2]
(set(for [x s1 y s2] [x y]))))
(and
(= (__ #{"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))))))
;; GROUP A SEQUENCE. PROBLEM 63.
(def __ (fn seq-group-by [f s]
(apply merge-with concat (map #(assoc {} (f %) [%]) 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]]}))
;; RE-IMPLEMENT MAP. PROBLEM 118.
(comment (def __ (fn map [f [elt & rest]]
(if (empty? rest)
(list (f elt))
(conj (map f rest) (f elt))))) )
(def __ (fn m [op v]
(if (empty? v)
v
(lazy-seq (cons (op (first v)) (m op (rest v)))))) )
(doc conj)
(doc cons)
(__ inc [2 3 4 5 6])
(__ (fn [_] nil) (range 10))
(repeat 10 nil)
(->> (__ inc (range)) (drop (dec 1000000)) (take 2))
(and (= [3 4 5 6 7] (__ inc [2 3 4 5 6]))
(= (repeat 10 nil) (__ (fn [_] nil) (range 10)))
(= [1000000 1000001] (->> (__ inc (range)) (drop (dec 1000000)) (take 2))))
(doc filter)
;; SUM OF SQUARE OF DIGITS. PROBLEM 120.
(defn list-of-digits [x]
( let [list-of-digits-aux (fn list-of-digits-aux [x] (if (zero? x) '() (cons (mod x 10) (list-of-digits-aux (quot x 10)))))]
(if (zero? x) '(0) (list-of-digits-aux x))))
(list-of-digits 0)
(list-of-digits 100)
(list-of-digits 1234657890)
(list-of-digits 100001)
(list-of-digits 456854)
(list-of-digits 456854000534354)
(list-of-digits 45685400053435400)
(defn square [x] (* x x))
(map square (range 100))
(defn sum-of-square-digits [x]
(apply + (map square (list-of-digits x))))
(def __ (fn [v]
(count (filter #(< % (sum-of-square-digits %)) v ))))
(range 10)
(__ (range 10))
(__ (range 30))
(list-of-digits 11)
(sum-of-square-digits 11)
(count (__ (range 30)))
(def __
(fn [v]
(let [ list-of-digits (fn list-of-digits [x] ( let [list-of-digits-aux (fn list-of-digits-aux [x] (if (zero? x) '() (cons (mod x 10) (list-of-digits-aux (quot x 10)))))] (if (zero? x) '(0) (list-of-digits-aux x))))
square (fn square [x] (* x x))
sum-of-square-digits (fn sum-of-square-digits [x] (apply + (map square (list-of-digits x))))]
(count (filter #(< % (sum-of-square-digits %)) v )))))
(and (= 8 (__ (range 10)))
(= 19 (__ (range 30)))
(= 50 (__ (range 100)))
(= 50 (__ (range 1000))))
;; RECOGNIZE PLAYING CARDS. PROBLEM 128.
(defn get-suit [a] (cond (= a \D) :diamond (= a \H) :heart (= a \C) :club (= a \S) :spades))
(defn get-rank-figures-case [b] (- (int b) 50))
(defn get-rank [b] (cond (= b \A) 12 (= b \K) 11 (= b \Q) 10 (= b \J) 9 (= b \T) 8 :else (get-rank-figures-case b) ))
(def __ (fn [[a b]] {:suit (get-suit a) :rank (get-rank b)} ))
(__ "DQ")
(get-rank-figures-case \2)
(and (= {:suit :diamond :rank 10} (__ "DQ"))
(= {:suit :heart :rank 3} (__ "H5"))
(= {:suit :club :rank 12} (__ "CA"))
(= (range 13) (map (comp :rank __ str)
'[S2 S3 S4 S5 S6 S7
S8 S9 ST SJ SQ SK SA])))
(def __ (fn [[a b]]
(let [get-suit (fn get-suit [a] (cond (= a \D) :diamond (= a \H) :heart (= a \C) :club (= a \S) :spades))
get-rank-figures-case (fn get-rank-figures-case [b] (- (int b) 50))
get-rank (fn get-rank [b] (cond (= b \A) 12 (= b \K) 11 (= b \Q) 10 (= b \J) 9 (= b \T) 8 :else (get-rank-figures-case b) ))]
{:suit (get-suit a) :rank (get-rank b)} )))
;; BEAUTY IS SYMMETRY. PROBLEM 96.
(fn tree? [node]
(let [third #(first (rest (rest %)))]
(if (nil? node)
true
(if (and (coll? node) (= (count node) 3))
(and (tree? (second node)) (tree? (third node)))
false))))
(doc nil?)
(defn value [node] (first node))
(defn l-child [node] (first (rest node)))
(defn r-child [node] (first (rest (rest node))))
(value '(:a nil nil))
(l-child '(:a nil nil))
(r-child '(:a nil nil))
(value '(:a :b :c))
(l-child '(:a :b :c))
(r-child '(:a :b :c))
(value '(:a (1 2) (3 4)))
(l-child '(:a (1 2) (3 4)))
(r-child '(:a (1 2) (3 4)))
(defn equal? [n1 n2]
(cond
(and (nil? n1) (nil? n2)) true
(and (coll? n1) (coll? n2) (= (val n1) (val n2)) (equal? (r-child n1) (r-child n2)) (equal? (l-child n1) (l-child n2))) true
:else false))
(= :a :a)
(equal? nil nil )
(equal? nil nil )
(equal? '(:q nil nil) '(:q nil nil))
(equal? '(:q nil nil) '(:z nil nil))
(equal? '(:q (4 nil nil) nil) '(:q (4 nil nil) nil))
(defn mirror? [node])
(defn symmetric? [node]
(cond (nil? node) true
(coll? node) (equal? node (mirror node))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment