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

Published at Jun 13 2020
·
0 comments

Instructions

Test suite

Solution

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**.

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

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!*

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
```

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.

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

```
%% 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)).
'6_trinary_to_hexadecimal_test'() ->
?assertMatch({ok, [2, 10]},
all_your_base:rebase([1, 1, 2, 0], 3, 16)).
'7_hexadecimal_to_trinary_test'() ->
?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)).
'12_leading_zeros_test'() ->
?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]).
pow(_, 0) ->
1;
pow(X, N) ->
X * pow(X, N - 1).
to_dec(_, Base) when Base < 2 ->
{error, "input base must be >= 2"};
to_dec(Xs, Base) ->
to_dec(Xs, Base, 0).
to_dec([], _, N) ->
{ok, N};
to_dec([X | _], Base, _) when X < 0 orelse X >= Base ->
{error, "all digits must satisfy 0 <= d < input base"};
to_dec([X | Xs], Base, N) ->
to_dec(Xs, Base, N + X * pow(Base, length(Xs))).
from_dec(_, Base) when Base < 2 ->
{error, "output base must be >= 2"};
from_dec(0, _) ->
{ok, [0]};
from_dec(N, Base) ->
from_dec(N, Base, []).
from_dec(0, _, Xs) ->
{ok, Xs};
from_dec(N, Base, Xs) ->
from_dec(N div Base, Base, [N rem Base | Xs]).
rebase(Digits, InputBase, OutputBase) ->
case Err = to_dec(Digits, InputBase) of
{error, _} ->
Err;
{ok, Xs} ->
from_dec(Xs, OutputBase)
end.
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments