diff options
| -rw-r--r-- | challenge-321/wanderdoc/perl/ch-1.pl | 69 | ||||
| -rw-r--r-- | challenge-321/wanderdoc/perl/ch-2.pl | 67 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-321/wanderdoc/perl/ch-1.pl b/challenge-321/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..39c0b925d1 --- /dev/null +++ b/challenge-321/wanderdoc/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array of numbers with even length. + +Write a script to return the count of distinct average. The average is calculate by removing the minimum and the maximum, then average of the two. + +Example 1 + +Input: @nums = (1, 2, 4, 3, 5, 6) +Output: 1 + +Step 1: Min = 1, Max = 6, Avg = 3.5 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 1. + + +Example 2 + +Input: @nums = (0, 2, 4, 8, 3, 5) +Output: 2 + +Step 1: Min = 0, Max = 8, Avg = 4 +Step 2: Min = 2, Max = 5, Avg = 3.5 +Step 3: Min = 3, Max = 4, Avg = 3.5 + +The count of distinct average is 2. + + +Example 3 + +Input: @nums = (7, 3, 1, 0, 5, 9) +Output: 2 + +Step 1: Min = 0, Max = 9, Avg = 4.5 +Step 2: Min = 1, Max = 7, Avg = 4 +Step 3: Min = 3, Max = 5, Avg = 4 + +The count of distinct average is 2. +=cut + + +use List::Util qw(sum); +use Test2::V0 -no_srand => 1; + + +is(distinct_average(1, 2, 4, 3, 5, 6), 1, 'Example 1'); +is(distinct_average(0, 2, 4, 8, 3, 5), 2, 'Example 2'); +is(distinct_average(7, 3, 1, 0, 5, 9), 2, 'Example 3'); +done_testing(); + + +sub distinct_average +{ + my @arr = sort { $a <=> $b } @_; + my %averages; + while (scalar(@arr)) + { + my $avg = sum(@arr)/scalar(@arr); + $averages{$avg} = undef; + shift @arr; + pop @arr if ( scalar @arr ); + } + return scalar keys %averages; +} diff --git a/challenge-321/wanderdoc/perl/ch-2.pl b/challenge-321/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..1178b50f10 --- /dev/null +++ b/challenge-321/wanderdoc/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given two strings containing zero or more #. +Write a script to return true if the two given strings are same by treating # as backspace. + +Example 1 +Input: $str1 = "ab#c" + $str2 = "ad#c" +Output: true + +For first string, we remove "b" as it is followed by "#". +For second string, we remove "d" as it is followed by "#". +In the end both strings became the same. + + +Example 2 + +Input: $str1 = "ab##" + $str2 = "a#b#" +Output: true + + +Example 3 + +Input: $str1 = "a#b" + $str2 = "c" +Output: false +=cut + + + +use constant { true => 1, false => 0 }; +use Test2::V0 -no_srand => 1; + + + + +is(backspace_compare("ab#c", "ad#c"), true, 'Example 1'); +is(backspace_compare("ab##", "a#b#"), true, 'Example 2'); +is(backspace_compare("a#b", "c"), false, 'Example 3'); + +done_testing(); + +sub backspace_compare +{ + my @strings = @_; + for my $str ( @strings ) + { + my $output = ''; + for my $chr ( split(//, $str) ) + { + if ( $chr eq '#' ) + { + substr($output, -1) = ''; + } + else + { + $output .= $chr; + } + } + $str = $output; + } + return $strings[0] eq $strings[1] ? true : false; +} |
