Skip to content

Commit

Permalink
wren all-your-base
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Leach committed Nov 9, 2023
1 parent 0534a9c commit 5e033cb
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
2 changes: 2 additions & 0 deletions wren/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
48 changes: 48 additions & 0 deletions wren/all-your-base/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Help

## Running the tests

## Installing the test suite

Inside your exercise folder simply run:

```
$ wrenc package.wren install
```

## Running the tests

Execute the test suite with:

```
$ wrenc [spec-file]
```

Example:

```
$ wrenc hello-world.spec.wren
```

## Submitting your solution

You can submit your solution using the `exercism submit all-your-base.wren` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Wren track's documentation](https://exercism.org/docs/tracks/wren)
- The [Wren track's programming category on the forum](https://forum.exercism.org/c/programming/wren)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

If you're having trouble with an exercise, feel free to ask for help in the [Wren Discord](https://discord.gg/VTzuWmBavH).
44 changes: 44 additions & 0 deletions wren/all-your-base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# All Your Base

Welcome to All Your Base on Exercism's Wren Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## 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

## Source

### Created by

- @keiravillekode
88 changes: 88 additions & 0 deletions wren/all-your-base/all-your-base.spec.wren
Original file line number Diff line number Diff line change
@@ -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])
}

do.test("binary to single decimal") {
Expect.value(AllYourBase.rebase(2, [1, 0, 1], 10)).toEqual([5])
}

do.test("single decimal to binary") {
Expect.value(AllYourBase.rebase(10, [5], 2)).toEqual([1, 0, 1])
}

do.test("binary to multiple decimal") {
Expect.value(AllYourBase.rebase(2, [1, 0, 1, 0, 1, 0], 10)).toEqual([4, 2])
}

do.test("decimal to binary") {
Expect.value(AllYourBase.rebase(10, [4, 2], 2)).toEqual([1, 0, 1, 0, 1, 0])
}

do.test("trinary to hexadecimal") {
Expect.value(AllYourBase.rebase(3, [1, 1, 2, 0], 16)).toEqual([2, 10])
}

do.test("hexadecimal to trinary") {
Expect.value(AllYourBase.rebase(16, [2, 10], 3)).toEqual([1, 1, 2, 0])
}

do.test("15-bit integer") {
Expect.value(AllYourBase.rebase(97, [3, 46, 60], 73)).toEqual([6, 10, 45])
}

do.test("empty list") {
Expect.value(AllYourBase.rebase(2, [], 10)).toEqual([0])
}

do.test("single zero") {
Expect.value(AllYourBase.rebase(10, [0], 2)).toEqual([0])
}

do.test("multiple zeros") {
Expect.value(AllYourBase.rebase(10, [0, 0, 0], 2)).toEqual([0])
}

do.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")
}
48 changes: 48 additions & 0 deletions wren/all-your-base/all-your-base.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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")
}

if (digits.count == 0) {
return [0]
}

var sorted = digits[0..-1]
sorted.sort()
if (sorted[0] < 0 || sorted[-1] >= inputBase) {
Fiber.abort("all digits must satisfy 0 <= d < input base")
}

return toDigits(fromDigits(digits, inputBase), outputBase)
}

static fromDigits(digits, base) {
var number = 0
var multiplier = 1
for (digit in digits[(digits.count - 1)..0]) {
number = number + digit * multiplier
multiplier = multiplier * base
}
return number
}

static toDigits(number, base) {
if (number == 0) {
return [0]
}

var result = []
while (number > 0) {
result.add(number % base)
number = (number / base).floor
}
return result[(result.count - 1)..0]
}

}

0 comments on commit 5e033cb

Please sign in to comment.