All Your Base
Convert a number written as digits in one base into its digits in another.
Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.
// FILE: all_your_base.nomi
//# Converts a number, written as digits in one base, to its digits in another.
import std::int.Radix
/// Why a number cannot be rebased.
pub enum Error
InvalidInputBase
InvalidDigit
InvalidOutputBase
end
/// The digits, most significant first, of the number that `digits` writes
/// in `input_base`, written in `output_base`. Zero, and an empty list of
/// digits, come out as `[0]`.
pub fn rebase(input_base: Int, digits: List<Int>, output_base: Int) -> Result<List<Int>, Error>
Some(from) = Radix.try_from(input_base) else return Err(.InvalidInputBase)
Some(number) = Int.from_digits(digits, from) else return Err(.InvalidDigit)
Some(to) = Radix.try_from(output_base) else return Err(.InvalidOutputBase)
Ok(Int.to_digits(number, to))
end
// FILE: all_your_base_test.nomi
import
all_your_base
all_your_base.Error
end
test "rebases digits"
[
("single bit one to decimal", 2, [1], 10, Ok([1])),
("binary to single decimal", 2, [1, 0, 1], 10, Ok([5])),
("single decimal to binary", 10, [5], 2, Ok([1, 0, 1])),
("binary to multiple decimal", 2, [1, 0, 1, 0, 1, 0], 10, Ok([4, 2])),
("decimal to binary", 10, [4, 2], 2, Ok([1, 0, 1, 0, 1, 0])),
("trinary to hexadecimal", 3, [1, 1, 2, 0], 16, Ok([2, 10])),
("hexadecimal to trinary", 16, [2, 10], 3, Ok([1, 1, 2, 0])),
("15-bit integer", 97, [3, 46, 60], 73, Ok([6, 10, 45])),
("empty list", 2, [], 10, Ok([0])),
("single zero", 10, [0], 2, Ok([0])),
("multiple zeros", 10, [0, 0, 0], 2, Ok([0])),
("leading zeros", 7, [0, 6, 0], 10, Ok([4, 2])),
("input base is one", 1, [0], 10, Err(Error.InvalidInputBase)),
("input base is zero", 0, [], 10, Err(.InvalidInputBase)),
("input base is negative", -2, [1], 10, Err(.InvalidInputBase)),
("negative digit", 2, [1, -1, 1, 0, 1, 0], 10, Err(.InvalidDigit)),
("invalid positive digit", 2, [1, 2, 1, 0, 1, 0], 10, Err(.InvalidDigit)),
("output base is one", 2, [1, 0, 1, 0, 1, 0], 1, Err(.InvalidOutputBase)),
("output base is zero", 10, [7], 0, Err(.InvalidOutputBase)),
("output base is negative", 2, [1], -7, Err(.InvalidOutputBase)),
("both bases are negative", -2, [1], -7, Err(.InvalidInputBase)),
]
|> subtests (description, input_base, digits, output_base, expected), description
assert all_your_base.rebase(input_base, digits, output_base) == expected
end
endok rebases digits (21 cases)
test result: ok. 21 passed, 0 failed
How it’s written
Section titled “How it’s written”- Errors are an
Errorenum with one variant per canonical message. The checks run in the canonical order (input base, digits, output base), so “both bases are negative” reports the input base. - Each check is an early
return, which keeps the conversion unindented. - The digits become a number with
Int.from_digits(digits, input_base), whoseNonefor a digit outside the base is theInvalidDigiterror. It runs after the input base check, since a base below 2 traps there. - The number becomes digits with
Int.to_digits(number, output_base), which gives[0]for zero, so zero and an empty list need no early return. - The 21 cases are one
subteststable. The first error row writesErr(Error.InvalidInputBase)and the later onesErr(.InvalidDigit): a qualified variant in an earlier row gives the enum to the bare ones after it, so the table needs no type annotation. - Each base is checked with
Radix.try_from, whoseNoneis the base’s error: aRadixis the baseInt.from_digitsandInt.to_digitstake.