Published at Jul 13 2018
·
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!*

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

```
module Test.Main where
import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.AVar (AVAR)
import Control.Monad.Eff.Console (CONSOLE)
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 [1])
(rebase 2 10 [1])
test "binary to single decimal" $
Assert.equal (Just [5])
(rebase 2 10 [1,0,1])
test "single decimal to binary" $
Assert.equal (Just [1,0,1])
(rebase 10 2 [5])
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])
test "trinary to hexadecimal" $
Assert.equal (Just [2,10])
(rebase 3 16 [1,1,2,0])
test "hexadecimal to trinary" $
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 [0])
test "multiple zeros" $
Assert.equal Nothing
(rebase 10 2 [0,0,0])
test "leading zeros" $
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 [7])
test "first base is negative" $
Assert.equal Nothing
(rebase (-2) 10 [1])
test "second base is negative" $
Assert.equal Nothing
(rebase 2 (-7) [1])
test "both bases are negative" $
Assert.equal Nothing
(rebase (-2) (-7) [1])
```

```
module AllYourBase
( rebase
) where
import Data.Foldable (class Foldable)
import Data.List.Lazy (foldMap, foldl)
import Data.Maybe (Maybe(..))
import Data.Monoid.Conj (Conj(..))
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)
import Data.Array (null, head, all, reverse)
import Prelude (mod, not, otherwise, (#), ($), (&&), (*), (+), (/), (<), (<=), (==), (>=))
rebase :: Int -> Int -> Array Int -> Maybe (Array Int)
rebase srcRadix destRadix digits
| srcRadix <= 1 = Nothing
| destRadix <= 1 = Nothing
| not $ all (isValidDigit srcRadix) digits = Nothing
| null digits = Nothing
| head digits == Just 0 = Nothing
| otherwise = Just $
fromFoldable digits # toUnfoldable # reverse
where
fromFoldable :: forall f. Foldable f => f Int -> Int
fromFoldable = foldl (\number digit -> number * srcRadix + digit) 0
toUnfoldable :: forall f. Unfoldable f => Int -> f Int
toUnfoldable = unfoldr (\number -> if number <= 0
then Nothing
else Just $ Tuple (number `mod` destRadix) (number / destRadix))
isValidRadix :: Int -> Boolean
isValidRadix radix = radix >= 2
isValidDigit :: Int -> Int -> Boolean
isValidDigit radix digit = 0 <= digit && digit < radix
```

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