Skip to content

Add anagram exercise #266

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

Merged
merged 1 commit into from
Jun 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "anagram",
"name": "Anagram",
"uuid": "4172a5bb-0c8e-4a66-9551-8b46acdfc57a",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "diamond",
"name": "Diamond",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/anagram/.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
16 changes: 16 additions & 0 deletions exercises/practice/anagram/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Instructions append

## Input/Output format

Each set of words is represented as a null-terminated string, with a newline character at the end of each word.

An example would be `"tones\nnotes\nSeton\n"`

## Registers

| Register | Usage | Type | Description |
| -------- | ------------ | ------- | -------------------------------------------------------------- |
| `$a0` | input | address | null-terminated target string, without newline |
| `$a1` | input | address | null-terminated candidates string with newline after each word |
| `$a2` | input/output | address | null-terminated output string with newline after each word |
| `$t0-9` | temporary | any | for temporary storage |
13 changes: 13 additions & 0 deletions exercises/practice/anagram/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Your task is to, given a target word and a set of candidate words, to find the subset of the candidates that are anagrams of the target.

An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`.
A word is _not_ its own anagram: for example, `"stop"` is not an anagram of `"stop"`.

The target and candidates are words of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`).
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `StoP` is not an anagram of `sTOp`.
The anagram set is the subset of the candidate set that are anagrams of the target (in any order).
Words in the anagram set should have the same letter case as in the candidate set.

