perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 319 - Task 1: Word Count

 1 #!/usr/bin/env perl
 2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-319/#TASK1
 3 #
 4 # Task 1: Word Count
 5 # ==================
 6 #
 7 # You are given a list of words containing alphabetic characters only.
 8 #
 9 # Write a script to return the count of words either starting with a vowel or
10 # ending with a vowel.
11 #
12 ## Example 1
13 ##
14 ## Input: @list = ("unicode", "xml", "raku", "perl")
15 ## Output: 2
16 ##
17 ## The words are "unicode" and "raku".
18 #
19 #
20 ## Example 2
21 ##
22 ## Input: @list = ("the", "weekly", "challenge")
23 ## Output: 2
24 #
25 #
26 ## Example 3
27 ##
28 ## Input: @list = ("perl", "python", "postgres")
29 ## Output: 0
30 #
31 ############################################################
32 ##
33 ## discussion
34 ##
35 ############################################################
36 #
37 # We just count the words that start or end in a vowel.
38 # There is one possible variation in case we want to do an xor instead
39 # of an or, in that case change the regular expression to
40 # /(^[aeiouAEIOU].*[^aeiouAEIOU]$|^[^aeiouAEIOU].*[aeiouAEIOU]$)/
41 # This doesn't make a difference in the examples, but for
42 # words like "oxide".
43 
44 use v5.36;
45 
46 word_count("unicode", "xml", "raku", "perl");
47 word_count("the", "weekly", "challenge");
48 word_count("perl", "python", "postgres");
49 
50 sub word_count( @list ) {
51     say "Input: (\"" . join("\", \"", @list) . ")";
52     my $count = 0;
53     foreach my $elem (@list) {
54         $count++ if $elem =~ m/(^[aeiouAEIOU].*|.*[aeiouAEIOU]$)/;
55     }
56     say "Output: $count";
57 }
58