Skip to content

Commit

Permalink
Add pig-latin exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jun 6, 2024
1 parent 16bdb15 commit 5fa176a
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "pig-latin",
"name": "Pig Latin",
"uuid": "d689fa3b-4ec9-437a-87e5-6490f868a797",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/pig-latin/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hints

## General

- The `$t0-9` registers can be used to temporarily store values
- The instructions specify which registers are used as input and output
9 changes: 9 additions & 0 deletions exercises/practice/pig-latin/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Instructions append

## Registers

| Register | Usage | Type | Description |
| -------- | ------------ | ------- | ----------------------------- |
| `$a0` | input | address | null-terminated phrase |
| `$a1` | input/output | address | null-terminated result string |
| `$t0-9` | temporary | any | for temporary storage |
46 changes: 46 additions & 0 deletions exercises/practice/pig-latin/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Instructions

Your task is to translate text from English to Pig Latin.
The translation is defined using four rules, which look at the pattern of vowels and consonants at the beginning of a word.
These rules look at each word's use of vowels and consonants:

- vowels: the letters `a`, `e`, `i`, `o`, and `u`
- consonants: the other 21 letters of the English alphabet

## Rule 1

If a word begins with a vowel, or starts with `"xr"` or `"yt"`, add an `"ay"` sound to the end of the word.

For example:

- `"apple"` -> `"appleay"` (starts with vowel)
- `"xray"` -> `"xrayay"` (starts with `"xr"`)
- `"yttria"` -> `"yttriaay"` (starts with `"yt"`)

## Rule 2

If a word begins with a one or more consonants, first move those consonants to the end of the word and then add an `"ay"` sound to the end of the word.

For example:

- `"pig"` -> `"igp"` -> `"igpay"` (starts with single consonant)
- `"chair"` -> `"airch"` -> `"airchay"` (starts with multiple consonants)
- `"thrush"` -> `"ushthr"` -> `"ushthray"` (starts with multiple consonants)

## Rule 3

If a word starts with zero or more consonants followed by `"qu"`, first move those consonants (if any) and the `"qu"` part to the end of the word, and then add an `"ay"` sound to the end of the word.

For example:

- `"quick"` -> `"ickqu"` -> `"ay"` (starts with `"qu"`, no preceding consonants)
- `"square"` -> `"aresqu"` -> `"aresquay"` (starts with one consonant followed by `"qu`")

## Rule 4

If a word starts with one or more consonants followed by `"y"`, first move the consonants preceding the `"y"`to the end of the word, and then add an `"ay"` sound to the end of the word.

Some examples:

- `"my"` -> `"ym"` -> `"ymay"` (starts with single consonant followed by `"y"`)
- `"rhythm"` -> `"ythmrh"` -> `"ythmrhay"` (starts with multiple consonants followed by `"y"`)
8 changes: 8 additions & 0 deletions exercises/practice/pig-latin/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

Your parents have challenged you and your sibling to a game of two-on-two basketball.
Confident they'll win, they let you score the first couple of points, but then start taking over the game.
Needing a little boost, you start speaking in [Pig Latin][pig-latin], which is a made-up children's language that's difficult for non-children to understand.
This will give you the edge to prevail over your parents!

[pig-latin]: https://en.wikipedia.org/wiki/Pig_latin
19 changes: 19 additions & 0 deletions exercises/practice/pig-latin/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"blurb": "Implement a program that translates from English to Pig Latin.",
"source": "The Pig Latin exercise at Test First Teaching by Ultrasaurus",
"source_url": "https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/"
}
104 changes: 104 additions & 0 deletions exercises/practice/pig-latin/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | --------------------------------------- |
# | `$a0` | input | address | beginning of word(s) |
# | `$a1` | input/output | address | output pointer |
# | `$t0` | temporary | address | position in current word |
# | `$t1` | temporary | address | end of current word |
# | `$t3` | temporary | byte | previous letter in word |
# | `$t4` | temporary | byte | current letter in word |
# | `$t5` | temporary | byte | second letter in word |
# | `$t6` | temporary | byte | non-zero when current letter is a vowel |
# | `$t7` | temporary | byte | 'a' |
# | `$t8` | temporary | byte | 'y' |
# | `$t9` | temporary | address | vowels table, minus 'a' |

.globl translate

.data

# non-zero for 'a' 'e' 'i' 'o' 'u' 'y'
vowels: .ascii "a\0\0\0e\0\0\0i\0\0\0\0\0o\0\0\0\0\0u\0\0\0y\0"


.text

translate:
li $t7, 'a'
li $t8, 'y'
la $t9, vowels
sub $t9, $t9, $t7 # vowels table, minus 'a'

start_word:
move $t0, $a0 # beginning of word
lb $t4, 0($a0) # first letter in word
beqz $t4, return
lb $t5, 1($a0) # second letter in word
add $t6, $t9, $t4
lb $t6, 0($t6) # non-zero if first letter is a vowel or 'y'
bnez $t6, check_yt

check_xr:
bne $t4, 'x', consonant
bne $t5, 'r', consonant
j vowel # Treat initial 'x' 'r' as vowel

check_yt:
bne $t4, 'y', vowel
beq $t5, 't', vowel # Treat initial 'y' 't' as vowel