Given the target `"stone"` and candidates `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, `"Seton"`, the anagram set is `"tones"`, `"notes"`, `"Seton"`.
12 changes: 12 additions & 0 deletions exercises/practice/anagram/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Introduction

At a garage sale, you find a lovely vintage typewriter at a bargain price!
Excitedly, you rush home, insert a sheet of paper, and start typing away.
However, your excitement wanes when you examine the output: all words are garbled!
For example, it prints "stop" instead of "post" and "least" instead of "stale."
Carefully, you try again, but now it prints "spot" and "slate."
After some experimentation, you find there is a random delay before each letter is printed, which messes up the order.
You now understand why they sold it for so little money!

You realize this quirk allows you to generate anagrams, which are words formed by rearranging the letters of another word.
Pleased with your finding, you spend the rest of the day generating hundreds of anagrams.
19 changes: 19 additions & 0 deletions exercises/practice/anagram/.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": "Given a word and a list of possible anagrams, select the correct sublist.",
"source": "Inspired by the Extreme Startup game",
"source_url": "https://github.com/rchatley/extreme_startup"
}
112 changes: 112 additions & 0 deletions exercises/practice/anagram/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | -------------------------------------------------------------- |
# | `$a0` | input | address | null-terminated target string, without newline |
# | `$a1` | input | address | null-terminated candidates string with newline after each word |
# | `$a2` | input/output | address | null-terminated output string with newline after each word |
# | `$sp` | stack | address | stack pointer, points to target letter count table |
# | `$a3` | temporary | address | points to candidate letter count table |
# | `$v0` | temporary | address | points to newline or null at end of word |


.macro count_letters (%word, %table)
# %word is address of input word
# %table is address of letter count table to populate
# $v0 is set to adddress of word terminator (\n or \0)
move $v0, %word
addiu $t1, %table, 104

clear:
subiu $t1, $t1, 4
sw $zero, 0($t1)
bne $t1, %table, clear

b read


count:
addiu $v0, $v0, 1 # address of next letter in input word
or $t0, $t0, 32 # force to lower case
subiu $t0, $t0, 'a'
sltiu $t1, $t0, 26 # $t1 is set to 1 if $t0 is alphabetic, otherwise 0
beqz $t1, read # jump to next character if not alphabetic

sll $t0, $t0, 2
add $t1, %table, $t0 # address for letter count
lw $t2, 0($t1)
addi $t2, $t2, 1 # increment letter count
sw $t2, 0($t1)

read:
lb $t0, 0($v0)
bge $t0, 32, count # repeat until end of word
.end_macro


.globl find_anagrams

find_anagrams:
subiu $sp, $sp, 208 # Allocate stack space for letter count tables
addiu $a3, $sp, 104 # Address of candidate letter count table
count_letters ($a0, $sp) # Populate target letter count table
j consider_candidate


copy_next_letter:
addiu $a1, $a1, 1

copy_word:
# Copy word $a1..$v0 inclusive to $a2
lb $t0, 0($a1)
sb $t0, 0($a2)
addiu $a2, $a2, 1
bne $a1, $v0, copy_next_letter

next_candidate:
addiu $a1, $v0, 1
lb $t0, 0($a1)
bnez $t0, consider_candidate

sb $zero, 0($a2) # Write null terminator
addiu $sp, $sp, 208 # Restore original stack pointer
jr $ra

consider_candidate:
count_letters ($a1, $a3)
move $t5, $sp # Pointer into target letter count table
move $t6, $a3 # Pointer into candidate letter count table

compare_count:
lw $t7, 0($t5)
lw $t8, 0($t6)
addiu $t5, $t5, 4 # Increment pointers
addiu $t6, $t6, 4
bne $t7, $t8, next_candidate # Reject candidate if count does not match
bne $t5, $a3, compare_count # Repeat until all 26 counts have been compared

# The letter counts match.
# We now perform a case-insentitive string comparison

move $t5, $a0 # Pointer into target string
move $t6, $a1 # Pointer into candidate string

compare_character:
lb $t7, 0($t5)
lb $t8, 0($t6)
addiu $t5, $t5, 1 # Increment pointers
addiu $t6, $t6, 1

# Force each letter to lowercase

blt $t7, 'A', target_letter_ready
bgt $t7, 'Z', target_letter_ready
addi $t7, $t7, 32

target_letter_ready:
blt $t8, 'A', candidate_letter_ready
bgt $t8, 'Z', candidate_letter_ready
addi $t8, $t8, 32

candidate_letter_ready:
bne $t7, $t8, copy_word # Candidate word is not the same as target word
bne $t6, $v0, compare_character # Repeat until the whole word has been compared
b next_candidate # Skip candidate word, because it is the target word
88 changes: 88 additions & 0 deletions exercises/practice/anagram/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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.

[dd40c4d2-3c8b-44e5-992a-f42b393ec373]
description = "no matches"

[b3cca662-f50a-489e-ae10-ab8290a09bdc]
description = "detects two anagrams"
include = false

[03eb9bbe-8906-4ea0-84fa-ffe711b52c8b]
description = "detects two anagrams"
reimplements = "b3cca662-f50a-489e-ae10-ab8290a09bdc"

[a27558ee-9ba0-4552-96b1-ecf665b06556]
description = "does not detect anagram subsets"

[64cd4584-fc15-4781-b633-3d814c4941a4]
description = "detects anagram"

[99c91beb-838f-4ccd-b123-935139917283]
description = "detects three anagrams"

[78487770-e258-4e1f-a646-8ece10950d90]
description = "detects multiple anagrams with different case"

[1d0ab8aa-362f-49b7-9902-3d0c668d557b]
description = "does not detect non-anagrams with identical checksum"

[9e632c0b-c0b1-4804-8cc1-e295dea6d8a8]
description = "detects anagrams case-insensitively"

[b248e49f-0905-48d2-9c8d-bd02d8c3e392]
description = "detects anagrams using case-insensitive subject"

[f367325c-78ec-411c-be76-e79047f4bd54]
description = "detects anagrams using case-insensitive possible matches"

[7cc195ad-e3c7-44ee-9fd2-d3c344806a2c]
description = "does not detect an anagram if the original word is repeated"
include = false

[630abb71-a94e-4715-8395-179ec1df9f91]
description = "does not detect an anagram if the original word is repeated"
reimplements = "7cc195ad-e3c7-44ee-9fd2-d3c344806a2c"

[9878a1c9-d6ea-4235-ae51-3ea2befd6842]
description = "anagrams must use all letters exactly once"

[85757361-4535-45fd-ac0e-3810d40debc1]
description = "words are not anagrams of themselves (case-insensitive)"
include = false

[68934ed0-010b-4ef9-857a-20c9012d1ebf]
description = "words are not anagrams of themselves"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[589384f3-4c8a-4e7d-9edc-51c3e5f0c90e]
description = "words are not anagrams of themselves even if letter case is partially different"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[ba53e423-7e02-41ee-9ae2-71f91e6d18e6]
description = "words are not anagrams of themselves even if letter case is completely different"
reimplements = "85757361-4535-45fd-ac0e-3810d40debc1"

[a0705568-628c-4b55-9798-82e4acde51ca]
description = "words other than themselves can be anagrams"
include = false

[33d3f67e-fbb9-49d3-a90e-0beb00861da7]
description = "words other than themselves can be anagrams"
reimplements = "a0705568-628c-4b55-9798-82e4acde51ca"

[a6854f66-eec1-4afd-a137-62ef2870c051]
description = "handles case of greek letters"
include = false

[fd3509e5-e3ba-409d-ac3d-a9ac84d13296]
description = "different characters may have the same bytes"
include = false
11 changes: 11 additions & 0 deletions exercises/practice/anagram/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | -------------------------------------------------------------- |
# | `$a0` | input | address | null-terminated target string, without newline |
# | `$a1` | input | address | null-terminated candidates string with newline after each word |
# | `$a2` | input/output | address | null-terminated output string with newline after each word |
# | `$t0-9` | temporary | any | for temporary storage |

.globl find_anagrams

find_anagrams:
jr $ra
Loading