Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Exercise]: All Your Base #248

Merged
merged 2 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@
],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "all-your-base",
"name": "All Your Base",
"uuid": "d9887ff7-95fe-4e9c-b960-aa5a6a0baa61",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
33 changes: 33 additions & 0 deletions exercises/practice/all-your-base/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Instructions

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][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!*

[positional-notation]: https://en.wikipedia.org/wiki/Positional_notation
63 changes: 63 additions & 0 deletions exercises/practice/all-your-base/.meta/AllYourBase.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Function Invoke-Rebase() {
<#
.SYNOPSIS
Convert a number, represented as a sequence of digits in one base, to any other base.

.DESCRIPTION
Implement general base conversion of a number.
Given an array of digits represent a number in base "a", convert it and return an array of digits represent the same number in base "b".

.PARAMETER Digits
Array of digits represent the number to be converted.

.PARAMETER InputBase
The original base of the number.

.PARAMETER OutputBase
The base to be converted to.

.EXAMPLE
Invoke-Rebase -Digits @(1, 0, 1 , 0 ,1 ) -InputBase 2 -OutputBase 10
return : @(2, 1)
#>
[CmdletBinding()]
Param(
[int[]]$Digits,
[int]$InputBase,
[int]$OutputBase
)
ErrorHandling $Digits $InputBase $OutputBase

$result = @()
$decimal = ConvertToDecimal $Digits $InputBase

while ($decimal -ge $OutputBase) {
$result += $decimal % $OutputBase
$decimal = [Math]::Floor($decimal / $OutputBase)
}
if ($decimal -lt $OutputBase) {
$result += $decimal
}
[array]::Reverse($result)
return $result
}

Function ConvertToDecimal($Digits, $Base) {
$DecimalValue = 0
for ($i = $Digits.Count-1; $i -ge 0; $i--) {
$DecimalValue += $Digits[$i] * [Math]::Pow($Base, ($Digits.Count - 1) - $i)
}
return $DecimalValue
}

Function ErrorHandling($Digits, $InputBase, $OutputBase) {
if ($InputBase -lt 2) {
Throw "input base must be >= 2"
}
if ($OutputBase -lt 2) {
Throw "output base must be >= 2"
}
if (($Digits | Where-Object { $_ -lt 0 -or $_ -ge $InputBase}).Count ) {
Throw "all digits must satisfy 0 <= digit < input base"
}
}
15 changes: 15 additions & 0 deletions exercises/practice/all-your-base/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"authors": ["glaxxie"],
"files": {
"solution": [
"AllYourBase.ps1"
],
"test": [
"AllYourBase.tests.ps1"
],
"example": [
".meta/AllYourBase.example.ps1"
]
},
"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"
30 changes: 30 additions & 0 deletions exercises/practice/all-your-base/AllYourBase.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Function Invoke-Rebase() {
<#
.SYNOPSIS
Convert a number, represented as a sequence of digits in one base, to any other base.

.DESCRIPTION
Implement general base conversion of a number.
Given an array of digits represent a number in base "a", convert it and return an array of digits represent the same number in base "b".

.PARAMETER Digits
Array of digits represent the number to be converted.

.PARAMETER InputBase
The original base of the number.

.PARAMETER OutputBase
The base to be converted to.

.EXAMPLE
Invoke-Rebase -Digits @(1, 0, 1 , 0 ,1 ) -InputBase 2 -OutputBase 10
return : @(2, 1)
glaxxie marked this conversation as resolved.
Show resolved Hide resolved
#>
[CmdletBinding()]
Param(
[int[]]$Digits,
[int]$InputBase,
[int]$OutputBase
)
Throw "Please implement this function"
}
129 changes: 129 additions & 0 deletions exercises/practice/all-your-base/AllYourBase.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
BeforeAll {
. ".\AllYourBase.ps1"
}

Describe "All your base test cases" {
Context "passing tests" {
It "single bit one to decimal" {
$got = Invoke-Rebase -Digits @(1) -InputBase 2 -OutputBase 10
$want = @(1)

$got | Should -BeExactly $want
}

It "binary to single decimal" {
$got = (Invoke-Rebase -Digits @(1,0,1) -InputBase 2 -OutputBase 10)
$want = @(5)

$got | Should -BeExactly $want
}

It "single decimal to binary" {
$got = (Invoke-Rebase -Digits @(5) -InputBase 10 -OutputBase 2)
$want = @(1,0,1)

$got | Should -BeExactly $want
}

It "binary to multiple decimal" {
$got = (Invoke-Rebase -Digits @(1, 0, 1, 0, 1, 0) -InputBase 2 -OutputBase 10)
$want = @(4,2)

$got | Should -BeExactly $want
}

It "decimal to binary" {
$got = (Invoke-Rebase -Digits @(4, 2) -InputBase 10 -OutputBase 2)
$want = @(1,0,1,0,1,0)

$got | Should -BeExactly $want
}

It "trinary to hexadecimal" {
$got = (Invoke-Rebase -Digits @(1, 1, 2, 0) -InputBase 3 -OutputBase 16)
$want = @(2,10)

$got | Should -BeExactly $want
}

It "hexadecimal to trinary" {
$got = (Invoke-Rebase -Digits @(2, 10) -InputBase 16 -OutputBase 3)
$want = @(1,1,2,0)

$got | Should -BeExactly $want
}

It "15-bit integer" {
$got = (Invoke-Rebase -Digits @(3, 46, 60) -InputBase 97 -OutputBase 73)
$want = @(6,10,45)

$got | Should -BeExactly $want
}

It "empty list" {
$got = (Invoke-Rebase -Digits @() -InputBase 2 -OutputBase 10)
$want = @(0)

$got | Should -BeExactly $want
}

It "single zero" {
$got = (Invoke-Rebase -Digits @(0) -InputBase 10 -OutputBase 2)
$want = @(0)

$got | Should -BeExactly $want
}

It "multiple zeros" {
$got = (Invoke-Rebase -Digits @(0, 0, 0, 0) -InputBase 10 -OutputBase 2)
$want = @(0)

$got | Should -BeExactly $want
}

It "leading zeros" {
$got = (Invoke-Rebase -Digits @(0, 6, 0) -InputBase 7 -OutputBase 10)
$want = @(4,2)

$got | Should -BeExactly $want
}
}

Context "invalid inputs" {
It "input base is one" {
{Invoke-Rebase -Digits @(0) -InputBase 1 -OutputBase 10} | Should -Throw "input base must be >= 2"
}

It "input base is zero" {
{Invoke-Rebase -Digits @() -InputBase 0 -OutputBase 10} | Should -Throw "input base must be >= 2"
}

It "input base is negative" {
{Invoke-Rebase -Digits @(1) -InputBase -3 -OutputBase 10} | Should -Throw "input base must be >= 2"
}

It "negative digit" {
{Invoke-Rebase -Digits @(1, 0 ,-1 ,0 , 1) -InputBase 3 -OutputBase 10} | Should -Throw "all digits must satisfy 0 <= digit < input base"
}

It "invalid positive digit" {
{Invoke-Rebase -Digits @(1, 0, 2 ,1 ,0 , 1) -InputBase 2 -OutputBase 10} | Should -Throw "all digits must satisfy 0 <= digit < input base"
}

It "output base is one" {
{Invoke-Rebase -Digits @(1) -InputBase 4 -OutputBase 1} | Should -Throw "output base must be >= 2"
}

It "output base is zero" {
{Invoke-Rebase -Digits @(1) -InputBase 5 -OutputBase 0} | Should -Throw "output base must be >= 2"
}

It "output base is negative" {
{Invoke-Rebase -Digits @(1) -InputBase 3 -OutputBase -7} | Should -Throw "output base must be >= 2"
}

It "both bases are negative" {
{Invoke-Rebase -Digits @(1) -InputBase -2 -OutputBase -5} | Should -Throw "input base must be >= 2"
}
}
}