diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-25 17:33:56 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-25 17:33:56 +0000 |
| commit | 0fdec9c64fad596ea551b4f68ce93c06acd3c27a (patch) | |
| tree | d86d85e2bf5964b5a161d66d29e9e38c9af170dd /challenge-083 | |
| parent | 9c1b461db273795742ed6bf1d358c7982a9def4e (diff) | |
| parent | 84d4eb6d2443b26a7202c7e76ac77cba35d8541a (diff) | |
| download | perlweeklychallenge-club-0fdec9c64fad596ea551b4f68ce93c06acd3c27a.tar.gz perlweeklychallenge-club-0fdec9c64fad596ea551b4f68ce93c06acd3c27a.tar.bz2 perlweeklychallenge-club-0fdec9c64fad596ea551b4f68ce93c06acd3c27a.zip | |
Merge pull request #2613 from wanderdoc/master
Solution to task #1 challenge-083.
Diffstat (limited to 'challenge-083')
| -rw-r--r-- | challenge-083/wanderdoc/perl/ch-1.pl | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/challenge-083/wanderdoc/perl/ch-1.pl b/challenge-083/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..608fff76f0 --- /dev/null +++ b/challenge-083/wanderdoc/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string $S with 3 or more words. Write a script to find the length of the string except the first and last words ignoring whitespace. +Example 1: Input: $S = "The Weekly Challenge" Output: 6 +Example 2: Input: $S = "The purpose of our lives is to be happy" Output: 23 +=cut + + + + + + + +use List::Util qw(reduce); +use Test::More; + +sub words_length +{ + my $string = $_[0]; + my @words = split(/\s+/, $string); + + shift @words; + + pop @words; + + my $length = reduce {$a + $b} map length, @words; + return $length; +} + +is(words_length("The Weekly Challenge"), 6, 'Example 1'); +is(words_length("The purpose of our lives is to be happy"), 23, 'Example 1'); +done_testing();
\ No newline at end of file |
