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