Avatar of paulfioravanti

paulfioravanti's solution

to All Your Base in the Elm Track

Published at Aug 10 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.

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!

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


rebase : Int -> List Int -> Int -> Maybe (List Int)
rebase inBase digits outBase =
    case digits of
        [] ->
            Nothing

        0 :: tail ->
            rebase inBase tail outBase

        _ ->
            if
                hasInvalidBases inBase outBase
                    || containsInvalidDigits inBase digits
            then
                Nothing

            else
                let
                    outBaseDigits =
                        digits
                            |> sumInput inBase
                            |> convertToOutBaseDigits outBase []
                in
                Just outBaseDigits



-- PRIVATE


hasInvalidBases : Int -> Int -> Bool
hasInvalidBases inBase outBase =
    let
        minimumBase =
            2
    in
    inBase < minimumBase || outBase < minimumBase


containsInvalidDigits : Int -> List Int -> Bool
containsInvalidDigits inBase digits =
    let
        isInvalidDigit : Int -> Int -> Bool
        isInvalidDigit base digit =
            digit < 0 || digit >= base
    in
    digits
        |> List.any (isInvalidDigit inBase)


sumInput : Int -> List Int -> Int
sumInput inBase digits =
    let
        addPower : Int -> ( Int, Int ) -> Int -> Int
        addPower base ( index, digit ) acc =
            acc + digit * base ^ index
    in
    digits
        |> List.reverse
        |> List.indexedMap Tuple.pair
        |> List.foldl (addPower inBase) 0


convertToOutBaseDigits : Int -> List Int -> Int -> List Int
convertToOutBaseDigits outBase digits total =
    let
        remainder =
            modBy outBase total

        outBaseDigits =
            remainder :: digits
    in
    if total < outBase then
        outBaseDigits

    else
        (total // outBase)
            |> convertToOutBaseDigits outBase outBaseDigits

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?