Skip to content

Commit

Permalink
Add anagram (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored May 16, 2024
1 parent 55770e8 commit bd8368f
Show file tree
Hide file tree
Showing 8 changed files with 542 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "anagram",
"name": "Anagram",
"uuid": "ed6857bd-f588-4384-bdf9-6193cf39f21f",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "binary-search",
"name": "Binary Search",
Expand Down
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

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"`.

Given a target word and a set of candidate words, this exercise requests the anagram set: the subset of the candidates that are anagrams of the target.

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"`.
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": [
"BNAndras"
],
"files": {
"solution": [
"anagram.red"
],
"test": [
"anagram-test.red"
],
"example": [
".meta/example.red"
]
},
"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"
}
24 changes: 24 additions & 0 deletions exercises/practice/anagram/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Red [
description: {"Anagram" exercise solution for exercism platform}
author: "BNAndras"
]

find-anagrams: function [
subject
candidates
] [

matches: copy []
subject-lowered: lowercase copy subject
subject-letters: sort copy subject-lowered

foreach candidate candidates [
candidate-lowered: lowercase copy candidate
candidate-letters: sort copy candidate-lowered
if ((subject-lowered <> candidate-lowered) and (subject-letters = candidate-letters)) [
append matches candidate
]
]

matches
]
81 changes: 81 additions & 0 deletions exercises/practice/anagram/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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"

168 changes: 168 additions & 0 deletions exercises/practice/anagram/anagram-test.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
Red [
description: {Tests for "Anagram" Exercism exercise}
author: "loziniak"
]

#include %testlib.red

test-init/limit %anagram.red 1
; test-init/limit %.meta/example.red 1 ; test example solution

canonical-cases: [#[
description: "no matches"
input: #[
subject: "diaper"
candidates: ["hello" "world" "zombies" "pants"]
]
expected: []
function: "find-anagrams"
uuid: "dd40c4d2-3c8b-44e5-992a-f42b393ec373"
] #[
description: "detects two anagrams"
input: #[
subject: "solemn"
candidates: ["lemons" "cherry" "melons"]
]
expected: ["lemons" "melons"]
function: "find-anagrams"
uuid: "03eb9bbe-8906-4ea0-84fa-ffe711b52c8b"
] #[
description: "does not detect anagram subsets"
input: #[
subject: "good"
candidates: ["dog" "goody"]
]
expected: []
function: "find-anagrams"
uuid: "a27558ee-9ba0-4552-96b1-ecf665b06556"
] #[
description: "detects anagram"
input: #[
subject: "listen"
candidates: ["enlists" "google" "inlets" "banana"]
]
expected: ["inlets"]
function: "find-anagrams"
uuid: "64cd4584-fc15-4781-b633-3d814c4941a4"
] #[
description: "detects three anagrams"
input: #[
subject: "allergy"
candidates: ["gallery" "ballerina" "regally" "clergy" "largely" "leading"]
]
expected: ["gallery" "regally" "largely"]
function: "find-anagrams"
uuid: "99c91beb-838f-4ccd-b123-935139917283"
] #[
description: "detects multiple anagrams with different case"
input: #[
subject: "nose"
candidates: ["Eons" "ONES"]
]
expected: ["Eons" "ONES"]
function: "find-anagrams"
uuid: "78487770-e258-4e1f-a646-8ece10950d90"
] #[
description: {does not detect non-anagrams with identical checksum}
input: #[
subject: "mass"
candidates: ["last"]
]
expected: []
function: "find-anagrams"
uuid: "1d0ab8aa-362f-49b7-9902-3d0c668d557b"
] #[
description: "detects anagrams case-insensitively"
input: #[
subject: "Orchestra"
candidates: ["cashregister" "Carthorse" "radishes"]
]
expected: ["Carthorse"]
function: "find-anagrams"
uuid: "9e632c0b-c0b1-4804-8cc1-e295dea6d8a8"
] #[
description: "detects anagrams using case-insensitive subject"
input: #[
subject: "Orchestra"
candidates: ["cashregister" "carthorse" "radishes"]
]
expected: ["carthorse"]
function: "find-anagrams"
uuid: "b248e49f-0905-48d2-9c8d-bd02d8c3e392"
] #[
description: {detects anagrams using case-insensitive possible matches}
input: #[
subject: "orchestra"
candidates: ["cashregister" "Carthorse" "radishes"]
]
expected: ["Carthorse"]
function: "find-anagrams"
uuid: "f367325c-78ec-411c-be76-e79047f4bd54"
]#[
description: {does not detect an anagram if the original word is repeated}
input: #[
subject: "go"
candidates: ["goGoGO"]
]
expected: []
function: "find-anagrams"
uuid: "630abb71-a94e-4715-8395-179ec1df9f91"
] #[
description: "anagrams must use all letters exactly once"
input: #[
subject: "tapper"
candidates: ["patter"]
]
expected: []
function: "find-anagrams"
uuid: "9878a1c9-d6ea-4235-ae51-3ea2befd6842"
] #[
description: "words are not anagrams of themselves"
input: #[
subject: "BANANA"
candidates: ["BANANA"]
]
expected: []
function: "find-anagrams"
uuid: "68934ed0-010b-4ef9-857a-20c9012d1ebf"
] #[
description: {words are not anagrams of themselves even if letter case is partially different}
input: #[
subject: "BANANA"
candidates: ["Banana"]
]
expected: []
function: "find-anagrams"
uuid: "589384f3-4c8a-4e7d-9edc-51c3e5f0c90e"
] #[
description: {words are not anagrams of themselves even if letter case is completely different}
input: #[
subject: "BANANA"
candidates: ["banana"]
]
expected: []
function: "find-anagrams"
uuid: "ba53e423-7e02-41ee-9ae2-71f91e6d18e6"
] #[
description: "words other than themselves can be anagrams"
input: #[
subject: "LISTEN"
candidates: ["LISTEN" "Silent"]
]
expected: ["Silent"]
function: "find-anagrams"
uuid: "33d3f67e-fbb9-49d3-a90e-0beb00861da7"
]]


foreach c-case canonical-cases [
case-code: reduce [
'expect c-case/expected compose [
(to word! c-case/function) (values-of c-case/input)
]
]

test c-case/description case-code
]

test-results/print
12 changes: 12 additions & 0 deletions exercises/practice/anagram/anagram.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Red [
description: {"Anagram" exercise solution for exercism platform}
author: "" ; you can write your name here, in quotes
]

find-anagrams: function [
subject
candidates
] [
cause-error 'user 'message "You need to implement find-anagrams function."
]

Loading

0 comments on commit bd8368f

Please sign in to comment.