Skip to content

Instantly share code, notes, and snippets.

@Kuzcoo
Last active December 11, 2016 20:04
Show Gist options
  • Save Kuzcoo/8c05a73a2f7fb90f193821d938137267 to your computer and use it in GitHub Desktop.
Save Kuzcoo/8c05a73a2f7fb90f193821d938137267 to your computer and use it in GitHub Desktop.
(defn my-last [a]
"P01 - find the last box of a list"
((fn my-last-recur [b c]
(if (empty? c)
b
(my-last-recur (first c) (rest c))
))
(first a)
(rest a))
)
(defn my-last2 [a]
"P01 - find the last box of a list"
(if ((= 1 (count a)))
(first a)
(recur (rest a)))
)
(defn my-last3 [a]
"P01 - find the last box of a list"
(if (next a))
(first a)
(recur (rest a)))
)
(defn my-but-last [a]
"P02 - find the last but one box of a list"
(if (= 2 (count a))
a
(recur (rest a)))
)
(defn element-at [a n]
"P03 - find the K'th element of a list"
(if (zero? n)
(first a)
(recur (rest a) (dec n)))
)
(defn my-count [a]
"P04 - Find the number of elements of a list"
(loop [r a
n 0]
(if (empty? r)
n
(recur (rest r) (inc n))))
)
(defn my-count2 [a]
"P04 - Find the number of elements of a list"
(reduce (fn [a b] (inc a)) 0 a)
)
(defn my-reverse [a]
"P05 - Reverse a list"
(reduce #(cons %2 %1) '() a)
)
(defn palindrome? [a]
"P06 - Find out whether a list is a palindrome"
(= a (my-reverse a))
)
(defn my-flatten [a]
"P07 - Fatten a nested list structure"
(reduce (fn [acc b]
(if (seq? b)
(concat acc (my-flatten b))
(concat acc (list b)))
) '() a)
)
(defn my-compress [a]
"P08 - Eleminate consecutive duplicates of list elements"
(reduce
(fn [acc b]
(if (= (last acc) b)
acc
(concat acc (list b)) ))
'() a)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment