programming kata

1 minute read

Code kata

Problem The elevator can be on first floor or second floor The elevator can be either openned or closed. The elevator can go up or down. But when it goes up or down, the door has to be closed. The door can open or close, but it cannot open when it is already openned or close when it is already closed. Write a function that takes a list of actions with :done indicating the end, and return if this sequence is legal or not.

1 minute read

Code kata

Problem Implement a unit converter that uses the given the following metric (def metric {:meter 1 :km 1000 :cm 1/100 :mm [1/10 :cm]}) The converter should anwser questions like: How many meters are there in 10 km and 20 cm? Solution (defn convert [context descriptor] (reduce (fn [result [mag unit]] (let [val (metric unit)] (if (vector? val) (+ result (* mag (convert context val))) (+ result (* mag val))))) 0 (partition 2 descriptor))) (convert metric [1 :meter]) (convert metric [3 :km 10 :meter])

1 minute read

Code kata

Problem Here is a list of students' exam scores. Write a function to sort them based on given criteria. Like Math first, then Physics and then chemistry and then English. (def exam-scores [{:math 78 :physics 80 :english 97 :chemistry 65} {:math 78 :physics 80 :english 66 :chemistry 65} {:math 78 :physics 54 :english 97 :chemistry 65} {:math 78 :physics 80 :english 97 :chemistry 61} {:math 100 :physics 89 :english 47 :chemistry 85} {:math 98 :physics 80 :english 79 :chemistry 65}]) Solution (defn rank [scores & criteria] (reverse (sort-by (fn [score] (mapv score criteria)) scores))) Note The sort-by in Clojure is very powerful, the idea is to reduce each row into one value that we know how to sort, like numbers, strings or lists.