The weekly challenge 240 - Task 1: Acronym

 1 #!/usr/bin/perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-240/#TASK1
 3 #
 4 # Task 1: Acronym
 5 # ===============
 6 #
 7 # You are given an array of strings and a check string.
 8 #
 9 # Write a script to find out if the check string is the acronym of the words in
10 # the given array.
11 #
12 ## Example 1
13 ##
14 ## Input: @str = ("Perl", "Python", "Pascal")
15 ##        $chk = "ppp"
16 ## Output: true
17 #
18 ## Example 2
19 ##
20 ## Input: @str = ("Perl", "Raku")
21 ##        $chk = "rp"
22 ## Output: false
23 #
24 ## Example 3
25 ##
26 ## Input: @str = ("Oracle", "Awk", "C")
27 ##        $chk = "oac"
28 ## Output: true
29 #
30 ############################################################
31 ##
32 ## discussion
33 ##
34 ############################################################
35 #
36 # This one is straight forward:
37 # - get the first character of each string in the array
38 # - lowercase it
39 # - join all of these together into a string
40 # - compare that string to $chk
41 
42 use strict;
43 use warnings;
44 
45 acronym( ["Perl", "Python", "Pascal"], "ppp");
46 acronym( ["Perl", "Raku"], "rp");
47 acronym( ["Oracle", "Awk", "C"], "oac");
48 
49 sub acronym {
50    my ($strings, $chk) = @_;
51    print "Input: (" . join(", ", @$strings) . "), \"$chk\"\n";
52    my $str = join("", map { lc(substr($_,0,1)) } @$strings);
53    if($str eq $chk) {
54       print "Output: true\n";
55    } else {
56       print "Output: false\n";
57    }
58 }
59