Created
March 23, 2012 12:35
-
-
Save jshiell/2170296 to your computer and use it in GitHub Desktop.
Clojure Exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; http://aperiodic.net/phil/scala/s-99/ | |
; P01 - find the last element of a list | |
(defn p01 [the-list] | |
(if (empty? (rest the-list)) | |
(first the-list) | |
(recur (rest the-list)) | |
) | |
) | |
; P02 - find the last element but one of a list | |
(defn p02 [the-list] | |
(if (empty? (rest (rest the-list))) | |
(first the-list) | |
(recur (rest the-list)) | |
) | |
) | |
; P03 - find the kth element of a list | |
(defn p03 [k the-list] | |
(loop [acc 0 | |
remainder the-list] | |
(if (= acc k) | |
(first remainder) | |
(recur (inc acc) (rest remainder)) | |
) | |
) | |
) | |
; P04 - find the number of elements in a list | |
(defn p04 [the-list] | |
(loop [acc 0 | |
l the-list] | |
(if (empty? l) | |
acc | |
(recur (inc acc) (rest l)) | |
) | |
) | |
) | |
; P05 - reverse a list | |
(defn p05 [the-list] | |
(loop [l the-list | |
r []] | |
(if (empty? l) | |
r | |
(recur (rest l) (concat [(first l)] r)) | |
) | |
) | |
) | |
; P06 - find out whether a list is a palindrome | |
(defn p06 [l] | |
(if (< (count l) 2) | |
true | |
(if (not (= (first l) (last l))) | |
false | |
(recur (take (- (count l) 2) (rest l))) | |
) | |
) | |
) | |
; P07 - Flatten a nested list structure | |
(defn p07 [l] | |
(loop [f [] | |
c (first l) | |
r (rest l)] | |
(let [filtered (if (or (seq? c) (vector? c)) (p07 c) (list c))] | |
(if (empty? r) | |
(concat f filtered r) | |
(recur (concat f filtered) (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P08 - Eliminate consecutive duplicates of list elements | |
(defn p08 [l] | |
(loop [f [] | |
c (first l) | |
r (rest l)] | |
(let [pf (if (= (last f) c) f (concat f (list c)))] | |
(if (empty? r) | |
pf | |
(recur pf (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P09 - If a list contains repeated elements they should be placed in separate sub-lists | |
(defn p09 [l] | |
(loop [f [] | |
cl [] | |
c (first l) | |
r (rest l)] | |
(let [pf (if (= (first cl) c) f (if (< (count cl) 2) (concat f cl) (concat f (list cl)))) | |
pcl (if (nil? c) (list) (if (= (first cl) c) (concat cl (list c)) (list c)))] | |
(if (empty? r) | |
(concat pf (if (< (count pcl) 2) pcl (list pcl))) | |
(recur pf pcl (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P10 - RLE of a list | |
(defn p10-rle [l] | |
(if (empty? l) | |
l | |
(list (list (count l) (first l)))) | |
) | |
(defn p10 [l] | |
(loop [f [] | |
cl [] | |
c (first l) | |
r (rest l)] | |
(let [pf (if (= (first cl) c) f (concat f (p10-rle cl))) | |
pcl (if (nil? c) (list) (if (= (first cl) c) (concat cl (list c)) (list c)))] | |
(if (empty? r) | |
(concat pf (p10-rle pcl)) | |
(recur pf pcl (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P11 Modified RLE (optimised single elements) | |
(defn p11-rle [l] | |
(if (< (count l) 2) | |
l | |
(list (list (count l) (first l)))) | |
) | |
(defn p11 [l] | |
(loop [f [] | |
cl [] | |
c (first l) | |
r (rest l)] | |
(let [pf (if (= (first cl) c) f (concat f (p11-rle cl))) | |
pcl (if (nil? c) (list) (if (= (first cl) c) (concat cl (list c)) (list c)))] | |
(if (empty? r) | |
(concat pf (p11-rle pcl)) | |
(recur pf pcl (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P12 Decode RLE | |
(defn p12 [l] | |
(loop [f [] | |
c (first l) | |
r (rest l)] | |
(let [pf (if (or (seq? c) (vector? c)) (concat f (repeat (first c) (last c))) (if (nil? c) f (concat f (list c))))] | |
(if (empty? r) | |
pf | |
(recur pf (first r) (rest r)) | |
) | |
) | |
) | |
) | |
; P13 - Standalone RLE (done as P11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment