Published at Oct 02 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)
toDigit : Int -> List Int -> Int
toDigit inbase digits =
digits |> List.indexedMap (\i x -> x * inbase ^ (List.length digits - i - 1)) |> List.sum
fromDigit : Int -> List Int -> Int -> List Int
fromDigit inbase outList num =
if inbase <= 0 then
[ 0 ]
else if num < inbase then
List.append outList [ num ]
else
fromDigit inbase (List.append outList [ modBy inbase num ]) (num // inbase)
rebase : Int -> List Int -> Int -> Maybe (List Int)
rebase inBase digits outBase =
let
outDigits =
digits |> toDigit inBase |> fromDigit outBase [] |> List.reverse
negativeDigits =
List.filter (\x -> x < 0) digits
invalidDigits =
List.filter (\x -> x // inBase >= 1) digits
in
if List.length outDigits == 1 && List.head outDigits == Just 0 then
Nothing
else if List.length negativeDigits > 0 || List.length invalidDigits > 0 then
Nothing
else
Just outDigits
```

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