Camel
Peter
Peter Campbell Smith

Acronyms and FS

Weekly challenge 317 — 14 April 2025

Week 317: 14 Apr 2025

Task 1

Task — Acronyms

You are given an array of words and a word. Write a script to return true if concatenating the first letter of each word in the given array matches the given word, and return false otherwise.

Examples


Example 1
Input: @array = ('Perl', 'Weekly', 'Challenge')
       $word  = 'PWC'
Output: true

Example 2
Input: @array = ('Bob', 'Charlie', 'Joe')
       $word  = 'BCJ'
Output: true

Example 3
Input: @array = ('Morning', 'Good')
       $word  = 'MM'
Output: false

Analysis

One line to concatenate the initial letters of the array elements and that's more-or-less it.

I helped in the development of POLARIS (see examples below), which was perhaps the best acronym I came across in my career.

Others will no doubt point out that a set of initials isn't strictly an acronym unless it is a pronounceable word such as RADAR or NATO, but I think they've lost that battle.

Try it 

Try running the script with any input:



example: Every good boy deserves fun



example: EGBDF

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2025-04-14
use utf8;     # Week 317 - task 1 - Acronyms
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

acronyms([qw(produces every result lovingly)], 'perl');
acronyms([qw(this one is not true)], 'false');
acronyms([qw(true results usually exist)], 'true');
acronyms([qw(Port of London Authority River Information System)], 'POLARIS');

sub acronyms {
    
    my ($input, @words, $acronym, $concat);

    # intialise
    @words = @{$_[0]};
    $acronym = $_[1];
    
    # generate acronym from words
    $concat .= substr($words[$_], 0, 1) for 0 .. $#words;

    # report result
    say qq[\nInput:  \@words = ('] . join(q[', '], @words) . qq['), \$acronym = '$acronym'];
    say qq[Output: ] . (lc($concat) eq lc($acronym) ? 'true' : 'false');
}

Output


Input:  @words = ('produces', 'every', 'result',
   'lovingly'), $acronym = 'perl'
Output: true

Input:  @words = ('this', 'one', 'is', 'not', 'true'),
   $acronym = 'false'
Output: false

Input:  @words = ('true', 'results', 'usually', 'exist'),
   $acronym = 'true'
Output: true

Input:  @words = ('Port', 'of', 'London', 'Authority',
   'River', 'Information', 'System'), $acronym = 'POLARIS'
Output: true

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain