Skip to content

Commit

Permalink
Add all-your-base (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jul 28, 2024
1 parent 1df943c commit bee86ed
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "all-your-base",
"name": "All Your Base",
"uuid": "608ec8fa-6a60-44eb-a4e2-79e4a4c984eb",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
Expand Down
28 changes: 28 additions & 0 deletions exercises/practice/all-your-base/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Instructions

Convert a sequence of digits in one base, representing a number, into a sequence of digits in another base, representing the same number.

~~~~exercism/note
Try to implement the conversion yourself.
Do not use something else to perform the conversion for you.
~~~~

## About [Positional Notation][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¹) + (2 × 10⁰)`

The number 101010, _in base 2_, means:

`(1 × 2⁵) + (0 × 2⁴) + (1 × 2³) + (0 × 2²) + (1 × 2¹) + (0 × 2⁰)`

The number 1120, _in base 3_, means:

`(1 × 3³) + (1 × 3²) + (2 × 3¹) + (0 × 3⁰)`

_Yes. Those three numbers above are exactly the same. Congratulations!_

[positional-notation]: https://en.wikipedia.org/wiki/Positional_notation
8 changes: 8 additions & 0 deletions exercises/practice/all-your-base/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

You've just been hired as professor of mathematics.
Your first week went well, but something is off in your second week.
The problem is that every answer given by your students is wrong!
Luckily, your math skills have allowed you to identify the problem: the student answers _are_ correct, but they're all in base 2 (binary)!
Amazingly, it turns out that each week, the students use a different base.
To help you quickly verify the student answers, you'll be building a tool to translate between bases.
17 changes: 17 additions & 0 deletions exercises/practice/all-your-base/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"src/AllYourBase.idr"
],
"test": [
"test/src/Main.idr"
],
"example": [
"example/AllYourBase.idr"
]
},
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base."
}
73 changes: 73 additions & 0 deletions exercises/practice/all-your-base/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[5ce422f9-7a4b-4f44-ad29-49c67cb32d2c]
description = "single bit one to decimal"

[0cc3fea8-bb79-46ac-a2ab-5a2c93051033]
description = "binary to single decimal"

[f12db0f9-0d3d-42c2-b3ba-e38cb375a2b8]
description = "single decimal to binary"

[2c45cf54-6da3-4748-9733-5a3c765d925b]
description = "binary to multiple decimal"

[65ddb8b4-8899-4fcc-8618-181b2cf0002d]
description = "decimal to binary"

[8d418419-02a7-4824-8b7a-352d33c6987e]
description = "trinary to hexadecimal"

[d3901c80-8190-41b9-bd86-38d988efa956]
description = "hexadecimal to trinary"

[5d42f85e-21ad-41bd-b9be-a3e8e4258bbf]
description = "15-bit integer"

[d68788f7-66dd-43f8-a543-f15b6d233f83]
description = "empty list"

[5e27e8da-5862-4c5f-b2a9-26c0382b6be7]
description = "single zero"

[2e1c2573-77e4-4b9c-8517-6c56c5bcfdf2]
description = "multiple zeros"

[3530cd9f-8d6d-43f5-bc6e-b30b1db9629b]
description = "leading zeros"

[a6b476a1-1901-4f2a-92c4-4d91917ae023]
description = "input base is one"

[e21a693a-7a69-450b-b393-27415c26a016]
description = "input base is zero"

[54a23be5-d99e-41cc-88e0-a650ffe5fcc2]
description = "input base is negative"

[9eccf60c-dcc9-407b-95d8-c37b8be56bb6]
description = "negative digit"

[232fa4a5-e761-4939-ba0c-ed046cd0676a]
description = "invalid positive digit"

[14238f95-45da-41dc-95ce-18f860b30ad3]
description = "output base is one"

[73dac367-da5c-4a37-95fe-c87fad0a4047]
description = "output base is zero"

[13f81f42-ff53-4e24-89d9-37603a48ebd9]
description = "output base is negative"

[0e6c895d-8a5d-4868-a345-309d094cfe8d]
description = "both bases are negative"
3 changes: 3 additions & 0 deletions exercises/practice/all-your-base/all-your-base.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package all-your-base
modules = AllYourBase
sourcedir = "src"
24 changes: 24 additions & 0 deletions exercises/practice/all-your-base/example/AllYourBase.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module AllYourBase

import Data.List

public export
data Error = InvalidBase | InvalidDigit

convertTo : Int -> Int -> List Int
convertTo outputBase = reverse . loop
where loop : Int -> List Int
loop value = if value < outputBase then [value]
else (mod value outputBase) :: (loop (div value outputBase))

convertFrom : Int -> List Int -> Int
convertFrom inputBase digits = loop 0 digits
where loop : Int -> List Int -> Int
loop acc [] = acc
loop acc (x :: xs) = loop (acc * inputBase + x) xs

export
rebase : Int -> List Int -> Int -> Either Error (List Int)
rebase inputBase digits outputBase = if inputBase < 2 || outputBase < 2 then Left InvalidBase
else if find (\digit => digit < 0 || digit >= inputBase) digits /= Nothing then Left InvalidDigit
else Right $ convertTo outputBase $ convertFrom inputBase digits
10 changes: 10 additions & 0 deletions exercises/practice/all-your-base/pack.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[custom.all.all-your-base]
type = "local"
path = "."
ipkg = "all-your-base.ipkg"
test = "test/test.ipkg"

[custom.all.all-your-base-test]
type = "local"
path = "test"
ipkg = "test.ipkg"
8 changes: 8 additions & 0 deletions exercises/practice/all-your-base/src/AllYourBase.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module AllYourBase

public export
data Error = InvalidBase | InvalidDigit

export
rebase : Int -> List Int -> Int -> Either Error (List Int)
rebase inputBase digits outputBase = ?rebase_rhs
51 changes: 51 additions & 0 deletions exercises/practice/all-your-base/test/src/Main.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Main

import System
import Tester
import Tester.Runner

import AllYourBase

export
implementation Eq Error where
(==) InvalidBase InvalidBase = True
(==) InvalidDigit InvalidDigit = True
(==) _ _ = False

export
implementation Show Error where
show InvalidBase = "InvalidBase"
show InvalidDigit = "InvalidDigit"

tests : List Test
tests =
[ test "single bit one to decimal" (assertEq (rebase 2 [1] 10) $ Right [1])
, test "binary to single decimal" (assertEq (rebase 2 [1, 0, 1] 10) $ Right [5])
, test "single decimal to binary" (assertEq (rebase 10 [5] 2) $ Right [1, 0, 1])
, test "binary to multiple decimal" (assertEq (rebase 2 [1, 0, 1, 0, 1, 0] 10) $ Right [4, 2])
, test "decimal to binary" (assertEq (rebase 10 [4, 2] 2) $ Right [1, 0, 1, 0, 1, 0])
, test "trinary to hexadecimal" (assertEq (rebase 3 [1, 1, 2, 0] 16) $ Right [2, 10])
, test "hexadecimal to trinary" (assertEq (rebase 16 [2, 10] 3) $ Right [1, 1, 2, 0])
, test "15-bit integer" (assertEq (rebase 97 [3, 46, 60] 73) $ Right [6, 10, 45])
, test "empty list" (assertEq (rebase 2 [] 10) $ Right [0])
, test "single zero" (assertEq (rebase 10 [0] 2) $ Right [0])
, test "multiple zeros" (assertEq (rebase 10 [0, 0, 0] 2) $ Right [0])
, test "leading zeros" (assertEq (rebase 7 [0, 6, 0] 10) $ Right [4, 2])
, test "input base is one" (assertEq (rebase 1 [0] 10) $ Left InvalidBase)
, test "input base is zero" (assertEq (rebase 0 [] 10) $ Left InvalidBase)
, test "input base is negative" (assertEq (rebase (-2) [1] 10) $ Left InvalidBase)
, test "negative digit" (assertEq (rebase 2 [1, -1, 1, 0, 1, 0] 10) $ Left InvalidDigit)
, test "invalid positive digit" (assertEq (rebase 2 [1, 2, 1, 0, 1, 0] 10) $ Left InvalidDigit)
, test "output base is one" (assertEq (rebase 2 [1, 0, 1, 0, 1, 0] 1) $ Left InvalidBase)
, test "output base is zero" (assertEq (rebase 10 [7] 0) $ Left InvalidBase)
, test "output base is negative" (assertEq (rebase 2 [1] (-7)) $ Left InvalidBase)
, test "both bases are negative" (assertEq (rebase (-2) [1] (-7)) $ Left InvalidBase)
]

export
main : IO ()
main = do
success <- runTests tests
if success
then putStrLn "All tests passed"
else exitFailure
6 changes: 6 additions & 0 deletions exercises/practice/all-your-base/test/test.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package all-your-base-test
depends = all-your-base
, tester
main = Main
executable = "all-your-base-test"
sourcedir = "src"
37 changes: 37 additions & 0 deletions generators/exercises/all_your_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

HEADER = """
export
implementation Eq Error where
(==) InvalidBase InvalidBase = True
(==) InvalidDigit InvalidDigit = True
(==) _ _ = False
export
implementation Show Error where
show InvalidBase = "InvalidBase"
show InvalidDigit = "InvalidDigit"
"""

def header():
return HEADER

def generate_test(case):
property = case["property"]
expected = case["expected"]
if expected.__class__ == dict:
if "digits" in expected["error"]:
expected = "Left InvalidDigit"
else:
expected = "Left InvalidBase"
else:
expected = f"Right {expected}"

inputBase = case["input"]["inputBase"]
if inputBase < 0:
inputBase = f"({inputBase})"
digits = case["input"]["digits"]
outputBase = case["input"]["outputBase"]
if outputBase < 0:
outputBase = f"({outputBase})"

return f'assertEq ({property} {inputBase} {digits} {outputBase}) $ {expected}'

0 comments on commit bee86ed

Please sign in to comment.