From c269bd6c4378561b3f34967ba39d2928f5c8f2e7 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Fri, 29 Nov 2024 14:44:58 -0500 Subject: [PATCH] Add acronym exercise (#704) * Add acronym exercise * remove variable length lookbehind --- config.json | 8 +++ .../practice/acronym/.docs/instructions.md | 17 +++++ exercises/practice/acronym/.meta/config.json | 19 ++++++ .../acronym/.meta/solutions/lib/Acronym.pm | 18 ++++++ .../acronym/.meta/solutions/t/acronym.t | 1 + .../practice/acronym/.meta/template-data.yaml | 26 ++++++++ exercises/practice/acronym/.meta/tests.toml | 37 +++++++++++ exercises/practice/acronym/lib/Acronym.pm | 12 ++++ exercises/practice/acronym/t/acronym.t | 63 +++++++++++++++++++ 9 files changed, 201 insertions(+) create mode 100644 exercises/practice/acronym/.docs/instructions.md create mode 100644 exercises/practice/acronym/.meta/config.json create mode 100644 exercises/practice/acronym/.meta/solutions/lib/Acronym.pm create mode 120000 exercises/practice/acronym/.meta/solutions/t/acronym.t create mode 100644 exercises/practice/acronym/.meta/template-data.yaml create mode 100644 exercises/practice/acronym/.meta/tests.toml create mode 100644 exercises/practice/acronym/lib/Acronym.pm create mode 100755 exercises/practice/acronym/t/acronym.t diff --git a/config.json b/config.json index 5fd332f7..54733667 100644 --- a/config.json +++ b/config.json @@ -148,6 +148,14 @@ ], "difficulty": 1 }, + { + "slug": "acronym", + "name": "Acronym", + "uuid": "79fce6b7-5af9-4ee5-bd41-743ecc00cebc", + "practices": [], + "prerequisites": [], + "difficulty": 3 + }, { "slug": "all-your-base", "name": "All Your Base", diff --git a/exercises/practice/acronym/.docs/instructions.md b/exercises/practice/acronym/.docs/instructions.md new file mode 100644 index 00000000..133bd2cb --- /dev/null +++ b/exercises/practice/acronym/.docs/instructions.md @@ -0,0 +1,17 @@ +# Instructions + +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). + +Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input. + +For example: + +| Input | Output | +| ------------------------- | ------ | +| As Soon As Possible | ASAP | +| Liquid-crystal display | LCD | +| Thank George It's Friday! | TGIF | diff --git a/exercises/practice/acronym/.meta/config.json b/exercises/practice/acronym/.meta/config.json new file mode 100644 index 00000000..59a131dc --- /dev/null +++ b/exercises/practice/acronym/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "lib/Acronym.pm" + ], + "test": [ + "t/acronym.t" + ], + "example": [ + ".meta/solutions/lib/Acronym.pm" + ] + }, + "blurb": "Convert a long phrase to its acronym.", + "source": "Julien Vanier", + "source_url": "https://github.com/monkbroc" +} diff --git a/exercises/practice/acronym/.meta/solutions/lib/Acronym.pm b/exercises/practice/acronym/.meta/solutions/lib/Acronym.pm new file mode 100644 index 00000000..fb2100d3 --- /dev/null +++ b/exercises/practice/acronym/.meta/solutions/lib/Acronym.pm @@ -0,0 +1,18 @@ +package Acronym; + +use strict; +use warnings; +use experimental qw; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub abbreviate ($phrase) { + + # capture letters preceded by the start of the string + # or by a non-(letter or apostrophe) + my @letters = $phrase =~ /(?:^|[^'a-z])\K([a-z])/ig; + return uc join "", @letters; +} + +1; diff --git a/exercises/practice/acronym/.meta/solutions/t/acronym.t b/exercises/practice/acronym/.meta/solutions/t/acronym.t new file mode 120000 index 00000000..cafef07b --- /dev/null +++ b/exercises/practice/acronym/.meta/solutions/t/acronym.t @@ -0,0 +1 @@ +../../../t/acronym.t \ No newline at end of file diff --git a/exercises/practice/acronym/.meta/template-data.yaml b/exercises/practice/acronym/.meta/template-data.yaml new file mode 100644 index 00000000..a9c09061 --- /dev/null +++ b/exercises/practice/acronym/.meta/template-data.yaml @@ -0,0 +1,26 @@ +subs: abbreviate + +properties: + abbreviate: + test: |- + use Data::Dmp; + sprintf(<<'END', map {dmp($_)} $case->{input}{phrase}, $case->{expected}, $case->{description}); + is( + abbreviate( %s ), + %s, + %s, + ); + END + +example: |- + sub abbreviate ($phrase) { + # capture letters preceded by the start of the string + # or by a non-(letter or apostrophe) + my @letters = $phrase =~ /(?:^|[^'a-z])\K([a-z])/ig; + return uc join "", @letters; + } + +stub: |- + sub abbreviate ($phrase) { + return undef; + } diff --git a/exercises/practice/acronym/.meta/tests.toml b/exercises/practice/acronym/.meta/tests.toml new file mode 100644 index 00000000..6e3277c6 --- /dev/null +++ b/exercises/practice/acronym/.meta/tests.toml @@ -0,0 +1,37 @@ +# 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. + +[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4] +description = "basic" + +[79ae3889-a5c0-4b01-baf0-232d31180c08] +description = "lowercase words" + +[ec7000a7-3931-4a17-890e-33ca2073a548] +description = "punctuation" + +[32dd261c-0c92-469a-9c5c-b192e94a63b0] +description = "all caps word" + +[ae2ac9fa-a606-4d05-8244-3bcc4659c1d4] +description = "punctuation without whitespace" + +[0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9] +description = "very long abbreviation" + +[6a078f49-c68d-4b7b-89af-33a1a98c28cc] +description = "consecutive delimiters" + +[5118b4b1-4572-434c-8d57-5b762e57973e] +description = "apostrophes" + +[adc12eab-ec2d-414f-b48c-66a4fc06cdef] +description = "underscore emphasis" diff --git a/exercises/practice/acronym/lib/Acronym.pm b/exercises/practice/acronym/lib/Acronym.pm new file mode 100644 index 00000000..810c24b1 --- /dev/null +++ b/exercises/practice/acronym/lib/Acronym.pm @@ -0,0 +1,12 @@ +package Acronym; + +use v5.40; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub abbreviate ($phrase) { + return undef; +} + +1; diff --git a/exercises/practice/acronym/t/acronym.t b/exercises/practice/acronym/t/acronym.t new file mode 100755 index 00000000..2ec5beee --- /dev/null +++ b/exercises/practice/acronym/t/acronym.t @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +use Test2::V0; + +use FindBin qw<$Bin>; +use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; + +use Acronym qw; + +is( # begin: 1e22cceb-c5e4-4562-9afe-aef07ad1eaf4 + abbreviate("Portable Network Graphics"), + "PNG", + "basic", +); # end: 1e22cceb-c5e4-4562-9afe-aef07ad1eaf4 + +is( # begin: 79ae3889-a5c0-4b01-baf0-232d31180c08 + abbreviate("Ruby on Rails"), + "ROR", + "lowercase words", +); # end: 79ae3889-a5c0-4b01-baf0-232d31180c08 + +is( # begin: ec7000a7-3931-4a17-890e-33ca2073a548 + abbreviate("First In, First Out"), + "FIFO", + "punctuation", +); # end: ec7000a7-3931-4a17-890e-33ca2073a548 + +is( # begin: 32dd261c-0c92-469a-9c5c-b192e94a63b0 + abbreviate("GNU Image Manipulation Program"), + "GIMP", + "all caps word", +); # end: 32dd261c-0c92-469a-9c5c-b192e94a63b0 + +is( # begin: ae2ac9fa-a606-4d05-8244-3bcc4659c1d4 + abbreviate("Complementary metal-oxide semiconductor"), + "CMOS", + "punctuation without whitespace", +); # end: ae2ac9fa-a606-4d05-8244-3bcc4659c1d4 + +is( # begin: 0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9 + abbreviate("Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"), + "ROTFLSHTMDCOALM", + "very long abbreviation", +); # end: 0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9 + +is( # begin: 6a078f49-c68d-4b7b-89af-33a1a98c28cc + abbreviate("Something - I made up from thin air"), + "SIMUFTA", + "consecutive delimiters", +); # end: 6a078f49-c68d-4b7b-89af-33a1a98c28cc + +is( # begin: 5118b4b1-4572-434c-8d57-5b762e57973e + abbreviate("Halley's Comet"), + "HC", + "apostrophes", +); # end: 5118b4b1-4572-434c-8d57-5b762e57973e + +is( # begin: adc12eab-ec2d-414f-b48c-66a4fc06cdef + abbreviate("The Road _Not_ Taken"), + "TRNT", + "underscore emphasis", +); # end: adc12eab-ec2d-414f-b48c-66a4fc06cdef + +done_testing;