diff options
| -rw-r--r-- | challenge-321/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-321/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-321/jeanluc2020/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-321/jeanluc2020/perl/ch-2.pl | 65 |
4 files changed, 146 insertions, 0 deletions
diff --git a/challenge-321/jeanluc2020/blog-1.txt b/challenge-321/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..f028be7fb9 --- /dev/null +++ b/challenge-321/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-321-1.html diff --git a/challenge-321/jeanluc2020/blog-2.txt b/challenge-321/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..d756a1af35 --- /dev/null +++ b/challenge-321/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-321-2.html diff --git a/challenge-321/jeanluc2020/perl/ch-1.pl b/challenge-321/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..dbf0de9aa9 --- /dev/null +++ b/challenge-321/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/#TASK1 +# +# Task 1: Distinct Average +# ======================== +# +# 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. +# +############################################################ +## +## discussion +## +############################################################ +# +# At first, we sort the array. Then we can always remove the last +# and first element via pop() and shift() until there are no more +# elements in the array. Of course we calculate the medium value at +# each step and take note of it so in the end we can have the number +# of distinct averages. + +use v5.36; +use List::Util qw(sum); + +distinct_average(1, 2, 4, 3, 5, 6); +distinct_average(0, 2, 4, 8, 3, 5); +distinct_average(7, 3, 1, 0, 5, 9); + +sub distinct_average(@nums) { + say "Input: (" . join(", ", @nums) . ")"; + my @sorted = sort { $a <=> $b } @nums; + my $avg = {}; + while(scalar(@sorted)) { + my $s = sum(@sorted); + my $average = $s / scalar(@sorted); + $avg->{$average} = 1; + pop @sorted; + shift @sorted; + } + my $count = scalar(keys(%$avg)); + say "Output: $count"; +} diff --git a/challenge-321/jeanluc2020/perl/ch-2.pl b/challenge-321/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..c3ec495718 --- /dev/null +++ b/challenge-321/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-321/#TASK2 +# +# Task 2: Backspace Compare +# ========================= +# +# 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 +# +############################################################ +## +## discussion +## +############################################################ +# +# As long as there's a # in the word following another character, we replce +# both the # character and the character before it. Once done on both characters, +# a simple string comparison shows whether the two strings are equal. + +use v5.36; + +backspace_compare( "ab#c", "ad#c" ); +backspace_compare( "ab##", "a#b#" ); +backspace_compare( "a#b", "c"); + +sub backspace_compare($str1, $str2) { + say "\$str1 = $str1, \$str2 = $str2"; + while($str1 =~ m/[^#]#/) { + $str1 =~ s/[^#]#//; + } + while($str2 =~ m/[^#]#/) { + $str2 =~ s/[^#]#//; + } + if($str1 eq $str2) { + say "Output: true"; + } else { + say "Output: false"; + } +} |
