🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # TatriX's solution

## to All Your Base in the PureScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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!

## Submitting Incomplete Solutions

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

### Main.purs

``````module Test.Main where

import Prelude
import Data.Maybe (Maybe(..))
import Test.Unit.Assert as Assert
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Console (TESTOUTPUT)
import Test.Unit.Main (runTest)
import AllYourBase (rebase)

main :: forall eff
. Eff ( avar :: AVAR
, console :: CONSOLE
, testOutput :: TESTOUTPUT
| eff
)
Unit
main = runTest suites

suites :: forall e. TestSuite e
suites = do
suite "AllYourBase.rebase" do

test "single bit one to decimal" \$
Assert.equal (Just )
(rebase 2 10 )

test "binary to single decimal" \$
Assert.equal (Just )
(rebase 2 10 [1,0,1])

test "single decimal to binary" \$
Assert.equal (Just [1,0,1])
(rebase 10 2 )

test "binary to multiple decimal" \$
Assert.equal (Just [4,2])
(rebase 2 10 [1,0,1,0,1,0])

test "decimal to binary" \$
Assert.equal (Just [1,0,1,0,1,0])
(rebase 10 2 [4,2])

Assert.equal (Just [2,10])
(rebase 3 16 [1,1,2,0])

Assert.equal (Just [1,1,2,0])
(rebase 16 3 [2,10])

test "15-bit integer" \$
Assert.equal (Just [6,10,45])
(rebase 97 73 [3,46,60])

test "empty list" \$
Assert.equal Nothing
(rebase 2 10 [])

test "single zero" \$
Assert.equal Nothing
(rebase 10 2 )

test "multiple zeros" \$
Assert.equal Nothing
(rebase 10 2 [0,0,0])

Assert.equal Nothing
(rebase 7 10 [0,6,0])

test "negative digit" \$
Assert.equal Nothing
(rebase 2 10 [1,-1,1,0,1,0])

test "invalid positive digit" \$
Assert.equal Nothing
(rebase 2 10 [1,2,1,0,1,0])

test "first base is one" \$
Assert.equal Nothing
(rebase 1 10 [])

test "second base is one" \$
Assert.equal Nothing
(rebase 2 1 [1,0,1,0,1,0])

test "first base is zero" \$
Assert.equal Nothing
(rebase 0 10 [])

test "second base is zero" \$
Assert.equal Nothing
(rebase 10 0 )

test "first base is negative" \$
Assert.equal Nothing
(rebase (-2) 10 )

test "second base is negative" \$
Assert.equal Nothing
(rebase 2 (-7) )

test "both bases are negative" \$
Assert.equal Nothing
(rebase (-2) (-7) )``````
``````module AllYourBase where

import Prelude

import Data.Maybe (Maybe(..), isJust)

type Base = Int

toDigits ::  Base -> Int -> Array Int
toDigits b n = toDigits' [] n where
toDigits' :: Array Int -> Int -> Array Int
toDigits' acc m
| m > 0 = toDigits' ([m `mod` b] <> acc) (m / b)
| otherwise = acc

fromDigits :: Base -> Array Int -> Int
fromDigits b = foldl (\n x -> b * n + x) 0

invalid :: Array Int -> Boolean
invalid digits = case head digits of
Nothing -> true
Just 0 -> true
_ -> false

rebase :: Int -> Int -> Array Int -> Maybe (Array Int)
rebase from to digits
| from <= 1 = Nothing
| to <= 1 = Nothing
| invalid digits = Nothing
| isJust \$ find (\x -> x < 0 || x >= from) digits = Nothing
| otherwise = Just \$ toDigits to \$ fromDigits from digits``````