ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# r36's solution

## to All Your Base in the Erlang Track

Published at Aug 03 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Convert a number, represented as a sequence of digits in one base, to any other base.

Implement general base conversion. Given a number in base a, represented as a sequence of digits, convert it to base b.

## Note

• Try to implement the conversion yourself. Do not use something else to perform the conversion for you.

## About Positional Notation

In positional notation, a number in base b can be understood as a linear combination of powers of b.

The number 42, in base 10, means:

(4 * 10^1) + (2 * 10^0)

The number 101010, in base 2, means:

(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)

The number 1120, in base 3, means:

(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)

I think you got the idea!

Yes. Those three numbers above are exactly the same. Congratulations!

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### all_your_base_tests.erl

``````%% Based on canonical data version 2.3.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/all-your-base/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(all_your_base_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_single_bit_one_to_decimal_test'() ->
?assertMatch({ok, [1]},
all_your_base:rebase([1], 2, 10)).

'2_binary_to_single_decimal_test'() ->
?assertMatch({ok, [5]},
all_your_base:rebase([1, 0, 1], 2, 10)).

'3_single_decimal_to_binary_test'() ->
?assertMatch({ok, [1, 0, 1]},
all_your_base:rebase([5], 10, 2)).

'4_binary_to_multiple_decimal_test'() ->
?assertMatch({ok, [4, 2]},
all_your_base:rebase([1, 0, 1, 0, 1, 0], 2, 10)).

'5_decimal_to_binary_test'() ->
?assertMatch({ok, [1, 0, 1, 0, 1, 0]},
all_your_base:rebase([4, 2], 10, 2)).

?assertMatch({ok, [2, 10]},
all_your_base:rebase([1, 1, 2, 0], 3, 16)).

?assertMatch({ok, [1, 1, 2, 0]},
all_your_base:rebase([2, 10], 16, 3)).

'8_15_bit_integer_test'() ->
?assertMatch({ok, [6, 10, 45]},
all_your_base:rebase([3, 46, 60], 97, 73)).

'9_empty_list_test'() ->
?assertMatch({ok, [0]},
all_your_base:rebase([], 2, 10)).

'10_single_zero_test'() ->
?assertMatch({ok, [0]},
all_your_base:rebase([0], 10, 2)).

'11_multiple_zeros_test'() ->
?assertMatch({ok, [0]},
all_your_base:rebase([0, 0, 0], 10, 2)).

?assertMatch({ok, [4, 2]},
all_your_base:rebase([0, 6, 0], 7, 10)).

'13_input_base_is_one_test'() ->
?assertMatch({error, "input base must be >= 2"},
all_your_base:rebase([0], 1, 10)).

'14_input_base_is_zero_test'() ->
?assertMatch({error, "input base must be >= 2"},
all_your_base:rebase([], 0, 10)).

'15_input_base_is_negative_test'() ->
?assertMatch({error, "input base must be >= 2"},
all_your_base:rebase([1], -2, 10)).

'16_negative_digit_test'() ->
?assertMatch({error,
"all digits must satisfy 0 <= d < input "
"base"},
all_your_base:rebase([1, -1, 1, 0, 1, 0], 2, 10)).

'17_invalid_positive_digit_test'() ->
?assertMatch({error,
"all digits must satisfy 0 <= d < input "
"base"},
all_your_base:rebase([1, 2, 1, 0, 1, 0], 2, 10)).

'18_output_base_is_one_test'() ->
?assertMatch({error, "output base must be >= 2"},
all_your_base:rebase([1, 0, 1, 0, 1, 0], 2, 1)).

'19_output_base_is_zero_test'() ->
?assertMatch({error, "output base must be >= 2"},
all_your_base:rebase([7], 10, 0)).

'20_output_base_is_negative_test'() ->
?assertMatch({error, "output base must be >= 2"},
all_your_base:rebase([1], 2, -7)).

'21_both_bases_are_negative_test'() ->
?assertMatch({error, "input base must be >= 2"},
all_your_base:rebase([1], -2, -7)).``````
``````-module(all_your_base).

-export([rebase/3]).

rebase(_Digits, InputBase, _OutputBase) when InputBase<2 -> {error, "input base must be >= 2"};
rebase(_Digits, _InputBase, OutputBase)when OutputBase<2 -> {error, "output base must be >= 2"};
rebase(Digits, InputBase, OutputBase) ->
try lists:foldl(fun
(E,{N,Len})  when E>=0,E<InputBase->
{N+(E*math:pow(InputBase,Len-1)),Len-1};
(_,_)->erlang:throw(error)
end,
{0,length(Digits)},Digits) of
{Number,_} -> {ok,base(Number,OutputBase)}
catch
_:_  -> {error, "all digits must satisfy 0 <= d < input base"}
end.

base(Number,OutputBase)->base(erlang:trunc(Number),OutputBase,[]).
base(Number,OutputBase,Acc) when Number<OutputBase -> [Number|Acc];
base(Number,OutputBase,Acc)->base(erlang:trunc(Number div OutputBase),OutputBase,[Number rem OutputBase|Acc]).``````