the answer of 4clojure 101--

102.intoCamelCase

(fn name [s]
  (let [words (re-seq #"[a-zA-Z]+" s)
        words (cons (first words)
                    (map clojure.string/capitalize (rest words)))]
    (apply str words)))
;; best answer
#(clojure.string/replace % #"-(\w)" (fn [[a b]] (clojure.string/capitalize b)))

107.Simple closures

(fn [y]
  (fn [x] 
    (apply * (repeat y x)))
  )

118.Re-implement Map

(fn mymap [f coll]
  (if (false? (empty? coll))
        (lazy-seq
          (cons (f (first coll)) (mymap f (rest coll))))))

120.Sum of square of digits

(fn cnt-sqrt [arg]
  (let [get-digits (fn [n]
                     (map #(Integer/valueOf (str %)) (String/valueOf n)))
        digits-sqr (fn [n]
                     (apply + (map #(* % %) (get-digits n))))
        res-seq (filter #(< % (digits-sqr %)) arg)]
    (count res-seq)))

122.Read a binary number

#(Integer/valueOf % 2)

126.Through the Looking Class

Class

128. Recognize Playing Cards

(fn poke [s]
  (let [suit-map {\H :heart, \C :club, \D :diamond, \S :spades}
        rank-map {\2 0, \3 1, \4 2, \5 3, \6 4, \7 5,
                  \8 6, \9 7, \T 8, \J 9, \Q 10, \K 11, \A 12}
        ]
    {:suit (suit-map (first s)), :rank (rank-map (last s))}
  ))

134.A nil key

#(and (contains? %2 %1) (nil? (%2 %1)) )  


;; #(nil?(%2 %1 0)) seems better

135.Infix Calculator

(fn infix [& args]
  (reduce (fn [a [op b]] (op a b))
          (first args)
          (partition 2 (rest args))))

143.dot product

(fn [a b]
   (apply + (map * a b)))

145.For the win

'(1 5 9 13 17 21 25 29 33 37)

146. Trees into tables

(fn [mp]
  (into {}
        (for [[k v] mp
        [vk vv] v]
    (vec [[k vk] vv]))))

147. Pascal's Trapezoid

(fn pas-seq [a]
  (let [pas (fn [a]
            (let [a1 (concat [0] a)
                  a2 (concat a [0])
                  b (map + a1 a2)]
                  b))]
  (lazy-seq
    (concat [a] (pas-seq (pas a) )))))
;; or

 (fn [v]
  (iterate #(vec (map + (cons 0 %) (conj % 0))) v))

153.Pairwise Disjoint Sets.clj

(fn [coll]
  (let [ss (for [a coll b coll
                 :when (not (identical? a b))]
             (clojure.set/intersection a b))]
    (every? empty? ss)))
;; best answer
#(apply distinct? (mapcat seq %))

156.Map Defaults

(fn [defval init-keys]
  (loop [res {}, key init-keys]
    (if (empty? key)
      res
      (recur 
        (assoc res (first key) defval)
        (rest key))
      )))


;; or
 
#(zipmap %2 (repeat %1))

157. Indexing Sequences

(fn [v]
  (map #(vector %1 %2) v (range )))
;;or
#(map vector % (range))

161. Subset and Superset
#{2 1 3}

162.Logical falsity and truth
1
;; In Clojure, only nil and false representing the values of logical falsity in conditional tests - anything else is logical truth.

166. Comparisons
(fn [op a b]
  (cond
    (op a b) :lt
    (op b a) :gt
    :else :eq))

173. Intro to Destructuring 2
op arg


你可能感兴趣的:(the answer of 4clojure 101--)