Skip to content

Commit

Permalink
Merge pull request #137 from haus/add_all-your-base_exercise
Browse files Browse the repository at this point in the history
Add all-your-base exercise
  • Loading branch information
yurrriq committed Sep 26, 2016
2 parents 9b399dc + bc83cec commit 2568ad3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"binary-search-tree",
"difference-of-squares",
"hexadecimal",
"all-your-base",
"largest-series-product",
"pov",
"bracket-push",
Expand Down
4 changes: 4 additions & 0 deletions exercises/all-your-base/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(defproject all-your-base "0.1.0-SNAPSHOT"
:description "all-your-base exercise."
:url "https://github.com/exercism/xclojure/tree/master/exercises/all-your-base"
:dependencies [[org.clojure/clojure "1.8.0"]])
31 changes: 31 additions & 0 deletions exercises/all-your-base/src/example.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns all-your-base)

(defn- digits->decimal
"Converts a sequence of digits given in input-base into decimal format."
[input-base digits]
(loop [sum 0
[num & nums] digits]
(if num
(recur (+ (* sum input-base) num) nums)
sum)))

(defn- decimal->digits
"Converts a decimal number into a sequence of digits in the desired output base."
[output-base number]
(loop [digits nil
num number]
(if (zero? num)
digits
(recur (conj digits (mod num output-base)) (quot num output-base)))))

(defn convert
"Converts a sequence of digits given in input-base into a sequence of digits in the desired output-base."
[input-base digits output-base]
(cond
(some #(< % 2) (list input-base output-base)) nil
(not-every? #(and (not (neg? %)) (< % input-base)) digits) nil
(empty? digits) ()
(every? #(zero? %) digits) '(0)
:else (->> digits
(digits->decimal input-base)
(decimal->digits output-base))))
87 changes: 87 additions & 0 deletions exercises/all-your-base/test/all_your_base_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
(ns all-your-base-test
(require [clojure.test :refer [deftest testing is]]
[all-your-base]))

(deftest test-single-bit-to-one-decimal
(testing "Base 2 '1' converts to base 10 '1'"
(is (= '(1) (all-your-base/convert 2 '(1) 10)))))

(deftest test-binary-to-single-decimal
(testing "Base 2 '1 0 1' converts to base 10 '5'"
(is (= '(5) (all-your-base/convert 2 '(1 0 1) 10)))))

(deftest test-single-decimal-to-binary
(testing "Base 10 '5' converts to base 2 '1 0 1'"
(is (= '(1 0 1) (all-your-base/convert 10 '(5) 2)))))

(deftest test-binary-to-multiple-decimal
(testing "Base 2 '1 0 1 0 1 0' converts to base 10 '4 2'"
(is (= '(4 2) (all-your-base/convert 2 '(1 0 1 0 1 0) 10)))))

(deftest test-decimal-to-binary
(testing "Base 10 '4 2' conves to base 2 '1 0 1 0 1 0'"
(is (= '(1 0 1 0 1 0) (all-your-base/convert 10 '(4 2) 2)))))

(deftest test-trinary-to-hexadecimal
(testing "Base 3 '1 1 2 0' converts to base 16 '2 10'"
(is (= '(2 10) (all-your-base/convert 3 '(1 1 2 0) 16)))))

(deftest test-hexadecimal-to-trinary
(testing "Base 16 '2 10' converts to base 3 '1 1 2 0'"
(is (= '(1 1 2 0) (all-your-base/convert 16 '(2 10) 3)))))

(deftest test-15-bit-integer
(testing "Base 97 '3 46 60' converts to base 73 '6 10 45'"
(is (= '(6 10 45) (all-your-base/convert 97 '(3 46 60) 73)))))

(deftest test-empty-list
(testing "Empty input digits returns empty sequence"
(is (empty? (all-your-base/convert 2 () 10)))))

(deftest test-single-zero
(testing "0 converts to 0, no matter the base"
(is (= '(0) (all-your-base/convert 10 '(0) 2)))))

(deftest test-multiple-zeroes
(testing "0 converts to 0, no matter the how many zeroes"
(is (= '(0) (all-your-base/convert 10 '(0 0 0) 2)))))

(deftest test-leading-zeros
(testing "Leading zeroes don't affect conversion"
(is (= '(4 2) (all-your-base/convert 7 '(0 6 0) 10)))))

(deftest test-negative-digit
(testing "Negative digits result in nil"
(is (nil? (all-your-base/convert 2 '(1 -1 1 0 1 0) 10)))))

(deftest test-invalid-positive-digit
(testing "Invalid digits return nil"
(is (nil? (all-your-base/convert 2 '(1 2 1 0 1 0) 10)))))

(deftest test-first-base-is-one
(testing "Input base of 1 returns nil"
(is (nil? (all-your-base/convert 1 () 10)))))

(deftest test-second-base-is-one
(testing "Output base of 1 returns nil"
(is (nil? (all-your-base/convert 2 '(1 0 1 0 1 0) 1)))))

(deftest test-first-base-is-zero
(testing "Input base of 0 returns nil"
(is (nil? (all-your-base/convert 0 () 10)))))

(deftest test-second-base-is-zero
(testing "Output base of 0 returns nil"
(is (nil? (all-your-base/convert 10 '(7) 0)))))

(deftest test-first-base-is-negative
(testing "Negative input base returns nil"
(is (nil? (all-your-base/convert -2 '(1) 10)))))

(deftest test-second-base-is-negative
(testing "Negative output base returns nil"
(is (nil? (all-your-base/convert 2 '(1) -7)))))

(deftest test-both-bases-are-negative
(testing "When both bases are negative, nil is returned"
(is (nil? (all-your-base/convert -2 '(1) -7)))))

0 comments on commit 2568ad3

Please sign in to comment.