diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2023-01-31 20:53:13 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2023-01-31 20:53:13 +0100 |
| commit | 0c9c3f511e440098678ee2fefd3e61f4388adc39 (patch) | |
| tree | bae32784e361235d1a113c877ef376d6ba63309d /challenge-202 | |
| parent | 81e796219fe7ddaf5f50e72a0702297a2dee6db7 (diff) | |
| download | perlweeklychallenge-club-0c9c3f511e440098678ee2fefd3e61f4388adc39.tar.gz perlweeklychallenge-club-0c9c3f511e440098678ee2fefd3e61f4388adc39.tar.bz2 perlweeklychallenge-club-0c9c3f511e440098678ee2fefd3e61f4388adc39.zip | |
Add solutions for challenge 202
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
Diffstat (limited to 'challenge-202')
| -rw-r--r-- | challenge-202/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-202/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-202/jeanluc2020/perl/ch-1.pl | 79 | ||||
| -rwxr-xr-x | challenge-202/jeanluc2020/perl/ch-2.pl | 108 |
4 files changed, 189 insertions, 0 deletions
diff --git a/challenge-202/jeanluc2020/blog-1.txt b/challenge-202/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..8d7e1dc3f7 --- /dev/null +++ b/challenge-202/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-202-1.html diff --git a/challenge-202/jeanluc2020/blog-2.txt b/challenge-202/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..0c24c259a8 --- /dev/null +++ b/challenge-202/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-202-2.html diff --git a/challenge-202/jeanluc2020/perl/ch-1.pl b/challenge-202/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..7cd6363572 --- /dev/null +++ b/challenge-202/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-202/#TASK1 +# +# You are given an array of integers. +# +# Write a script to print 1 if there are THREE consecutive odds in the given array otherwise print 0. +# +## Example 1 +## +## Input: @array = (1,5,3,6) +## Output: 1 +# +## Example 2 +## +## Input: @array = (2,6,3,5) +## Output: 0 +# +## Example 3 +## +## Input: @array = (1,2,3,4) +## Output: 0 +# +## Example 4 +## +## Input: @array = (2,3,5,7) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# basically we need to walk the array from left to right and +# count consecutive odds +# The way the task is set this doesn't mean there need to be +# EXACT 3 consecutive odds, but AT LEAST 3 consecutive odds, +# which makes the solution even easier, as we can short-curcuit +# the execution once we found 3 consecutive odds + +use strict; +use warnings; +use feature 'say'; + +my @examples = ( + [1,5,3,6], + [2,6,3,5], + [1,2,3,4], + [2,3,5,7], + [1,2,3,4,5,7,9,11] +); + +foreach my $array (@examples) { + check_three_consecutive_odds(@$array); +} + + +sub check_three_consecutive_odds { + my @array = @_; + my $consecutive_odds = 0; + foreach my $elem (@array) { + if($elem % 2) { + # odd number + $consecutive_odds++; + if($consecutive_odds >= 3) { + # we have at least 3 consecutive odds, so + # we're finished + say "(" . join(", ", @array) . ") has output 1"; + return; + } + } else { + # even number, start over at 0 + $consecutive_odds = 0; + } + } + # we didn't find 3 consecutive odds anywhere + say "(" . join(", ", @array) . ") has output 0"; +} + diff --git a/challenge-202/jeanluc2020/perl/ch-2.pl b/challenge-202/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..e3e5b40999 --- /dev/null +++ b/challenge-202/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,108 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-202/#TASK2 +# +# Given a profile as a list of altitudes, return the leftmost widest valley. A +# valley is defined as a subarray of the profile consisting of two parts: the +# first part is non-increasing and the second part is non-decreasing. Either +# part can be empty. +# +## Example 1 +## +## Input: 1, 5, 5, 2, 8 +## Output: 5, 5, 2, 8 +# +## Example 2 +## +## Input: 2, 6, 8, 5 +## Output: 2, 6, 8 +# +## Example 3 +## +## Input: 9, 8, 13, 13, 2, 2, 15, 17 +## Output: 13, 13, 2, 2, 15, 17 +# +## Example 4 +## +## Input: 2, 1, 2, 1, 3 +## Output: 2, 1, 2 +# +## Example 5 +## +## Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +## Output: 3, 3, 2, 1, 2, 3, 3 +# +############################################################ +## +## discussion +## +############################################################ +# +# Each position in the array is also the beginning of a valley. So basically we +# have to find the longest valley that starts in each position in the array, +# and then select the longest valley overall (and the leftmost of those if +# multiple valleys are of the same length). + +use strict; +use warnings; +use feature 'say'; + +my @examples = ( + [1, 5, 5, 2, 8], + [2, 6, 8, 5], + [9, 8, 13, 13, 2, 2, 15, 17], + [2, 1, 2, 1, 3], + [1, 3, 3, 2, 1, 2, 3, 3, 2] +); + +foreach my $array (@examples) { + say "Input: " . join(", ", @$array); + say "Output: " . join(", ", get_longest_valley(@$array)); +} + +sub get_longest_valley { + my @array = @_; + my @longest = (); + foreach my $index (0..$#array) { + # get the longest valley starting at the current index + my @valley = get_valley(@array[$index..$#array]); + # if this valley is longer than the longest so far, we have a + # new longest valley + if($#valley > $#longest) { + @longest = @valley; + } + } + return @longest; +} + +# get the longest valley at the beginning of the given array +sub get_valley { + my @array = @_; + # we start off as non-increasing + my $non_increasing = 1; + my @result = (); + # let's just grab the first element + # of the array and put it into the result + my $last = shift @array; + push @result, $last; + foreach my $elem (@array) { + # if we're still in the non-increasing part, we + # switch to non-decreasing if the current element + # is bigger than the last one + if($non_increasing) { + if($elem > $last) { + $non_increasing = 0; + } + push @result, $elem; + $last = $elem; + } else { # non-decreasing part + # if we have a smaller value than the last, we have + # found the end of valley and can return it + if($elem < $last) { + return @result; + } + push @result, $elem; + $last = $elem; + } + } + return @result; +} |
