 # stephl001's solution

## to All Your Base in the Elm Track

Published at Sep 30 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.

## Note

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

## Elm Installation

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

## Writing the Code

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!

## Submitting Incomplete Solutions

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

### Tests.elm

``````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 <|
\() -> Expect.equal (Just [ 2, 10 ]) (rebase 3 [ 1, 1, 2, 0 ] 16)
, skip <|
\() -> 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 <|
\() -> 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)

type Base
= Base Int

createBase : Int -> Maybe Base
createBase base =
if base <= 1 then
Nothing

else
Just <| Base base

type Digits
= Digits (List Int)

invalidDigits : Base -> List Int -> Bool
invalidDigits (Base base) digits =
[ List.isEmpty
, List.all ((==) 0)
, List.any (\n -> n < 0)
, List.any (\n -> n >= base)
]
|> List.any ((|>) digits)

createDigits : List Int -> Base -> Maybe Digits
createDigits digits base =
if invalidDigits base digits then
Nothing

else
Just <| Digits digits

extractDigits : Maybe Digits -> Maybe (List Int)
extractDigits digits =
case digits of
Just (Digits listDigits) ->
Just listDigits

Nothing ->
Nothing

rebaseMaybe : Base -> Digits -> Base -> List Int
rebaseMaybe (Base inBase) (Digits digits) (Base outBase) =
evaluate inBase digits
|> toBase outBase

evaluate : Int -> List Int -> Int
evaluate base =
List.foldr (\digit ( exp, acc ) -> ( exp + 1, acc + digit * base ^ exp )) ( 0, 0 )
>> Tuple.second

toBaseAcc : List Int -> Int -> Int -> List Int
toBaseAcc acc base value =
if value < 1 then
acc

else
toBaseAcc (remainderBy base value :: acc) base (value // base)

toBase : Int -> Int -> List Int
toBase =
toBaseAcc []

rebase : Int -> List Int -> Int -> Maybe (List Int)
rebase inBase digits outBase =
let
inBaseValidated =
createBase inBase

outBaseValidated =
createBase outBase

digitsValidated =
Maybe.andThen (createDigits digits) inBaseValidated
in
Maybe.map3
rebaseMaybe
inBaseValidated
digitsValidated
outBaseValidated``````