Commits

tednaleid committed 91fca52

more problems

  • Participants
  • Parent commits b919524

Comments (0)

Files changed (1)

File src/four_clojure/answered.clj

 (= (my-interleave [1 2 3 4] [5]) [1 5])
 (= (my-interleave [30 20] [25 15]) [30 25 20 15])
 
-; http://www.4clojure.com/problem/
-;
-;
+; http://www.4clojure.com/problem/40
+; Interpose a Seq
+; Write a function which separates the items of a sequence by an arbitrary value.
+; restriction: interpose  
 
+(defn my-interpose [sep s]
+                    (rest (interleave (repeat sep) s)))
+(= (my-interpose 0 [1 2 3]) [1 0 2 0 3])
+(= (apply str (my-interpose ", " ["one" "two" "three"])) "one, two, three")
+(= (my-interpose :z [:a :b :c :d]) [:a :z :b :z :c :z :d])
+
+
+; http://www.4clojure.com/problem/52
+; Intro to Destructuring
+; Let bindings and function parameter lists support destructuring.
+(= [2 4] (let [[a b c d e f g] (range)] [c e]))
+
+; http://www.4clojure.com/problem/31
+; Pack a Sequence
+; Write a function which packs consecutive duplicates into sub-lists.
+(defn pack [x]
+  (partition-by identity x))
+(= (pack [1 1 2 1 1 1 3 3]) '((1 1) (2) (1 1 1) (3 3)))
+(= (pack [:a :a :b :b :c]) '((:a :a) (:b :b) (:c)))
+(= (pack [[1 2] [1 2] [3 4]]) '(([1 2] [1 2]) ([3 4])))
+
+
+; http://www.4clojure.com/problem/41
+; Drop every nth item
+; Write a function which drops every Nth item from a sequence.
+
+(defn drop-nth [col n]
+  (mapcat #(take (dec n) %) (partition-all n col)))
+
+(= (drop-nth [1 2 3 4 5 6 7 8] 3) [1 2 4 5 7 8])
+(= (drop-nth [:a :b :c :d :e :f] 2) [:a :c :e])
+(= (drop-nth [1 2 3 4 5 6] 4) [1 2 3 5 6])
+
+; http://www.4clojure.com/problem/49
+; Split a sequence
+; Write a function which will split a sequence into two parts.
+; restrictions: split-at
+(defn my-split-at [n col]
+  [(take n col) (drop n col)])
+; or (juxt take drop)
+
+(= (my-split-at 3 [1 2 3 4 5 6]) [[1 2 3] [4 5 6]])
+(= (my-split-at 1 [:a :b :c :d]) [[:a] [:b :c :d]])
+(= (my-split-at 2 [[1 2] [3 4] [5 6]]) [[[1 2] [3 4]] [[5 6]]])
+
+; http://www.4clojure.com/problem/51
+; Advanced Destructuring
+; Here's an example of some more sohpisticated 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]))
+
+
+; http://www.4clojure.com/problem/83
+; A Half-Truth
+; 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.
+
+(defn half-truth [& s]
+  (not (or (every? true? s) (every? false? s))))
+; or not=
+
+(= false (half-truth false false))
+(= true (half-truth true false))
+(= false (half-truth true))
+(= true (half-truth false true false))
+(= false (half-truth true true true))
+(= true (half-truth true true true false))
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
 
 ; http://www.4clojure.com/problem/
 ;
 ;
 
+; http://www.4clojure.com/problem/
+;
+;
+
+