Published at Aug 24 2019
·
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!*

Refer to the Installing Elm page for information about installing elm.

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

```
$ elm-test
```

Automatically run tests again when you save changes:

```
$ elm-test --watch
```

As you work your way through the test suite, be sure to remove the `skip <|`

calls from each test until you get them all passing!

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

```
module Tests exposing (tests)
import AllYourBase exposing (rebase)
import Expect
import Test exposing (..)
tests : Test
tests =
describe "all-your-base"
[ test "single bit one to decimal" <|
\() -> Expect.equal (Just [ 1 ]) (rebase 2 [ 1 ] 10)
, skip <|
test "binary to single decimal" <|
\() -> Expect.equal (Just [ 5 ]) (rebase 2 [ 1, 0, 1 ] 10)
, skip <|
test "single decimal to binary" <|
\() -> Expect.equal (Just [ 1, 0, 1 ]) (rebase 10 [ 5 ] 2)
, skip <|
test "binary to multiple decimal" <|
\() -> Expect.equal (Just [ 4, 2 ]) (rebase 2 [ 1, 0, 1, 0, 1, 0 ] 10)
, skip <|
test "decimal to binary" <|
\() -> Expect.equal (Just [ 1, 0, 1, 0, 1, 0 ]) (rebase 10 [ 4, 2 ] 2)
, skip <|
test "trinary to hexadecimal" <|
\() -> Expect.equal (Just [ 2, 10 ]) (rebase 3 [ 1, 1, 2, 0 ] 16)
, skip <|
test "hexadecimal to trinary" <|
\() -> Expect.equal (Just [ 1, 1, 2, 0 ]) (rebase 16 [ 2, 10 ] 3)
, skip <|
test "15-bit integer" <|
\() -> Expect.equal (Just [ 6, 10, 45 ]) (rebase 97 [ 3, 46, 60 ] 73)
, skip <|
test "empty list" <|
\() -> Expect.equal Nothing (rebase 2 [] 10)
, skip <|
test "single zero" <|
\() -> Expect.equal Nothing (rebase 10 [ 0 ] 2)
, skip <|
test "multiple zeros" <|
\() -> Expect.equal Nothing (rebase 10 [ 0, 0, 0 ] 2)
, skip <|
test "leading zeros" <|
\() -> Expect.equal (Just [ 4, 2 ]) (rebase 7 [ 0, 6, 0 ] 10)
, skip <|
test "first base is one" <|
\() -> Expect.equal Nothing (rebase 1 [] 10)
, skip <|
test "first base is zero" <|
\() -> Expect.equal Nothing (rebase 0 [] 10)
, skip <|
test "first base is negative" <|
\() -> Expect.equal Nothing (rebase -1 [] 10)
, skip <|
test "negative digit" <|
\() -> Expect.equal Nothing (rebase 2 [ 1, -1, 1, 0, 1, 0 ] 10)
, skip <|
test "invalid positive digit" <|
\() -> Expect.equal Nothing (rebase 2 [ 1, 2, 1, 0, 1, 0 ] 10)
, skip <|
test "second base is one" <|
\() -> Expect.equal Nothing (rebase 10 [] 1)
, skip <|
test "second base is zero" <|
\() -> Expect.equal Nothing (rebase 10 [ 1 ] 0)
, skip <|
test "second base is negative" <|
\() -> Expect.equal Nothing (rebase 10 [ 1 ] -1)
, skip <|
test "both bases are negative" <|
\() -> Expect.equal Nothing (rebase -1 [ 1 ] -1)
]
```

```
module AllYourBase exposing (rebase)
toBase10 : Int -> List Int -> Maybe Int
toBase10 inBase digits =
let
convertDigit base digit accumulator =
case accumulator of
Nothing ->
Nothing
Just ( value, exponent ) ->
if (0 <= digit) && (digit < base) then
Just ( value + digit * exponent, exponent * base )
else
Nothing
in
case digits of
[] ->
Nothing
_ ->
List.foldr (convertDigit inBase) (Just ( 0, 1 )) digits
|> Maybe.map (\( a, b ) -> a)
fromBase10 : Int -> Int -> List Int
fromBase10 toBase number =
case number of
0 ->
[]
_ ->
fromBase10 toBase (number // toBase) ++ [ modBy toBase number ]
exceptIfEmpty : Maybe (List Int) -> Maybe (List Int)
exceptIfEmpty list =
case list of
Nothing ->
Nothing
Just [] ->
Nothing
_ ->
list
rebase : Int -> List Int -> Int -> Maybe (List Int)
rebase inBase digits outBase =
if (inBase < 1) || (outBase < 1) then
Nothing
else
toBase10 inBase digits
|> Maybe.map (fromBase10 outBase)
|> exceptIfEmpty
```

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,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments