From 84d4eb6d2443b26a7202c7e76ac77cba35d8541a Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Sun, 25 Oct 2020 18:02:27 +0100 Subject: Solution to task #1 challenge-083. --- challenge-083/wanderdoc/perl/ch-1.pl | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-083/wanderdoc/perl/ch-1.pl 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 -- cgit