Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Anagram in the Perl 6 Track

0
0
Genius
0
0
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove6 . in the exercise directory.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

anagram.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use Anagram;
plan 12;

my $c-data = from-json $=pod.pop.contents;
cmp-ok match-anagrams( |%(.<input><subject candidates>:p) ), '~~', .<expected>.Set, .<description> for $c-data<cases>.values;

=head2 Canonical Data
=begin code
{
  "exercise": "anagram",
  "version": "1.2.0",
  "comments": [
    "The string argument cases possible matches are passed in as",
    "individual arguments rather than arrays. Languages can include",
    "these string argument cases if passing individual arguments is",
    "idiomatic in that language."
  ],
  "cases": [
    {
      "description": "no matches",
      "property": "anagrams",
      "input": {
        "subject": "diaper",
        "candidates": ["hello", "world", "zombies", "pants"]
      },
      "expected": []
    },
    {
      "description": "detects two anagrams",
      "property": "anagrams",
      "input": {
        "subject": "master",
        "candidates": ["stream", "pigeon", "maters"]
      },
      "expected": ["stream", "maters"]
    },
    {
      "description": "does not detect anagram subsets",
      "property": "anagrams",
      "input": {
        "subject": "good",
        "candidates": ["dog", "goody"]
      },
      "expected": []
    },
    {
      "description": "detects anagram",
      "property": "anagrams",
      "input": {
        "subject": "listen",
        "candidates": ["enlists", "google", "inlets", "banana"]
      },
      "expected": ["inlets"]
    },
    {
      "description": "detects three anagrams",
      "property": "anagrams",
      "input": {
        "subject": "allergy",
        "candidates": [
          "gallery",
          "ballerina",
          "regally",
          "clergy",
          "largely",
          "leading"
        ]
      },
      "expected": ["gallery", "regally", "largely"]
    },
    {
      "description": "does not detect non-anagrams with identical checksum",
      "property": "anagrams",
      "input": {
        "subject": "mass",
        "candidates": ["last"]
      },
      "expected": []
    },
    {
      "description": "detects anagrams case-insensitively",
      "property": "anagrams",
      "input": {
        "subject": "Orchestra",
        "candidates": ["cashregister", "Carthorse", "radishes"]
      },
      "expected": ["Carthorse"]
    },
    {
      "description": "detects anagrams using case-insensitive subject",
      "property": "anagrams",
      "input": {
        "subject": "Orchestra",
        "candidates": ["cashregister", "carthorse", "radishes"]
      },
      "expected": ["carthorse"]
    },
    {
      "description": "detects anagrams using case-insensitive possible matches",
      "property": "anagrams",
      "input": {
        "subject": "orchestra",
        "candidates": ["cashregister", "Carthorse", "radishes"]
      },
      "expected": ["Carthorse"]
    },
    {
      "description": "does not detect a anagram if the original word is repeated",
      "property": "anagrams",
      "input": {
        "subject": "go",
        "candidates": ["go Go GO"]
      },
      "expected": []
    },
    {
      "description": "anagrams must use all letters exactly once",
      "property": "anagrams",
      "input": {
        "subject": "tapper",
        "candidates": ["patter"]
      },
      "expected": []
    },
    {
      "description": "capital word is not own anagram",
      "property": "anagrams",
      "input": {
        "subject": "BANANA",
        "candidates": ["Banana"]
      },
      "expected": []
    }
  ]
}
=end code
unit class Anagram;

method match(Str $word, Array $words --> Array) {
    $words.grep( { .lc.comb.Bag ~~ $word.lc.comb.Bag and .lc ne $word.lc } ).Array
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?