🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ikarachristos

ikarachristos's solution

to All Your Base in the Erlang Track

Published at Feb 07 2021 · 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.

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

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/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_'() ->
    {"single bit one to decimal",
     ?_assertMatch({ok, [1]},
		   all_your_base:rebase([1], 2, 10))}.

'2_binary_to_single_decimal_test_'() ->
    {"binary to single decimal",
     ?_assertMatch({ok, [5]},
		   all_your_base:rebase([1, 0, 1], 2, 10))}.

'3_single_decimal_to_binary_test_'() ->
    {"single decimal to binary",
     ?_assertMatch({ok, [1, 0, 1]},
		   all_your_base:rebase([5], 10, 2))}.

'4_binary_to_multiple_decimal_test_'() ->
    {"binary to multiple decimal",
     ?_assertMatch({ok, [4, 2]},
		   all_your_base:rebase([1, 0, 1, 0, 1, 0], 2, 10))}.

'5_decimal_to_binary_test_'() ->
    {"decimal to binary",
     ?_assertMatch({ok, [1, 0, 1, 0, 1, 0]},
		   all_your_base:rebase([4, 2], 10, 2))}.

'6_trinary_to_hexadecimal_test_'() ->
    {"trinary to hexadecimal",
     ?_assertMatch({ok, [2, 10]},
		   all_your_base:rebase([1, 1, 2, 0], 3, 16))}.

'7_hexadecimal_to_trinary_test_'() ->
    {"hexadecimal to trinary",
     ?_assertMatch({ok, [1, 1, 2, 0]},
		   all_your_base:rebase([2, 10], 16, 3))}.

'8_15_bit_integer_test_'() ->
    {"15-bit integer",
     ?_assertMatch({ok, [6, 10, 45]},
		   all_your_base:rebase([3, 46, 60], 97, 73))}.

'9_empty_list_test_'() ->
    {"empty list",
     ?_assertMatch({ok, [0]},
		   all_your_base:rebase([], 2, 10))}.

'10_single_zero_test_'() ->
    {"single zero",
     ?_assertMatch({ok, [0]},
		   all_your_base:rebase([0], 10, 2))}.

'11_multiple_zeros_test_'() ->
    {"multiple zeros",
     ?_assertMatch({ok, [0]},
		   all_your_base:rebase([0, 0, 0], 10, 2))}.

'12_leading_zeros_test_'() ->
    {"leading zeros",
     ?_assertMatch({ok, [4, 2]},
		   all_your_base:rebase([0, 6, 0], 7, 10))}.

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

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

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

'16_negative_digit_test_'() ->
    {"negative digit",
     ?_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_'() ->
    {"invalid positive digit",
     ?_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_'() ->
    {"output base is one",
     ?_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_'() ->
    {"output base is zero",
     ?_assertMatch({error, "output base must be >= 2"},
		   all_your_base:rebase([7], 10, 0))}.

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

'21_both_bases_are_negative_test_'() ->
    {"both bases are negative",
     ?_assertMatch({error, "input base must be >= 2"},
		   all_your_base:rebase([1], -2, -7))}.
-module(all_your_base).

-export([rebase/3, convert_to_base_10/4]).


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) ->
  case convert_to_base_10(Digits, InputBase, length(Digits) - 1, 0) of
    {error, Message} -> {error, Message};
    0 -> {ok, [0]};
    Number ->  convert_to_base(Number, OutputBase, [])
  end.

convert_to_base_10([], _Base, _N, Sum) ->
  Sum;
convert_to_base_10([H|_], Base, _N, _Sum) when H < 0 orelse H >= Base ->
  {error , "all digits must satisfy 0 <= d < input base"};
convert_to_base_10([H|T], Base, N, Sum) ->
  convert_to_base_10(T, Base, N - 1, Sum + trunc(H * math:pow(Base, N))).

convert_to_base(0, _Base, Acc) -> {ok, Acc};
convert_to_base(N, Base, Acc) -> convert_to_base(N div Base, Base, [N rem Base | Acc]).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?