Last active
August 26, 2019 20:18
-
-
Save saskali/545bb3d5ab2b91ac86ee71330697d80f to your computer and use it in GitHub Desktop.
Balance of N - kata @socrates 2019
This file contains hidden or 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
(ns balance-of-n.core-test | |
(:require [clojure.test :refer :all])) | |
(defn number->digits | |
([number] (number->digits number [])) | |
([number result] | |
(if (< number 10) | |
(-> result (conj number) vec) | |
(number->digits (quot number 10) | |
(cons (mod number 10) result))))) | |
(defn sum-up-digits [digits] | |
(->> (quot (count digits) 2) | |
(subvec digits 0) | |
(apply +))) | |
(defn balance-of-n [number] | |
(let [test-vec (number->digits number)] | |
(= (sum-up-digits test-vec) | |
(sum-up-digits (-> test-vec reverse vec))))) | |
(deftest number->digits-should | |
(testing "convert a number into its digits" | |
(is (= [0] (number->digits 0))) | |
(is (= [5] (number->digits 5))) | |
(is (= [9] (number->digits 9))) | |
(is (= [1 0] (number->digits 10))) | |
(is (= [8 4] (number->digits 84))) | |
(is (= [2 3 4] (number->digits 234))) | |
(is (= [9 8 7] (number->digits 987))))) | |
(deftest balance-of-n-should | |
(testing "be true for numbers whose component digits have the | |
same sum on the left and right halves of the number" | |
(is (true? (balance-of-n 11))) | |
(is (true? (balance-of-n 121))) | |
(is (false? (balance-of-n 123))) | |
(is (false? (balance-of-n 88099))) | |
(is (true? (balance-of-n 89098))) | |
(is (true? (balance-of-n 89089))) | |
(is (= (take 20 (filter balance-of-n (range))) | |
[0 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment