diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-16 04:05:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-16 04:05:38 +0100 |
| commit | 9ce30897312aeea2953c8ecae23f13350e2b293b (patch) | |
| tree | 488fb82e7501f5d06f0e3db4fc2191b8aa71b3ad | |
| parent | 846ea11091bcbe5840cf1fc5366de12952fca6f1 (diff) | |
| parent | 5de6f923a82f7263022771ce186714993d3415e2 (diff) | |
| download | perlweeklychallenge-club-9ce30897312aeea2953c8ecae23f13350e2b293b.tar.gz perlweeklychallenge-club-9ce30897312aeea2953c8ecae23f13350e2b293b.tar.bz2 perlweeklychallenge-club-9ce30897312aeea2953c8ecae23f13350e2b293b.zip | |
Merge pull request #2079 from simongreen-net/swg-073
Challenge 73 solution by Simon
| -rw-r--r-- | challenge-073/sgreen/README.md | 39 | ||||
| -rw-r--r-- | challenge-073/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-073/sgreen/perl/ch-1.pl | 30 | ||||
| -rwxr-xr-x | challenge-073/sgreen/perl/ch-2.pl | 25 |
4 files changed, 71 insertions, 24 deletions
diff --git a/challenge-073/sgreen/README.md b/challenge-073/sgreen/README.md index 6dec32d966..a272d6ba1c 100644 --- a/challenge-073/sgreen/README.md +++ b/challenge-073/sgreen/README.md @@ -1,39 +1,30 @@ -# Perl Weekly Challenge 072 +# Perl Weekly Challenge 073 Solution by Simon Green. -No solutions for last week as I was moving interstate. I'm back on board this week :) +## TASK #1 › Min Sliding Window -## TASK #1 › Trailing Zeroes +I try not to use CPAN modules, since there is no guarantee that the user running the code has it installed. However, [List::Util](https://metacpan.org/pod/List::Util) is part of the perl core these days, so it should be safe. -When you think about it, a trailing zero is a product of 10, which is 2 × 5. Forgetting about the twos (since every second number is even), the five occurs every 5 numbers. Thus the factorial from 1 to 4 will contain no trailing zeros, 5-9 one trailing zero, 10-14 three trailing zeros, and so on. Once you reach 25, it gets a little more complicated as 25 is 5 × 5. Anyway, I digress from the actual task. +The hardest part of the challenge was taking a string and converting it into an array of integers. Originally I had `split /[^\d\-]+/`, but this didn't work as expected as the first and value would be `undef` if the first character was not a number. Nothing that a quick [search on Stack Overflow](https://stackoverflow.com/questions/2884549/how-can-i-capture-multiple-matches-from-the-same-perl-regex) doesn't fix :) -For this task, I take the input, calculate the factorial value, I then use a regular expression to find the trailing zeros (if any), and then display it. +Once the input is processed, I then work through the array and get the [minimum value](https://metacpan.org/pod/List::Util#min) for each subset of the array. ### Examples - » ./ch-1.pl 10 - 2 + » ./ch-1.pl "(1, 5, 0, 2, 9, 3, 7, 6, 4, 8)" 3 + 0, 0, 0, 2, 3, 3, 4, 4 - » ./ch-1.pl 7 - 1 +## TASK 2 › Smallest Neighbour - » ./ch-1.pl 4 - 0 +Originally I didn't understand what was required, so I reached out to Mohammad to clarify the task. In particular, I couldn't understand how the third value in the first example was '0'. I should have read the task more clearly. -## TASK 2 › Lines Range - -This tasks was relatively simple. Read the file, skip the lines < `$A` and exit the loop once line `$B` is reached (or the end of file). +Like with the first task, I walked through the list. If the current value was less than the previous minimum values, I would add a '0' to the result and set the new `$current_min` value. If it was not less, I would add the `$current_min` value to the result. ## Example - » ./ch-2.pl input.txt 4 12 - L4 - L5 - L6 - L7 - L8 - L9 - L10 - L11 - L12
\ No newline at end of file + » ./ch-2.pl 7 8 3 10 12 + 0, 7, 0, 3, 3 + + » ./ch-2.pl 4 6 5 + 0, 4, 4
\ No newline at end of file diff --git a/challenge-073/sgreen/blog.txt b/challenge-073/sgreen/blog.txt new file mode 100644 index 0000000000..4ddb372089 --- /dev/null +++ b/challenge-073/sgreen/blog.txt @@ -0,0 +1 @@ +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-073/sgreen/README.md diff --git a/challenge-073/sgreen/perl/ch-1.pl b/challenge-073/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..b842ec6d2f --- /dev/null +++ b/challenge-073/sgreen/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use List::Util 'min'; +use 5.10.1; + +sub main (@) { + my ( $string, $s ) = @_; + + # Convert the first value into a list of integers + my @list = ( $string =~ /(-?\d+)/g ); + my @result = (); + + # Sanity checks + die "The first value must be a list of intergers" unless scalar(@list); + die "The second value must be a postive integer\n" unless $s =~ /^[0-9]+$/; + die "The second value must be at least the number of items in the list\n" + if $s >= scalar(@list); + + # Go through the list, and calculate the minimum value + foreach my $i ( 0 .. scalar(@list) - $s ) { + push @result, min( @list[ $i .. $i + $s - 1 ] ); + } + + # Display the result + say join ', ', @result; +} + +main(@ARGV); diff --git a/challenge-073/sgreen/perl/ch-2.pl b/challenge-073/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..48544a5ff9 --- /dev/null +++ b/challenge-073/sgreen/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use 5.10.1; + +sub main (@) { + my @list = @_; + my $current_min = $list[0]; + + my @result = (0); + foreach my $i ( 1 .. $#list ) { + if ( $list[$i] < $current_min ) { + push @result, 0; + $current_min = $list[$i]; + } + else { + push @result, $current_min; + } + } + + say join ', ', @result; +} + +main(@ARGV); |
