I am trying to implement the Havel-Hakimi algorithm using Clojure.
The steps in the algorithm are: 1. Remove zeros and reverse sort a list of values from highest to lowest. 2. Check if the list of values is empty, in which case exit and return a value of true. 3. If not empty, check to see if the first value in the list is greater than the length of the rest of the list. If it is, return false, else remove the first entry in the list and repeat the process.
The following is my implementation in code.
(defn hh [lst]
(if-let [lst (empty? (remove #{0} (reverse (sort lst))))]
true
(if (> (first lst) (count (rest lst)))
false
(hh (map dec (rest lst))))))
The following are my test cases.
(deftest hh-test
(is (= true (hh [])))
(is (= false (hh [1])))
(is (= true (hh [1 1])))
(is (= false (hh [2 2])))
(is (= false (hh [2 2 0]))) ;;failing and i don't know why
(is (= false (hh [3 2 1])))
(is (= true (hh [3 1 2 3 1])))
(is (= false (hh [5 3 0 2 6 2 0 7 2 5])))) ;;also failing and I don't know why
The fifth test case is particularly troubling me because it should be equivalent to the previous test case (the zero should get immediately dropped from the list and would then be equivalent to the previous test case, but for some reason it isn't).
I'm still new and learning Clojure (and programming in general) and I haven't been able to get debugging in Cider working (I get some strange errors when trying to debug) so I'm a bit stuck. Any help is appreciated.