Ninety-Nine Problems In Clojure Part 1 1-15
The original Ninety-Nine Prolog Problems was written by Werner Hett at the Berne University of Applied Sciences in Berne, Switzerland. Phil Gold then adapted it to Ninety-Nine Scala Problems. I had fun reading them. It has inspired me to do a series of Ninety-Nine Problems in different languages. I’m starting with Clojure.
I’m taking a more practical approach. I use built-in library whenever possible. I use clojure-contrib as well. Because on a day to day basis, this is what we all do. They exist for a reason after all. If you feel that’s cheating. Feel free to write your own.
The difficulty ranking was for Prolog. The original text says
The problems have different levels of difficulty. Those marked with a single asterisk (*) are easy. If you have successfully solved the preceeding problems you should be able to solve them within a few (say 15) minutes. Problems marked with two asterisks (**) are of intermediate difficulty. If you are a skilled Proglog programmer it shouldn’t take you more than 30-90 minutes to solve them. Problems marked with three asterisks (***) are more difficult. You may need more time (i.e. a few hours or more) to find a good solution.
I think the difficulty scale for the most part is comparable in Clojure.
user=> (last [1 1 2 3 5 8]) 8
user=> (penultimate [1 1 2 3 5 8]) 5
user=> (nth2 2 [1 1 2 3 5 8]) 2
user=> (length [1 1 2 3 5 8]) 6
user=> (reverse [1 1 2 3 5 8]) (8 5 3 2 1 1)
user=> (palindrome? [1 2 3 2 1]) true
user=> (flatten [[1 1] 2 [3 [5 8]]]) (1 1 2 3 5 8 )
user=> (compress "aaaabccaadeeee") (\a \b \c \a \d \e)
user=> (pack "aaaabccaadeeee") ((\a \a \a \a) (\b) (\c \c) (\a \a) (\d) (\e \e \e \e))
(N, E) where N is the number of duplicates of the element E.Example:
user=> (encode "aaaabccaadeeee") ((4 \a) (1 \b) (2 \c) (2 \a) (1 \d) (4 \e))
(N, E) terms.Example:
user=> (encode-modified "aaaabccaadeeee") ((4 \a) (\b) (2 \c) (2 \a) (\d) (4 \e))
user=> (decode [[4 \a] [1 \b] [2 \c] [2 \a] [1 \d] [4 \e]]) (\a \a \a \a \b \c \c \a \a \d \e \e \e \e)
pack); do all the work directly.Example:
user=> (encode-direct "aaaabccaadeeee") ((4 \a) (\b) (2 \c) (2 \a) (\d) (4 \e))
user=> (duplicate "abccd") (\a \a \b \b \c \c \c \c \d \d)
user=> (duplicate-n 3 "abccd") (\a \a \a \b \b \b \c \c \c \c \c \c \d \d \d)


I am curious why you decided to use core functions for some (e.g. last, reverse) and wrap core functions for others (e.g. count)?
-m
[...] problems in #clojure part 1 (here, via @wmacgyver) — Attempting to solve the ninety-nine Prolog problems in #clojure Share [...]
[...] Here can be found an interesting effort to implement the 99 Prolog problems in Clojure. [...]
fogus: It was to keep in spirit of the exercise as much as possible. In case of length, the original Prolog 99 problem ask you to implement a “length” function. So I wrap it. But in case of reverse. The problem asked you to implement a “reverse” function, which clojure’s core function is in fact called reverse. So I just use it with the same name.
Makes sense. Thanks for the calrification.
-m