consonant:
addi $t0, $t0, 1
move $t3, $t4 # previous letter
lb $t4, 0($t0) # current letter in word
blt $t4, $t7, vowel # jump forward as we have reached the end of the word

add $t6, $t9, $t4
lb $t6, 0($t6) # non-zero if current letter is a vowel or 'y'
beqz $t6, consonant

bne $t4, 'u', vowel
bne $t3, 'q', vowel
addi $t0, $t0, 1 # group 'u' after 'q' with leading consonants

vowel:
# Letters $a0 (inclusive) through $t0 (exclusive)
# will be output after the word's remaining letters.
move $t1, $t0
j check_for_remaining_letters


copy_remaining_letters:
addi $t1, $t1, 1
sb $t4, 0($a1)
addi $a1, $a1, 1 # Increment output pointer

check_for_remaining_letters:
lb $t4, 0($t1)
bge $t4, $t7, copy_remaining_letters
j check_for_leading_consonants


copy_leading_consonants:
lb $t5, 0($a0)
addi $a0, $a0, 1
sb $t5, 0($a1)
addi $a1, $a1, 1 # Increment output pointer

check_for_leading_consonants:
bne $a0, $t0, copy_leading_consonants

copy_ay:
sb $t7, 0($a1) # 'a'
addi $a1, $a1, 1 # Increment output pointer
sb $t8, 0($a1) # 'y'
addi $a1, $a1, 1 # Increment output pointer
beqz $t4, return # Check for null terminator

sb $t4, 0($a1) # Output space between words
addi $a1, $a1, 1 # Increment output pointer
addi $a0, $t1, 1 # Start of next word
j start_word

return:
sb $zero, 0($a1)
jr $ra
76 changes: 76 additions & 0 deletions exercises/practice/pig-latin/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# 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.

[11567f84-e8c6-4918-aedb-435f0b73db57]
description = "ay is added to words that start with vowels -> word beginning with a"

[f623f581-bc59-4f45-9032-90c3ca9d2d90]
description = "ay is added to words that start with vowels -> word beginning with e"

[7dcb08b3-23a6-4e8a-b9aa-d4e859450d58]
description = "ay is added to words that start with vowels -> word beginning with i"

[0e5c3bff-266d-41c8-909f-364e4d16e09c]
description = "ay is added to words that start with vowels -> word beginning with o"

[614ba363-ca3c-4e96-ab09-c7320799723c]
description = "ay is added to words that start with vowels -> word beginning with u"

[bf2538c6-69eb-4fa7-a494-5a3fec911326]
description = "ay is added to words that start with vowels -> word beginning with a vowel and followed by a qu"

[e5be8a01-2d8a-45eb-abb4-3fcc9582a303]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with p"

[d36d1e13-a7ed-464d-a282-8820cb2261ce]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with k"

[d838b56f-0a89-4c90-b326-f16ff4e1dddc]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with x"

[bce94a7a-a94e-4e2b-80f4-b2bb02e40f71]
description = "first letter and ay are moved to the end of words that start with consonants -> word beginning with q without a following u"

[c01e049a-e3e2-451c-bf8e-e2abb7e438b8]
description = "some letter clusters are treated like a single consonant -> word beginning with ch"

[9ba1669e-c43f-4b93-837a-cfc731fd1425]
description = "some letter clusters are treated like a single consonant -> word beginning with qu"

[92e82277-d5e4-43d7-8dd3-3a3b316c41f7]
description = "some letter clusters are treated like a single consonant -> word beginning with qu and a preceding consonant"

[79ae4248-3499-4d5b-af46-5cb05fa073ac]
description = "some letter clusters are treated like a single consonant -> word beginning with th"

[e0b3ae65-f508-4de3-8999-19c2f8e243e1]
description = "some letter clusters are treated like a single consonant -> word beginning with thr"

[20bc19f9-5a35-4341-9d69-1627d6ee6b43]
description = "some letter clusters are treated like a single consonant -> word beginning with sch"

[54b796cb-613d-4509-8c82-8fbf8fc0af9e]
description = "some letter clusters are treated like a single vowel -> word beginning with yt"

[8c37c5e1-872e-4630-ba6e-d20a959b67f6]
description = "some letter clusters are treated like a single vowel -> word beginning with xr"

[a4a36d33-96f3-422c-a233-d4021460ff00]
description = "position of y in a word determines if it is a consonant or a vowel -> y is treated like a consonant at the beginning of a word"

[adc90017-1a12-4100-b595-e346105042c7]
description = "position of y in a word determines if it is a consonant or a vowel -> y is treated like a vowel at the end of a consonant cluster"

[29b4ca3d-efe5-4a95-9a54-8467f2e5e59a]
description = "position of y in a word determines if it is a consonant or a vowel -> y as second letter in two letter word"

[44616581-5ce3-4a81-82d0-40c7ab13d2cf]
description = "phrases are translated -> a whole phrase"
10 changes: 10 additions & 0 deletions exercises/practice/pig-latin/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | ----------------------------- |
# | `$a0` | input | address | null-terminated phrase |
# | `$a1` | input/output | address | null-terminated result string |
# | `$t0-9` | temporary | any | for temporary storage |

.globl translate

translate:
jr $ra
Loading

0 comments on commit 5fa176a

Please sign in to comment.