diff --git a/config.json b/config.json index 87a05ba..4564d7e 100644 --- a/config.json +++ b/config.json @@ -187,6 +187,14 @@ "prerequisites": [], "difficulty": 3 }, + { + "slug": "anagram", + "name": "Anagram", + "uuid": "70e71054-0af0-45c8-918e-b987d555ca78", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "robot-simulator", "name": "Robot Simulator", diff --git a/exercises/practice/anagram/.docs/instructions.md b/exercises/practice/anagram/.docs/instructions.md new file mode 100644 index 0000000..7d1c828 --- /dev/null +++ b/exercises/practice/anagram/.docs/instructions.md @@ -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"`. diff --git a/exercises/practice/anagram/.meta/config.json b/exercises/practice/anagram/.meta/config.json new file mode 100644 index 0000000..7aa4fe4 --- /dev/null +++ b/exercises/practice/anagram/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glaxxie" + ], + "files": { + "solution": [ + "anagram.gd" + ], + "test": [ + "anagram_test.gd" + ], + "example": [ + ".meta/example.gd" + ] + }, + "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" +} diff --git a/exercises/practice/anagram/.meta/example.gd b/exercises/practice/anagram/.meta/example.gd new file mode 100644 index 0000000..8c5eee1 --- /dev/null +++ b/exercises/practice/anagram/.meta/example.gd @@ -0,0 +1,13 @@ +func find_anagram(word, candidates): + var result = [] + var base_list = word.to_lower().split() + base_list.sort() + + for obj in candidates: + if word.to_lower() == obj.to_lower(): + continue + var char_list = obj.to_lower().split() + char_list.sort() + if char_list == base_list: + result.append(obj) + return result diff --git a/exercises/practice/anagram/.meta/tests.toml b/exercises/practice/anagram/.meta/tests.toml new file mode 100644 index 0000000..4d90562 --- /dev/null +++ b/exercises/practice/anagram/.meta/tests.toml @@ -0,0 +1,86 @@ +# 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" + +[fd3509e5-e3ba-409d-ac3d-a9ac84d13296] +description = "different characters may have the same bytes" diff --git a/exercises/practice/anagram/anagram.gd b/exercises/practice/anagram/anagram.gd new file mode 100644 index 0000000..3acc9d9 --- /dev/null +++ b/exercises/practice/anagram/anagram.gd @@ -0,0 +1,2 @@ +func find_anagram(word, candidates): + pass diff --git a/exercises/practice/anagram/anagram_test.gd b/exercises/practice/anagram/anagram_test.gd new file mode 100644 index 0000000..8e69454 --- /dev/null +++ b/exercises/practice/anagram/anagram_test.gd @@ -0,0 +1,118 @@ +func test_no_matches(solution_script): + var subject = "diaper" + var candidates = ["hello", "world", "zombies", "pants"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_two_anagrams(solution_script): + var subject = "solemn" + var candidates = ["lemons", "cherry", "melons"] + var expected = ["lemons", "melons"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_does_not_detect_anagram_subsets(solution_script): + var subject = "listen" + var candidates = ["enlists", "google", "inlets", "banana"] + var expected = ["inlets"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_three_anagrams(solution_script): + var subject = "allergy" + var candidates = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"] + var expected = ["gallery", "regally", "largely"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_multiple_anagrams_with_different_case(solution_script): + var subject = "nose" + var candidates = ["Eons", "ONES"] + var expected = ["Eons", "ONES"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_does_not_detect_non_anagrams_with_identical_checksum(solution_script): + var subject = "mass" + var candidates = ["last"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_anagrams_case_insensitively(solution_script): + var subject = "Orchestra" + var candidates = ["cashregister", "Carthorse", "radishes"] + var expected = ["Carthorse"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_anagrams_using_case_insensitive_subject(solution_script): + var subject = "Orchestra" + var candidates = ["cashregister", "carthorse", "radishes"] + var expected = ["carthorse"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_detects_anagrams_using_case_insensitive_possible_matches(solution_script): + var subject = "orchestra" + var candidates = ["cashregister", "Carthorse", "radishes"] + var expected = ["Carthorse"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_does_not_detect_an_anagram_if_the_original_word_is_repeated(solution_script): + var subject = "go" + var candidates = ["goGoGO"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_anagrams_must_use_all_letters_exactly_once(solution_script): + var subject = "tapper" + var candidates = ["patter"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_words_are_not_anagrams_of_themselves(solution_script): + var subject = "BANANA" + var candidates = ["BANANA"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different(solution_script): + var subject = "BANANA" + var candidates = ["Banana"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different(solution_script): + var subject = "BANANA" + var candidates = ["banana"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_words_other_than_themselves_can_be_anagrams(solution_script): + var subject = "LISTEN" + var candidates = ["LISTEN", "Silent"] + var expected = ["Silent"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_handles_case_of_greek_letters(solution_script): + var subject = "ΑΒΓ" + var candidates = ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"] + var expected = ["ΒΓΑ", "γβα"] + return [solution_script.find_anagram(subject, candidates), expected] + + +func test_different_characters_may_have_the_same_bytes(solution_script): + var subject = "a⬂" + var candidates = ["€a"] + var expected = [] + return [solution_script.find_anagram(subject, candidates), expected] +