Skip to content

Commit

Permalink
mostly mips
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Leach committed Dec 3, 2023
1 parent ef61b67 commit e68579b
Show file tree
Hide file tree
Showing 18 changed files with 624 additions and 9 deletions.
13 changes: 7 additions & 6 deletions mips/rotational-cipher/impl.mips
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
.globl rotate

rotate:
move $t0, $a0
move $t1, $a1
move $t9, $a2
move $t0, $a0 # $t0 will increment as we step through the input
#lw $t1, 0($a1) # shift key value in $t1
move $t9, $a2 # $t9 will increment as we step through the output

loop:
lb $t2, 0($t0)
addi $t0, $t0, 1
beqz $t2, done # end of input

sgeu $t4, $t2, 0x41 # >= 'a'
Expand All @@ -27,7 +28,7 @@ punctuation:
move $t3, $t2

lowercase:
add $t3, $t2, $t1
add $t3, $t2, $a1

# do we need to wrap the alphabet?
sgtu $t4, $t3, 0x7A # > 'z'
Expand All @@ -36,7 +37,7 @@ lowercase:
j store

uppercase:
add $t3, $t2, $t1
add $t3, $t2, $a1

# do we need to wrap the alphabet?
sgtu $t4, $t3, 0x5A # >= 'Z'
Expand All @@ -50,4 +51,4 @@ store:

done:
sb $zero, 0($t9) # add nul string terminator
jr $ra
jr $ra
9 changes: 6 additions & 3 deletions mips/rotational-cipher/runner.mips
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
# number of test cases
n: .word 10
# input values and expected output values (all null terminated)
texts: .asciiz "a", "a", "a", "m", "n", "OMG", "O M G", "Testing 1 2 3 testing", "Let's eat, Grandma!", "The quick brown fox jumps over the lazy dog."
shift_keys: .word 0, 1, 26, 13, 13, 5, 5, 4, 21, 13
outs: .asciiz "a", "b", "a", "z", "a", "TRL", "T R L", "Xiwxmrk 1 2 3 xiwxmrk", "Gzo'n zvo, Bmviyhv!", "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
# texts: .asciiz "a", "a", "a", "m", "n", "OMG", "O M G", "Testing 1 2 3 testing", "Let's eat, Grandma!", "The quick brown fox jumps over the lazy dog."
# shift_keys: .word 0, 1, 26, 13, 13, 5, 5, 4, 21, 13
# outs: .asciiz "a", "b", "a", "z", "a", "TRL", "T R L", "Xiwxmrk 1 2 3 xiwxmrk", "Gzo'n zvo, Bmviyhv!", "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
texts: .asciiz "a", "a", "m"
shift_keys: .word 1, 26, 13
outs: .asciiz "b", "a", "z"

failmsg: .asciiz "failed for test input: "
shiftkeymsg: .asciiz " with shift key "
Expand Down
48 changes: 48 additions & 0 deletions wren/collatz-conjecture/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 collatz-conjecture.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/collatz-conjecture/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Collatz Conjecture

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

## Instructions

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n.
If n is even, divide n by 2 to get n / 2.
If n is odd, multiply n by 3 and add 1 to get 3n + 1.
Repeat the process indefinitely.
The conjecture states that no matter which number you start with, you will always reach 1 eventually.

Given a number n, return the number of steps required to reach 1.

## Examples

Starting with n = 12, the steps would be as follows:

0. 12
1. 6
2. 3
3. 10
4. 5
5. 16
6. 8
7. 4
8. 2
9. 1

Resulting in 9 steps.
So for input n = 12, the return value would be 9.

## Source

### Created by

- @joshgoebel

### Based on

An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem
32 changes: 32 additions & 0 deletions wren/collatz-conjecture/collatz-conjecture.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "./collatz-conjecture" for Collatz
import "wren-testie/testie" for Testie, Expect

Testie.test("Collatz.steps()") { |do, skip|
do.test("zero steps for one") {
Expect.value(Collatz.steps(1)).toEqual(0)
}

do.test("divide if even") {
Expect.value(Collatz.steps(16)).toEqual(4)
}

do.test("even and odd steps") {
Expect.value(Collatz.steps(12)).toEqual(9)
}

do.test("large number of even and odd steps") {
Expect.value(Collatz.steps(1000000)).toEqual(152)
}

do.test("zero is an error") {
Expect.that {
Collatz.steps(0)
}.abortsWith("Only positive numbers are allowed")
}

do.test("negative value is an error") {
Expect.that {
Collatz.steps(-15)
}.abortsWith("Only positive numbers are allowed")
}
}
12 changes: 12 additions & 0 deletions wren/collatz-conjecture/collatz-conjecture.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Collatz {
static steps(n) {
if (n < 1) Fiber.abort("Only positive numbers are allowed")

var steps = 0
while (n > 1) {
n = (n % 2 == 0) ? n / 2 : 3 * n + 1
steps = steps + 1
}
return steps
}
}
48 changes: 48 additions & 0 deletions wren/darts/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 darts.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).
46 changes: 46 additions & 0 deletions wren/darts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Darts

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

## Instructions

Write a function that returns the earned points in a single toss of a Darts game.

[Darts][darts] is a game where players throw darts at a [target][darts-target].

In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:

![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)

- If the dart lands outside the target, player earns no points (0 points).
- If the dart lands in the outer circle of the target, player earns 1 point.
- If the dart lands in the middle circle of the target, player earns 5 points.
- If the dart lands in the inner circle of the target, player earns 10 points.

The outer circle has a radius of 10 units (this is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1.
Of course, they are all centered at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0).

Write a function that given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), returns the correct amount earned by a dart landing at that point.

## Credit

The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape].

[darts]: https://en.wikipedia.org/wiki/Darts
[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg
[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html
[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html
[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
[inkscape]: https://en.wikipedia.org/wiki/Inkscape

## Source

### Created by

- @joshgoebel

### Based on

Inspired by an exercise created by a professor Della Paolera in Argentina
56 changes: 56 additions & 0 deletions wren/darts/darts.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "./darts" for Darts
import "wren-testie/testie" for Testie, Expect

Testie.test("Darts") { |do, skip|
do.test("Missed target") {
Expect.value(Darts.score(-9, 9)).toEqual(0)
}

do.test("On the outer circle") {
Expect.value(Darts.score(0, 10)).toEqual(1)
}

do.test("On the middle circle") {
Expect.value(Darts.score(-5, 0)).toEqual(5)
}

do.test("On the inner circle") {
Expect.value(Darts.score(0, -1)).toEqual(10)
}

do.test("Exactly on centre") {
Expect.value(Darts.score(0, 0)).toEqual(10)
}

do.test("Near the centre") {
Expect.value(Darts.score(-0.1, -0.1)).toEqual(10)
}

do.test("Just within the inner circle") {
Expect.value(Darts.score(0.7, 0.7)).toEqual(10)
}

do.test("Just outside the inner circle") {
Expect.value(Darts.score(0.8, -0.8)).toEqual(5)
}

do.test("Just within the middle circle") {
Expect.value(Darts.score(-3.5, 3.5)).toEqual(5)
}

do.test("Just outside the middle circle") {
Expect.value(Darts.score(-3.6, -3.6)).toEqual(1)
}

do.test("Just within the outer circle") {
Expect.value(Darts.score(-7.0, 7.0)).toEqual(1)
}

do.test("Just outside the outer circle") {
Expect.value(Darts.score(7.1, -7.1)).toEqual(0)
}

do.test("Asymmetric position between the inner and middle circles") {
Expect.value(Darts.score(0.5, -4)).toEqual(5)
}
}
9 changes: 9 additions & 0 deletions wren/darts/darts.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Darts {
static score(x,y) {
var r_sq = x * x + y * y
if (r_sq <= 1) return 10
if (r_sq <= 25) return 5
if (r_sq <= 100) return 1
return 0
}
}
Loading

0 comments on commit e68579b

Please sign in to comment.