Skip to content

Instantly share code, notes, and snippets.

@hickst
Created February 8, 2010 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hickst/297912 to your computer and use it in GitHub Desktop.
Save hickst/297912 to your computer and use it in GitHub Desktop.
;; P01
(last [1 1 2 3 5 8])
;; P02
(defn penultimate [x]
(last (butlast x)) )
;; P03
(nth [1 1 2 3 5 8] 2)
;; P04
(count [1 1 2 3 5 8])
;; P05
(reverse [1 1 2 3 5 8])
;; P06
(defn palindrome? [x]
(= x (reverse x)) )
;; P07
(flatten [[1 1] 2 [3 [5 8]]])
;; P08
(defn compress [x]
(map first (partition-by identity x)) )
;; P09
(defn pack [x]
(partition-by identity x) )
;; P10
(defn encode [x]
(map #(list (count %) (first %))
(pack x)) )
;; P11
(defn encode-modified [x]
(map #(if (<= (count %) 1) % (list (count %) (first %)))
(pack x)) )
;; P12
(defn decode [x]
(mapcat #(repeat (first %) (second %)) x) )
;; P13
;; Not sure what this problem is trying to specify
;; P14
(defn duplicate [x]
(interleave x x) )
;; P15
(defn duplicate-n [n x]
(mapcat #(repeat n %) x) )
;; P16
(defn drop-nth [n coll]
(lazy-seq
(when-let [xs (seq coll)]
(concat (take (dec n) xs) (drop-nth n (drop n xs)))
)
)
)
;; P17
(split-at 3 "abcdefghij")
;; P19
(defn rotate [n coll]
(let [r (if (not (neg? n)) n (+ n (count coll)))]
(concat (drop r coll) (take r coll)) ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment