diff --git a/config.json b/config.json index fe38308..6705230 100644 --- a/config.json +++ b/config.json @@ -534,6 +534,14 @@ "optional_values" ] }, + { + "slug": "all-your-base", + "name": "All Your Base", + "uuid": "b969e21f-d328-49c6-a576-8cae8cb12a93", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "minesweeper", "name": "Minesweeper", diff --git a/exercises/practice/all-your-base/.docs/instructions.md b/exercises/practice/all-your-base/.docs/instructions.md new file mode 100644 index 0000000..4602b5c --- /dev/null +++ b/exercises/practice/all-your-base/.docs/instructions.md @@ -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 diff --git a/exercises/practice/all-your-base/.meta/config.json b/exercises/practice/all-your-base/.meta/config.json new file mode 100644 index 0000000..1aa1f76 --- /dev/null +++ b/exercises/practice/all-your-base/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "all-your-base.wren" + ], + "test": [ + "all-your-base.spec.wren" + ], + "example": [ + ".meta/proof.ci.wren" + ] + }, + "blurb": "Convert a number, represented as a sequence of digits in one base, to any other base." +} diff --git a/exercises/practice/all-your-base/.meta/proof.ci.wren b/exercises/practice/all-your-base/.meta/proof.ci.wren new file mode 100644 index 0000000..135e569 --- /dev/null +++ b/exercises/practice/all-your-base/.meta/proof.ci.wren @@ -0,0 +1,27 @@ +class AllYourBase { + static rebase(inputBase, digits, outputBase) { + if (inputBase < 2) { + Fiber.abort("input base must be >= 2") + } + if (outputBase < 2) { + Fiber.abort("output base must be >= 2") + } + + var number = 0 + for (digit in digits) { + if (digit < 0 || digit >= inputBase) { + Fiber.abort("all digits must satisfy 0 <= d < input base") + } + number = number * inputBase + digit + } + + var result = [] + while (number >= outputBase) { + var digit = number % outputBase + result.insert(0, digit) + number = (number - digit) / outputBase + } + result.insert(0, number) + return result + } +} diff --git a/exercises/practice/all-your-base/.meta/tests.toml b/exercises/practice/all-your-base/.meta/tests.toml new file mode 100644 index 0000000..8968c13 --- /dev/null +++ b/exercises/practice/all-your-base/.meta/tests.toml @@ -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" diff --git a/exercises/practice/all-your-base/LICENSE b/exercises/practice/all-your-base/LICENSE new file mode 100644 index 0000000..ef5d211 --- /dev/null +++ b/exercises/practice/all-your-base/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Exercism + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/exercises/practice/all-your-base/all-your-base.spec.wren b/exercises/practice/all-your-base/all-your-base.spec.wren new file mode 100644 index 0000000..64c1632 --- /dev/null +++ b/exercises/practice/all-your-base/all-your-base.spec.wren @@ -0,0 +1,88 @@ +import "./all-your-base" for AllYourBase +import "wren-testie/testie" for Testie, Expect + +Testie.test("AllYourBase.rebase()") { |do, skip| + do.test("single bit one to decimal") { + Expect.value(AllYourBase.rebase(2, [1], 10)).toEqual([1]) + } + + skip.test("binary to single decimal") { + Expect.value(AllYourBase.rebase(2, [1, 0, 1], 10)).toEqual([5]) + } + + skip.test("single decimal to binary") { + Expect.value(AllYourBase.rebase(10, [5], 2)).toEqual([1, 0, 1]) + } + + skip.test("binary to multiple decimal") { + Expect.value(AllYourBase.rebase(2, [1, 0, 1, 0, 1, 0], 10)).toEqual([4, 2]) + } + + skip.test("decimal to binary") { + Expect.value(AllYourBase.rebase(10, [4, 2], 2)).toEqual([1, 0, 1, 0, 1, 0]) + } + + skip.test("trinary to hexadecimal") { + Expect.value(AllYourBase.rebase(3, [1, 1, 2, 0], 16)).toEqual([2, 10]) + } + + skip.test("hexadecimal to trinary") { + Expect.value(AllYourBase.rebase(16, [2, 10], 3)).toEqual([1, 1, 2, 0]) + } + + skip.test("15-bit integer") { + Expect.value(AllYourBase.rebase(97, [3, 46, 60], 73)).toEqual([6, 10, 45]) + } + + skip.test("empty list") { + Expect.value(AllYourBase.rebase(2, [], 10)).toEqual([0]) + } + + skip.test("single zero") { + Expect.value(AllYourBase.rebase(10, [0], 2)).toEqual([0]) + } + + skip.test("multiple zeros") { + Expect.value(AllYourBase.rebase(10, [0, 0, 0], 2)).toEqual([0]) + } + + skip.test("leading zeros") { + Expect.value(AllYourBase.rebase(7, [0, 6, 0], 10)).toEqual([4, 2]) + } + + Expect.that { + AllYourBase.rebase(1, [0], 10) + }.abortsWith("input base must be >= 2") + + Expect.that { + AllYourBase.rebase(0, [], 10) + }.abortsWith("input base must be >= 2") + + Expect.that { + AllYourBase.rebase(-2, [1], 10) + }.abortsWith("input base must be >= 2") + + Expect.that { + AllYourBase.rebase(2, [1, -1, 1, 0, 1, 0], 10) + }.abortsWith("all digits must satisfy 0 <= d < input base") + + Expect.that { + AllYourBase.rebase(2, [1, 2, 1, 0, 1, 0], 10) + }.abortsWith("all digits must satisfy 0 <= d < input base") + + Expect.that { + AllYourBase.rebase(2, [1, 0, 1, 0, 1, 0], 1) + }.abortsWith("output base must be >= 2") + + Expect.that { + AllYourBase.rebase(10, [7], 0) + }.abortsWith("output base must be >= 2") + + Expect.that { + AllYourBase.rebase(2, [1], -7) + }.abortsWith("output base must be >= 2") + + Expect.that { + AllYourBase.rebase(-2, [1], -7) + }.abortsWith("input base must be >= 2") +} diff --git a/exercises/practice/all-your-base/all-your-base.wren b/exercises/practice/all-your-base/all-your-base.wren new file mode 100644 index 0000000..78848e4 --- /dev/null +++ b/exercises/practice/all-your-base/all-your-base.wren @@ -0,0 +1,5 @@ +class AllYourBase { + static rebase(inputBase, digits, outputBase) { + Fiber.abort("Remove this statement and implement this function") + } +} diff --git a/exercises/practice/all-your-base/package.wren b/exercises/practice/all-your-base/package.wren new file mode 100644 index 0000000..b18889e --- /dev/null +++ b/exercises/practice/all-your-base/package.wren @@ -0,0 +1,14 @@ +import "wren-package" for WrenPackage, Dependency +import "os" for Process + +class Package is WrenPackage { + construct new() {} + name { "exercism/all-your-base" } + dependencies { + return [ + Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git") + ] + } +} + +Package.new().default()