Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Acronym in the Perl 6 Track

Instructions
Test suite
Solution

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

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 prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

Source

Julien Vanier https://github.com/monkbroc

Submitting Incomplete Solutions

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

acronym.t

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

my Version:D $version = v2;

if Acronym.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nAcronym is {Acronym.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;

for $c-data<cases>»<cases>».Array.flat {
  is abbreviate(.<input><phrase>), |.<expected description>;
}

=head2 Canonical Data
=begin code
{
  "exercise": "acronym",
  "version": "1.3.0",
  "cases": [
    {
      "description": "Abbreviate a phrase",
      "cases": [
        {
          "description": "basic",
          "property": "abbreviate",
          "input": {
            "phrase": "Portable Network Graphics"
          },
          "expected": "PNG"
        },
        {
          "description": "lowercase words",
          "property": "abbreviate",
          "input": {
            "phrase": "Ruby on Rails"
          },
          "expected": "ROR"
        },
        {
          "description": "punctuation",
          "property": "abbreviate",
          "input": {
            "phrase": "First In, First Out"
          },
          "expected": "FIFO"
        },
        {
          "description": "all caps word",
          "property": "abbreviate",
          "input": {
            "phrase": "GNU Image Manipulation Program"
          },
          "expected": "GIMP"
        },
        {
          "description": "punctuation without whitespace",
          "property": "abbreviate",
          "input": {
            "phrase": "Complementary metal-oxide semiconductor"
          },
          "expected": "CMOS"
        }
      ]
    }
  ]
}
=end code
unit module Acronym:ver<2>;

sub abbreviate ($phrase) is export {
    $phrase.uc.comb(/\w+/).map(*.substr(0, 1)).join
}

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?