From 391da064891c3a64b1cf8d2c9a4ebb8aeb39efbe Mon Sep 17 00:00:00 2001 From: aut0exec <32361472+aut0exec@users.noreply.github.com> Date: Mon, 23 Jan 2023 20:47:54 -0600 Subject: Task 1 solution --- challenge-201/aut0exec/perl/Task1.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-201/aut0exec/perl/Task1.pl diff --git a/challenge-201/aut0exec/perl/Task1.pl b/challenge-201/aut0exec/perl/Task1.pl new file mode 100644 index 0000000000..5cec6c95bb --- /dev/null +++ b/challenge-201/aut0exec/perl/Task1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl +# +# You are given an array of unique numbers. +# Write a script to find out all missing numbers in the range 0..$n where $n is the array size. +# + +use strict; +use warnings; + +#~ my @num_array = (2, 1, 4, 3); +my @num_array = (0, 1, 2, 3, 4); +my $array_len = scalar(@num_array); +my %array_hash = map { $_ => 1 } @num_array; + +print("Array length is: $array_len \n"); + +foreach ( 0..$array_len ){ + #~ print ("Checking $_ \n"); + if (! exists($array_hash{$_})){ + print("Array is missing $_!\n"); + last; + } +} -- cgit From 0393906906fe7d945a9dfeefeccf62779005dc65 Mon Sep 17 00:00:00 2001 From: aut0exec <32361472+aut0exec@users.noreply.github.com> Date: Mon, 23 Jan 2023 20:50:00 -0600 Subject: Task 1 remove debug lines --- challenge-201/aut0exec/perl/Task1.pl | 1 - 1 file changed, 1 deletion(-) diff --git a/challenge-201/aut0exec/perl/Task1.pl b/challenge-201/aut0exec/perl/Task1.pl index 5cec6c95bb..c2a127b36b 100644 --- a/challenge-201/aut0exec/perl/Task1.pl +++ b/challenge-201/aut0exec/perl/Task1.pl @@ -15,7 +15,6 @@ my %array_hash = map { $_ => 1 } @num_array; print("Array length is: $array_len \n"); foreach ( 0..$array_len ){ - #~ print ("Checking $_ \n"); if (! exists($array_hash{$_})){ print("Array is missing $_!\n"); last; -- cgit From 8d47db3feb21a8675d4f14a64a854a0552969555 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Sat, 28 Jan 2023 01:32:57 -0500 Subject: Challenge 200 by Jaldhar H. Vyas. --- challenge-200/jaldhar-h-vyas/blog.txt | 1 + challenge-200/jaldhar-h-vyas/perl/ch-1.pl | 31 ++++++++++++++++++++++ challenge-200/jaldhar-h-vyas/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++ challenge-200/jaldhar-h-vyas/raku/ch-1.raku | 26 ++++++++++++++++++ challenge-200/jaldhar-h-vyas/raku/ch-2.raku | 39 +++++++++++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100644 challenge-200/jaldhar-h-vyas/blog.txt create mode 100755 challenge-200/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-200/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-200/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-200/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-200/jaldhar-h-vyas/blog.txt b/challenge-200/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..abcfa82f59 --- /dev/null +++ b/challenge-200/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/01/perl_weekly_challenge_week_200.html \ No newline at end of file diff --git a/challenge-200/jaldhar-h-vyas/perl/ch-1.pl b/challenge-200/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..894989ea7f --- /dev/null +++ b/challenge-200/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @array = @ARGV; + +my @results = (); +@array = sort { $a <=> $b } @array; + +for my $i (0 .. scalar @array - 3) { + slice: for my $j ($i + 2 .. scalar @array - 1) { + my @slice = @array[$i .. $j]; + my $diff = $slice[1] - $slice[0]; + for my $k (2 .. scalar @slice - 1) { + if ($slice[$k] - $slice[$k - 1] != $diff) { + last slice; + } + } + push @results, \@slice; + } +} + +say q{(}, +( + join q{), (}, + ( + map { join q{, }, @{$_} } sort { scalar @{$a} <=> scalar @{$b} } + @results + ) +), +q{)}; diff --git a/challenge-200/jaldhar-h-vyas/perl/ch-2.pl b/challenge-200/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..bec294ea66 --- /dev/null +++ b/challenge-200/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my $number = shift // die "need an integer\n"; +my @truth = qw/ abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg /; +my @segments = ( + [qw/ - a a a a a a a /], + [qw/ - F - - - - - B /], + [qw/ - F - - - - - B /], + [qw/ - g g g g g g g /], + [qw/ - E - - - - - C /], + [qw/ - E - - - - - C /], + [qw/ - d d d d d d d /], +); +my @template = map { $truth[$_]; } split //, $number; +my $offset = 0; +my @result; +for my $i (0 .. 6) { + for my $j (0 .. (length $number) * 8 - 1) { + $result[$i][$j] = q{ }; + } +} + +for my $digit (@template) { + for my $segment (split //, $digit) { + for my $row (0 .. 6) { + for my $column (0 .. 7) { + if (lc $segments[$row][$column] eq $segment) { + $result[$row][$offset + $column] = + ($segments[$row][$column] =~ /[A-Z]/) ? q{|} : q{-}; + } + } + } + } + $offset += 8; +} + +for my $i (0 .. 6) { + say join q{}, @{$result[$i]}; +} diff --git a/challenge-200/jaldhar-h-vyas/raku/ch-1.raku b/challenge-200/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..f2992680c1 --- /dev/null +++ b/challenge-200/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/raku + +sub MAIN( + *@array +) { + my @results = (); + @array = @array.sort({ $^a <=> $^b }); + + for 0 .. @array.end - 2 -> $i { + slice: for $i + 2 .. @array.end -> $j { + my @slice = @array[$i .. $j]; + my $diff = @slice[1] - @slice[0]; + for 2 .. @slice.end -> $k { + if @slice[$k] - @slice[$k - 1] != $diff { + last slice; + } + } + @results.push(@slice); + } + } + + say q{(}, + @results.sort({ $^a.elems <=> $^b.elems }).map({ @$_.join(q{, }) }) + .join(q{), (}), + q{)}; +} diff --git a/challenge-200/jaldhar-h-vyas/raku/ch-2.raku b/challenge-200/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..e39a530d70 --- /dev/null +++ b/challenge-200/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,39 @@ +#!/usr/bin/raku + +sub MAIN( + Int $number #= an integer +) { + + my @truth = ; + my @segments = ( + < - a a a a a a a >, + < - F - - - - - B >, + < - F - - - - - B >, + < - g g g g g g g >, + < - E - - - - - C >, + < - E - - - - - C >, + < - d d d d d d d >, + ); + + my @template = $number.comb.map({ @truth[$_] }); + my $offset = 0; + my @result = [ q{ } xx $number.chars * 8 ] xx 7; + + for @template -> $digit { + for $digit.comb -> $segment { + for 0 .. 6 -> $row { + for 0 .. 7 -> $column { + if @segments[$row][$column].lc eq $segment { + @result[$row][$offset + $column] = + (@segments[$row][$column] ~~ 'A' .. 'Z') ?? q{|} !! q{-}; + } + } + } + } + $offset += 8; + } + + for 0 .. 6 -> $i { + say @result[$i].join(q{}); + } +} \ No newline at end of file -- cgit From 8ed85a5f531dfd432aae700661fcc7415bea4fc3 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Mon, 30 Jan 2023 10:20:17 +0700 Subject: pwc201/go --- challenge-201/pokgopun/go/ch-2.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/challenge-201/pokgopun/go/ch-2.go b/challenge-201/pokgopun/go/ch-2.go index 4c7b355988..50ace14d7d 100644 --- a/challenge-201/pokgopun/go/ch-2.go +++ b/challenge-201/pokgopun/go/ch-2.go @@ -56,7 +56,15 @@ func main() { func penny(m map[string]struct{}, n ...uint64) int { last := n[len(n)-1] - if last == 0 && len(n) > 1 { + if last > 0 { + s := make([]uint64, len(n)+1) + copy(s, n) + for i := uint64(1); i <= last; i++ { + s[len(s)-2] = i + s[len(s)-1] = last - i + penny(m, s...) + } + } else if len(n) > 1 { sort.SliceStable(n, func(i, j int) bool { return n[i] < n[j] }) @@ -65,14 +73,6 @@ func penny(m map[string]struct{}, n ...uint64) int { b.WriteString(strconv.FormatUint(v, 10) + " ") } m[b.String()] = struct{}{} - } else { - s := make([]uint64, len(n)+1) - copy(s, n) - for i := uint64(1); i <= last; i++ { - s[len(s)-2] = i - s[len(s)-1] = last - i - penny(m, s...) - } } return len(m) } -- cgit From ba9d81a08e32764433aeac56eca15a42bf01296b Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Mon, 30 Jan 2023 12:48:39 +0700 Subject: pwc201/go --- challenge-201/pokgopun/go/ch-2.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/challenge-201/pokgopun/go/ch-2.go b/challenge-201/pokgopun/go/ch-2.go index 50ace14d7d..b112a34289 100644 --- a/challenge-201/pokgopun/go/ch-2.go +++ b/challenge-201/pokgopun/go/ch-2.go @@ -28,7 +28,6 @@ import ( "io" "log" "os" - "sort" "strconv" "strings" ) @@ -44,35 +43,30 @@ func main() { log.Fatal(err) } } - m := make(map[string]struct{}) - - fmt.Printf("Input: n = %d\nOutput: %d\n\nThere are %[2]d ways of stacking %[1]d pennies in ascending piles\n\n", n, penny(m, n)) var b strings.Builder - for k := range m { - b.WriteString("\t" + k + "\n") - } + fmt.Printf("Input: n = %d\nOutput: %d\n\nThere are %[2]d ways of stacking %[1]d pennies in ascending piles\n\n", n, penny(&b, n)) io.WriteString(os.Stdout, b.String()) } -func penny(m map[string]struct{}, n ...uint64) int { +func penny(b *strings.Builder, n ...uint64) int { last := n[len(n)-1] if last > 0 { s := make([]uint64, len(n)+1) copy(s, n) - for i := uint64(1); i <= last; i++ { + var first uint64 = 1 + if len(n) > 1 { + first = n[len(n)-2] + } + for i := first; i <= last; i++ { s[len(s)-2] = i s[len(s)-1] = last - i - penny(m, s...) + penny(b, s...) } } else if len(n) > 1 { - sort.SliceStable(n, func(i, j int) bool { - return n[i] < n[j] - }) - var b strings.Builder - for _, v := range n[1:] { + for _, v := range n[:len(n)-1] { b.WriteString(strconv.FormatUint(v, 10) + " ") } - m[b.String()] = struct{}{} + b.WriteRune('\n') } - return len(m) + return strings.Count(b.String(), "\n") } -- cgit From ed7f997e089890fe77a0f23fd151bf8595c5e7d1 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 30 Jan 2023 06:54:00 +0000 Subject: Create blog.txt --- challenge-202/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-202/james-smith/blog.txt diff --git a/challenge-202/james-smith/blog.txt b/challenge-202/james-smith/blog.txt new file mode 100644 index 0000000000..68f402558d --- /dev/null +++ b/challenge-202/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-202/james-smith -- cgit From 753f031b29712659d56956fdef30bc5ad7d25c1d Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 30 Jan 2023 06:54:45 +0000 Subject: Create ch-1.pl --- challenge-202/james-smith/perl/ch-1.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-202/james-smith/perl/ch-1.pl diff --git a/challenge-202/james-smith/perl/ch-1.pl b/challenge-202/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..b9dce5c04f --- /dev/null +++ b/challenge-202/james-smith/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [1,5,3,6], 1 ], + [ [2,6,3,5], 0 ], + [ [1,2,3,4], 0 ], + [ [2,3,5,7], 1 ], +); + +is( odd3( @{$_->[0]}), $_->[1] ) for @TESTS; +done_testing(); + +sub odd3 { + return 0 unless @_>2; + my $p = shift, my $q = shift; + $p&$q&$_[0]&1 ? (return 1) : (($p,$q)=($q,shift)) while @_; + 0; +} -- cgit From f69dfd30a71872a68ecddcdc932f24855860867e Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 30 Jan 2023 06:55:17 +0000 Subject: Create ch-2.pl --- challenge-202/james-smith/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-202/james-smith/perl/ch-2.pl diff --git a/challenge-202/james-smith/perl/ch-2.pl b/challenge-202/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..73ff683c1b --- /dev/null +++ b/challenge-202/james-smith/perl/ch-2.pl @@ -0,0 +1,30 @@ +!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; +use List::MoreUtils qw(slide); + +my @TESTS = ( + [ [1,5,5,2,8], '5 5 2 8' ], #1 0 -1 1 + [ [2,6,8,5], '2 6 8' ], # 1 1 -1 + [ [9,8,13,13,2,2,15,17], '13 13 2 2 15 17' ], # 0 1 0 -1 0 1 1 + [ [2,1,2,1,3], '2 1 2'], # -1 1 -1 1 + [ [1,3,3,2,1,2,3,3,2],'3 3 2 1 2 3 3' ], # 1 0 -1 -1 1 1 0 -1 + [ [2,3,2,1], '3 2 1' ], # 1 -1 -1 +); +is( "@{[ valley( @{$_->[0]} ) ]}", $_->[1] ) for @TESTS2; +done_testing(); + +sub valley { + my( $L, $R, @d )=( 0, 0, slide { $b <=> $a } @_ ); + for my $l ( 0 .. $#d-1) { + for my $r ( $l+1 .. $#d ) { + my($x,$t) = ( $d[$l] || -1, $l ); + $d[$t] && ( $d[$t]<$x ? last : ($x=$d[$t]) ) while $t++ < $r; + ($L,$R)=($l,$t) if $R-$L<$t-$l; + } + } + @_[$L..$R]; +} -- cgit From 821e504e733812f690134841920f388959f1942a Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 08:18:31 +0100 Subject: Task 1 done --- challenge-202/luca-ferrari/raku/ch-1.p6 | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-202/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-202/luca-ferrari/raku/ch-1.p6 b/challenge-202/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..1224d1b9ae --- /dev/null +++ b/challenge-202/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!raku + +# +# Perl Weekly Challenge 202 +# Task 1 +# +# See +# + +sub MAIN( Bool :$verbose = False, + *@list where { @list.grep( { $_ ~~ Int && $_ > 0 } ).elems == @list.elems } ) { + my @odds; + for @list { + next if $_ %% 2; + @odds.push: $_ and next if ( ! @odds ); + next if @odds.grep( $_ ); + next if $_ != ( @odds[ * - 1 ] + 2 ); + @odds.push: $_; + } + + @odds.join( ', ' ).say if $verbose; + '1'.say and exit if ( @odds.elems >= 3 ); + '0'.say; +} -- cgit From 7ce9f7c638244fdd17b7b8a6456dcbbdf2183226 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 30 Jan 2023 07:32:21 +0000 Subject: Update README.md --- challenge-202/james-smith/README.md | 128 +++++++++--------------------------- 1 file changed, 30 insertions(+), 98 deletions(-) diff --git a/challenge-202/james-smith/README.md b/challenge-202/james-smith/README.md index d8445e1324..c9b082532f 100644 --- a/challenge-202/james-smith/README.md +++ b/challenge-202/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 200](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-200/james-smith) | -[Next 202 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-202/james-smith) +[< Previous 201](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-201/james-smith) | +[Next 203 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-203/james-smith) -# The Weekly Challenge 201 +# The Weekly Challenge 202 You can find more information about this weeks, and previous weeks challenges at: @@ -13,121 +13,53 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-201/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-202/james-smith -# Task 1: Missing Numbers +# Task 1: Consecutive Odds -***You are given an array of unique numbers. Write a script to find out all missing numbers in the range 0..$n where $n is the array size.*** +***You are given an array of integers. Write a script to print `1` if there are **THREE** consecutive odss in the given array otherwise print `0`.*** ## Solution -First we note for the problem as defined there will only ever be one missing number. +We have to loop through a window of size 3 and check all three values are odd. We can do this with a single check. The 3 values will be ODD if and only if they all have the "0" bit set. So we can binary `&` them together and check the last bit by anding with `1`. -Second we note if we add the numbers up we have a total of `n(n+1)/2 - {missing number}`. - -This leads us to two quite compact solutions: +So we loop through - check this is the case - and if not move to the next value. ```perl -sub missing { my $t = @_*(@_+1)/2; $t-=$_ for @_; $t } -sub missing_sum { @_*(@_+1)/2 - sum0 @_ } +sub odd3 { + return 0 unless @_>2; + my $p = shift, my $q = shift; + $p&$q&$_[0]&1 ? (return 1) : (($p,$q)=($q,shift)) while @_; + 0; +} ``` -Where we take `sum0` from `List::Util`. - -### Performance - -We compared the two methods - in each case the `sum0` solution was faster - we will come back to this in task 2! +# Task 2:Widest Valley -What was interesting was relativee performance *vs* size of list. For short lists, the gain was a modest 25-30%, for medium size lists about 1,000 - 10,000 the gain was around 350%, but for larger lists again 100,000+ this dropped back down to 150-160%. This is probably the overhead of passing arrays around. - -# Task 2: Penny Piles - -***You are given an integer, `$n > 0`. Write a script to determine the number of ways of putting `$n` pennies in a row of piles of ascending heights from left to right.*** +***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.*** ## Solution -This is a simple recursive search at any point we have `n` coins which we have to layout with a maximum height of `m` - we start where `m` is equal `n`. - -For performance sake we will cache our results as there are lots of cache hits!. - -So if `n` is 0 then we have found a solution - so we count 1, otherwise we make each possible sized pile and pass back to the function.... - -This gives us: - -```perl -sub piles { - my($count,$n,$m)=(0,@_); - return 1 unless $n; ## We have found *A* solution - $m//=$n; ## First time we don't pass in $m - - ## so set it to $n - return $cache{"$n,$m"} if exists $cache{"$n,$m"}; ## If we have seen this combo return - $count += piles($n-$_,$_) for 1 .. ($m>$n?$n:$m); ## Otherwise loop through possible coin - ## counts and add a stack of that size - ## this is limited by (a) the number of - ## coins and (b) the height of the - ## previous stack. - $cache{"$n,$m"} ||= $count; ## cache result and return -} -``` - -Now we always like to simplify things - and in this case we return a cache value if -exists AND then compute the value and cache it - and return the cache value. +So we need a decreasing sequence followed by an increasing sequence. -This is because we have to run a for loop to do the sum. If we can avoid that then we -can simplify the code... +Firstly we convert the valley heights into a string of difference (or more precisely whether that difference is `+1`, `-1` or `0`. We can use `<=>` to do that. We use `List::MoreUtils`'s `slide` method to do this. -Method (1) We replace the summation by a second function which encapsulates that -value. +We look for a sequence which only contains `-1`s before `+1`s. -Method (2) We use `sum0` from `List::Util` -```perl -sub piles_2 { - my($count,$n,$m)=(0,@_); - return 1 unless $n; - $m//=$n; - $cache{"$n,$m"}//= sum_piles_2( $n, $m ); -} +We loop through all possible starts and see what the longest valley is. -sub sum_piles_2 { - my $count = 0; - $count += piles_2($_[0]-$_,$_) for 1 .. ($_[1]>$_[0]?$_[0]:$_[1]); - $count; -} - -sub piles_0 { - return 1 unless $_[0]; - $_[1]//=$_[0]; - $cache{"@_"}//= sum0 map { piles_0( $_[0]-$_,$_ ) } 1 .. ($_[0]>$_[1]?$_[1]:$_[0]); -} -``` - -Finally we try a further method without recursion but using a stack. Now this is much -harder to write - and also impossible to write a sensible cacheing algorithm for. We -will see that this is a bad idea in a moment: +Finally we return that interval. ```perl -sub piles_q { - my($count,$n,@q,$v)=(0,$_[0],[1,$_[0]]); - while($v = shift @q) { - $count++ if $v->[1]>=$v->[0]; - push @q, map { [$_,$v->[1]-$_] } $v->[0]..$v->[1]-1; +sub valley { + my( $L, $R, @d )=( 0, 0, slide { $b <=> $a } @_ ); + for my $l ( 0 .. $#d-1) { + my($x,$t) = ( $d[$l] || -1, $l ); + while($t++<$#d) { + $d[$t] && ( $d[$t]<$x ? last : ($x=$d[$t]) ) + } + ($L,$R)=($l,$t) if $R-$L<$t-$l; } - $count; + @_[$L..$R]; } ``` - -### Performance - -We got some results we didn't expect! So for our test sets (`$n` ranging from 5 to 50) we got: - -| method | runs per s | Rel performance | -| :--------- | ---------: | --------------: | -| piles_q | 0.482/s | 0.002 x | -| piles_0 | 253/s | 0.842 x | -| piles | 301/s | 1.000 x | -| piles_2 | 326/s | 1.114 x | - -We note that the queue method is difficult to write and optimal solution for... But the other three methods we get subtly different performances. - -Although in task 1 using `sum0` was better than not this time it's worse. Why is this? not sure - but it may be that in task 1 we summing an actual array but in this -case we are summing a list {the result of the `map`}. The two function method `piles_2` was slightly faster - I think because of merging the test for existance and allocation of the value to the cache using `//=`. -- cgit From 7b5970ded1466f0d928d45298793762a140b5775 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 30 Jan 2023 07:32:30 +0000 Subject: Update ch-2.pl --- challenge-202/james-smith/perl/ch-2.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-202/james-smith/perl/ch-2.pl b/challenge-202/james-smith/perl/ch-2.pl index 73ff683c1b..329d09863b 100644 --- a/challenge-202/james-smith/perl/ch-2.pl +++ b/challenge-202/james-smith/perl/ch-2.pl @@ -20,11 +20,11 @@ done_testing(); sub valley { my( $L, $R, @d )=( 0, 0, slide { $b <=> $a } @_ ); for my $l ( 0 .. $#d-1) { - for my $r ( $l+1 .. $#d ) { - my($x,$t) = ( $d[$l] || -1, $l ); - $d[$t] && ( $d[$t]<$x ? last : ($x=$d[$t]) ) while $t++ < $r; - ($L,$R)=($l,$t) if $R-$L<$t-$l; + my($x,$t) = ( $d[$l] || -1, $l ); + while($t++<$#d) { + $d[$t] && ( $d[$t]<$x ? last : ($x=$d[$t]) ) } + ($L,$R)=($l,$t) if $R-$L<$t-$l; } @_[$L..$R]; } -- cgit From 84debecccbeed32d55565cfb62abf09a58a89f7e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 09:04:48 +0100 Subject: Task 2 done --- challenge-202/luca-ferrari/raku/ch-2.p6 | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 challenge-202/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-202/luca-ferrari/raku/ch-2.p6 b/challenge-202/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..7a270c28b5 --- /dev/null +++ b/challenge-202/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,38 @@ +#!raku + +# +# Perl Weekly Challenge 202 +# Task 2 +# +# See +# + +sub MAIN( *@list where { @list.grep( { $_ > 0 && $_ ~~ Int } ).elems == @list.elems } ) { + + my %valleys; + + for 0 ..^ @list.elems - 1 { + my $current = @list[ $_ ]; + next if @list[ $_ + 1 ] > $current; # increasing! + + my @valley-left; + for $_ ..^ @list.elems { + @valley-left.push: @list[ $_ ] if ( @list[ $_ ] <= $current ); + last if @list[ $_ ] > $current; + } + + my @valley-right; + $current = @list[ $_ + @valley-left.elems ]; + if ( $_ + @valley-left.elems < @list.elems ) { + for $_ + @valley-left.elems ..^ @list.elems { + @valley-right.push: @list[ $_ ] if ( @list[ $_ ] >= $current ); + last if @list[ $_ ] < $current; + } + } + + %valleys{ @valley-left.elems + @valley-right.elems } = [ |@valley-left, |@valley-right ]; + } + + %valleys{ %valleys.keys.max }.join( ', ' ).say; + +} -- cgit From ceed87ea8af7c1fe69ca21169e0df51eb30a9e7c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 09:24:16 +0100 Subject: Task 1 plperl --- challenge-202/luca-ferrari/postgresql/ch-1.plperl | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-202/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-202/luca-ferrari/postgresql/ch-1.plperl b/challenge-202/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..96f16598d5 --- /dev/null +++ b/challenge-202/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,28 @@ +-- +-- Perl Weekly Challenge 202 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc202; + +CREATE OR REPLACE FUNCTION +pwc202.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $list ) = @_; + my @odds; + + for ( $list->@* ) { + next if $_ % 2 == 0; + + push( @odds, $_ ) and next if ! @odds; + next if $_ != ( $odds[ -1 ] + 2 ); + push( @odds, $_ ); + } + + return 1 if @odds >= 3; + return 0; + +$CODE$ +LANGUAGE plperl; -- cgit From 0bb490b80fd3cb06607e85875d6170d32eac500d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 09:49:19 +0100 Subject: Fixed errors in task 2 --- challenge-202/luca-ferrari/raku/ch-2.p6 | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/challenge-202/luca-ferrari/raku/ch-2.p6 b/challenge-202/luca-ferrari/raku/ch-2.p6 index 7a270c28b5..bb9bc30b42 100644 --- a/challenge-202/luca-ferrari/raku/ch-2.p6 +++ b/challenge-202/luca-ferrari/raku/ch-2.p6 @@ -11,22 +11,26 @@ sub MAIN( *@list where { @list.grep( { $_ > 0 && $_ ~~ Int } ).elems == @list.el my %valleys; - for 0 ..^ @list.elems - 1 { - my $current = @list[ $_ ]; - next if @list[ $_ + 1 ] > $current; # increasing! + for 0 ..^ @list.elems - 1 -> $index { + my $current = @list[ $index ]; + next if @list[ $index + 1 ] > $current; # increasing! my @valley-left; - for $_ ..^ @list.elems { - @valley-left.push: @list[ $_ ] if ( @list[ $_ ] <= $current ); - last if @list[ $_ ] > $current; + @valley-left.push: $current; + for $index ^..^ @list.elems { + my $previous = @valley-left[ * - 1 ]; + @valley-left.push: @list[ $_ ] if ( @list[ $_ ] <= $previous ); + last if @list[ $_ ] > $previous; } my @valley-right; - $current = @list[ $_ + @valley-left.elems ]; - if ( $_ + @valley-left.elems < @list.elems ) { - for $_ + @valley-left.elems ..^ @list.elems { - @valley-right.push: @list[ $_ ] if ( @list[ $_ ] >= $current ); - last if @list[ $_ ] < $current; + $current = @list[ $index + @valley-left.elems ]; + if ( $index + @valley-left.elems < @list.elems ) { + @valley-right.push: @list[ $index + @valley-left.elems ]; + for $index + @valley-left.elems ^..^ @list.elems { + my $previous = @valley-right[ * - 1 ]; + @valley-right.push: @list[ $_ ] if ( @list[ $_ ] >= $previous ); + last if @list[ $_ ] < $previous; } } -- cgit From 6088216d26f2a4a38775af92e5f826c99de4eb39 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 10:02:45 +0100 Subject: Task 2 plperl done --- challenge-202/luca-ferrari/raku/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-202/luca-ferrari/raku/ch-2.p6 b/challenge-202/luca-ferrari/raku/ch-2.p6 index bb9bc30b42..4fc4c5452e 100644 --- a/challenge-202/luca-ferrari/raku/ch-2.p6 +++ b/challenge-202/luca-ferrari/raku/ch-2.p6 @@ -24,7 +24,7 @@ sub MAIN( *@list where { @list.grep( { $_ > 0 && $_ ~~ Int } ).elems == @list.el } my @valley-right; - $current = @list[ $index + @valley-left.elems ]; + if ( $index + @valley-left.elems < @list.elems ) { @valley-right.push: @list[ $index + @valley-left.elems ]; for $index + @valley-left.elems ^..^ @list.elems { -- cgit From a3574bb79c1a246e200dffbd87520f481d4be086 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 10:11:04 +0100 Subject: Task 2 plperl --- challenge-202/luca-ferrari/postgresql/ch-2.plperl | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-202/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-202/luca-ferrari/postgresql/ch-2.plperl b/challenge-202/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..d8beda06ef --- /dev/null +++ b/challenge-202/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,49 @@ +-- +-- Perl Weekly Challenge 202 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc202; + +CREATE OR REPLACE FUNCTION +pwc202.task2_plperl( int[] ) +RETURNS int[] +AS $CODE$ + my ( $list ) = @_; + my ( %valleys ); + my $largest = 0; + + for my $index ( 0 .. scalar( $list->@* ) - 1 ) { + my $current = $list->[ $index ]; + next if $list->[ $index + 1 ] > $current; + + my ( @valley_left ) = ( $current ); + for ( $index + 1 .. scalar( $list->@* ) - 1 ) { + my $previous = $valley_left[ -1 ]; + push( @valley_left, $list->[ $_ ] ) if ( $list->[ $_ ] <= $previous ); + last if $list->[ $_ ] > $previous; + } + + + my @valley_right; + if ( $index + scalar( @valley_left ) < scalar( $list->@* ) ) { + my $previous = $list->[ $index + scalar( @valley_left ) ]; + @valley_right = ( $previous ); + for ( $index + scalar( @valley_left ) + 1 .. scalar( $list->@* ) - 1 ) { + my $previous = $valley_right[ -1 ]; + push( @valley_right, $list->[ $_ ] ) if ( $list->[ $_ ] >= $previous ); + last if $list->[ $_ ] < $previous; + } + } + + + + $valleys{ scalar( @valley_right ) + scalar( @valley_left ) } = [ @valley_left, @valley_right ]; + $largest = scalar( @valley_right ) + scalar( @valley_left ) if ( scalar( @valley_right ) + scalar( @valley_left ) > $largest ); + + } + + return $valleys{ $largest }; +$CODE$ +LANGUAGE plperl; -- cgit From 4b3cc2f5422c01d581c64266d8ae42669ef97764 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 10:11:12 +0100 Subject: Task 1 plpgsql --- challenge-202/luca-ferrari/postgresql/ch-1.sql | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 challenge-202/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-202/luca-ferrari/postgresql/ch-1.sql b/challenge-202/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..ddc906c223 --- /dev/null +++ b/challenge-202/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,42 @@ +-- +-- Perl Weekly Challenge 202 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc202; + +CREATE OR REPLACE FUNCTION +pwc202.task1_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + odds int[]; + cur int; +BEGIN + FOREACH cur IN ARRAY l LOOP + IF cur % 2 = 0 THEN + CONTINUE; + END IF; + + IF array_length( odds, 1 ) = 0 OR odds IS NULL THEN + odds := odds || cur; + CONTINUE; + END IF; + + IF odds[ array_length( odds, 1 ) ] + 2 <> cur THEN + CONTINUE; + END IF; + + odds := odds || cur; + END LOOP; + + IF array_length( odds, 1 ) >= 3 THEN + RETURN 1; + ELSE + RETURN 0; + END IF; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 6edb410c60fba61451e89e9b4289455df9a616d5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 10:31:58 +0100 Subject: Task 2 plpgsql done --- challenge-202/luca-ferrari/postgresql/ch-2.sql | 70 ++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 challenge-202/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-202/luca-ferrari/postgresql/ch-2.sql b/challenge-202/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..46efaf0434 --- /dev/null +++ b/challenge-202/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,70 @@ +-- +-- Perl Weekly Challenge 202 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc202; + +CREATE OR REPLACE FUNCTION +pwc202.task2_plpgsql( l int[] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + cur int; + lft int[]; + rgt int[]; + idx int; + iter int; + prev int; +BEGIN + + CREATE TEMPORARY TABLE IF NOT EXISTS pwc202 + ( lft int[], rgt int[], dim int DEFAULT 0 ); + TRUNCATE pwc202; + + + FOR idx IN 1 .. array_length( l, 1 ) - 1 LOOP + cur := l[ idx ]; + IF l[ idx + 1 ] > cur THEN + CONTINUE; + END IF; + + lft := NULL; + lft := array_append( lft, cur ); + FOR iter IN idx + 1 .. array_length( l, 1 ) - 1 LOOP + prev := lft[ array_length( lft, 1 ) ]; + IF l[ iter ] <= prev THEN + lft := array_append( lft, l[ iter ] ); + END IF; + EXIT WHEN l[ iter ] > prev; + END LOOP; + + rgt := NULL; + IF array_length( lft, 1 ) + idx <= array_length( l, 1 ) THEN + prev := l[ idx + array_length( lft, 1 ) ]; + rgt := array_append( rgt, prev ); + FOR iter IN array_length( lft, 1 ) + idx + 1 .. array_length( l, 1 ) LOOP + prev := rgt[ array_length( rgt, 1 ) ]; + IF l[ iter ] >= prev THEN + rgt := array_append( rgt, l[ iter ] ); + END IF; + EXIT WHEN l[ iter ] < prev; + END LOOP; + END IF; + + INSERT INTO pwc202 + VALUES( lft, rgt, array_length( lft, 1 ) + array_length( rgt, 1 ) ); + + + + END LOOP; + + RETURN QUERY SELECT array_cat( p.lft, p.rgt ) + FROM pwc202 p + ORDER BY dim DESC + LIMIT 1; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 266956e51185ba6fb9063081897c521afc6696c5 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 30 Jan 2023 11:06:57 +0100 Subject: Blog references --- challenge-202/luca-ferrari/blog-1.txt | 1 + challenge-202/luca-ferrari/blog-2.txt | 1 + challenge-202/luca-ferrari/blog-3.txt | 1 + challenge-202/luca-ferrari/blog-4.txt | 1 + challenge-202/luca-ferrari/blog-5.txt | 1 + challenge-202/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-202/luca-ferrari/blog-1.txt create mode 100644 challenge-202/luca-ferrari/blog-2.txt create mode 100644 challenge-202/luca-ferrari/blog-3.txt create mode 100644 challenge-202/luca-ferrari/blog-4.txt create mode 100644 challenge-202/luca-ferrari/blog-5.txt create mode 100644 challenge-202/luca-ferrari/blog-6.txt diff --git a/challenge-202/luca-ferrari/blog-1.txt b/challenge-202/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..670a0b07ff --- /dev/null +++ b/challenge-202/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task1 diff --git a/challenge-202/luca-ferrari/blog-2.txt b/challenge-202/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..3861789f73 --- /dev/null +++ b/challenge-202/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task2 diff --git a/challenge-202/luca-ferrari/blog-3.txt b/challenge-202/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..1333d03baf --- /dev/null +++ b/challenge-202/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task1plperl diff --git a/challenge-202/luca-ferrari/blog-4.txt b/challenge-202/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..fb1dc1093f --- /dev/null +++ b/challenge-202/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task2plperl diff --git a/challenge-202/luca-ferrari/blog-5.txt b/challenge-202/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..4d8a45bb5f --- /dev/null +++ b/challenge-202/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task1plpgsql diff --git a/challenge-202/luca-ferrari/blog-6.txt b/challenge-202/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..b4a0d2349c --- /dev/null +++ b/challenge-202/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/30/PerlWeeklyChallenge202.html#task2plpgsql -- cgit From 667894d64cee340829c478956ca0655063af741a Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 30 Jan 2023 12:02:12 -0600 Subject: Week 202 --- challenge-202/bob-lied/README | 6 +- challenge-202/bob-lied/perl/ch-1.pl | 57 +++++++++++ challenge-202/bob-lied/perl/ch-2.pl | 190 ++++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+), 3 deletions(-) create mode 100644 challenge-202/bob-lied/perl/ch-1.pl create mode 100644 challenge-202/bob-lied/perl/ch-2.pl diff --git a/challenge-202/bob-lied/README b/challenge-202/bob-lied/README index 3c3241bd85..a2b67fd974 100644 --- a/challenge-202/bob-lied/README +++ b/challenge-202/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 201 by Bob Lied +Solutions to weekly challenge 202 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-201/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-200/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-202/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-202/bob-lied diff --git a/challenge-202/bob-lied/perl/ch-1.pl b/challenge-202/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..53296aada8 --- /dev/null +++ b/challenge-202/bob-lied/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Week 202 Task 1 Consecutive Odds +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# 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 +#============================================================================= + +use v5.36; + +use List::Util qw/all/; +use List::MoreUtils qw/first_index/; + +use constant CONSECUTIVE => 3; + +use Getopt::Long; +my $DoTest = 0; +my $Consecutive = CONSECUTIVE; + +GetOptions("test" => \$DoTest, "consecutive:i" => \$Consecutive); +exit(!runTest()) if $DoTest; + +say consecOdd(\@ARGV, $Consecutive); + +sub isOdd($n) { $n % 2 } + + +sub consecOdd($array, $consec=CONSECUTIVE) +{ + my $i = first_index { isOdd($_) } $array->@*; + while ( defined $i && $i <= @$array - $consec ) + { + return 1 if all { isOdd($_) } $array->@[$i .. ($i + $consec - 1)]; + while ( ! isOdd($array->[++$i]) ) {} + } + return 0; +} + +sub runTest +{ + use Test2::V0; + + is( consecOdd([1,5,3,6]), 1, "Example 1"); + is( consecOdd([2,6,3,5]), 0, "Example 2"); + is( consecOdd([1,2,3,4]), 0, "Example 3"); + is( consecOdd([2,3,5,7]), 1, "Example 4"); + + done_testing; +} diff --git a/challenge-202/bob-lied/perl/ch-2.pl b/challenge-202/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..90e433b8bf --- /dev/null +++ b/challenge-202/bob-lied/perl/ch-2.pl @@ -0,0 +1,190 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Week 202 Task 2 Widest Valley +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# 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 +#============================================================================= + +use v5.36; + +# State machine states +use constant S_START => 0; +use constant S_CLIMB => 1; +use constant S_DESCEND => 2; +use constant S_FLAT_WHILE_CLIMBING => 3; +use constant S_FINISH => 4; + +# Valleys are represented as (start, end) pairs +use constant V_BEGIN => 0; +use constant V_END => 1; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +my $wide = widestValley(\@ARGV); +say "(", join(",", $wide->@*), ")"; + +sub widestValley($profile) +{ + return $profile if @$profile < 3; + + # Save a list of all valleys found and evaluate for the widest + # after we've gone throught the profile. + my @valleyList; # (start, end) pairs + my @valley = (0,0); + + # To handle flat sections that could be up or down + my $flatStart = 0; + + my $state = S_START; + my $place = 0; + my $endOfProfile = @$profile - 1; + + while ( $state != S_FINISH ) + { + say "STATE=$state place=$place v=(@valley)" if $Verbose; + if ( $state == S_START ) + { + @valley = ( 0, 1 ); + $place = 1; + $state = ( $profile->[1] <= $profile->[0] ) ? S_DESCEND : S_CLIMB; + } + elsif ( $state == S_CLIMB ) + { + if ( $place == $endOfProfile ) + { + $valley[V_END] = $place; + push @valleyList, [ @valley ]; + $state = S_FINISH; + next; + } + + my $step = ( $profile->[$place+1] <=> $profile->[$place] ); + if ( $step > 0 ) + { + $state = S_CLIMB; + $valley[V_END] = ++$place; + } + elsif ( $step < 0 ) + { + # Reverse direction. Start a new valley. + $state = S_DESCEND; + $valley[V_END] = $place; + push @valleyList, [ @valley ]; + @valley = ($place, $place+1); + $place++; + } + elsif ( $step == 0 ) + { + # We are moving horizontally in a flat section. + # If we descend at the end of the flat section, we need + # to know where the flat started to get the beginning + # of a subsequent valley. Meanwhile, we're still "climbing" + # in the current valley. + $state = S_FLAT_WHILE_CLIMBING; + $flatStart = $place; + $valley[V_END] = ++$place; + } + } + elsif ( $state == S_FLAT_WHILE_CLIMBING ) + { + if ( $place == $endOfProfile ) + { + $valley[V_END] = $place; + push @valleyList, [ @valley ]; + $state = S_FINISH; + next; + } + my $step = ( $profile->[$place+1] <=> $profile->[$place] ); + if ( $step == 0 ) + { + $state = S_FLAT_WHILE_CLIMBING; + $valley[V_END] = ++$place; + } + elsif ( $step > 0 ) + { + $state = S_CLIMB; + $valley[V_END] = ++$place; + } + else # $step < 0 + { + # We flattened out and then reversed to downhill. Finish the + # current uphill at the end of the flat, and start a new valley + # where the downhill starts back at the beginning of the flat. + + $state = S_DESCEND; + $valley[V_END] = $place; + push @valleyList, [ @valley ]; + @valley = ( $flatStart, ++$place ); + } + } + elsif ( $state = S_DESCEND ) + { + if ( $place == $endOfProfile ) + { + $valley[V_END] = $place; + push @valleyList, [ @valley ]; + $state = S_FINISH; + next; + } + my $step = ( $profile->[$place+1] <=> $profile->[$place] ); + if ( $step <= 0 ) + { + $state = S_DESCEND; + $valley[V_END] = ++$place; + } + else + { + $state = S_CLIMB; + $valley[V_END] = ++$place; + } + } + } + + my $maxWidth = 0; + my $widest = $valleyList[0]; + for my $v ( @valleyList ) + { + my $width = $v->[V_END] - $v->[V_BEGIN]; + if ( $width > $maxWidth ) + { + $maxWidth = $width; + $widest = $v; + } + } + + return [ $profile->@[ $widest->[V_BEGIN] .. $widest->[V_END] ] ]; +} + +sub runTest +{ + use Test2::V0; + + is( widestValley([1,5,5,2,8 ]), [5,5,2,8 ], "Example 1"); + is( widestValley([2,6,8,5 ]), [2,6,8 ], "Example 2"); + is( widestValley([9,8,13,13,2,2,15,17]), [13,13,2,2,15,17], "Example 3"); + is( widestValley([2,1,2,1,3 ]), [2,1,2 ], "Example 4"); + is( widestValley([1,3,3,2,1,2,3,3,2 ]), [3,3,2,1,2,3,3 ], "Example 5"); + + done_testing; +} -- cgit From 6bb0770458b327f265fd946bdb65536b90312d27 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 30 Jan 2023 12:12:16 -0600 Subject: Solve PWC202 --- challenge-202/wlmb/blog.txt | 1 + challenge-202/wlmb/perl/ch-1.pl | 13 +++++++++++++ challenge-202/wlmb/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 challenge-202/wlmb/blog.txt create mode 100755 challenge-202/wlmb/perl/ch-1.pl create mode 100755 challenge-202/wlmb/perl/ch-2.pl diff --git a/challenge-202/wlmb/blog.txt b/challenge-202/wlmb/blog.txt new file mode 100644 index 0000000000..1b85d67794 --- /dev/null +++ b/challenge-202/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/01/30/PWC202/ diff --git a/challenge-202/wlmb/perl/ch-1.pl b/challenge-202/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..edcaa690c2 --- /dev/null +++ b/challenge-202/wlmb/perl/ch-1.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 202 +# Task 1: Consecutive Odds +# +# See https://wlmb.github.io/2023/01/30/PWC202/#task-1-consecutive-odds +use v5.36; +my $out=0; +my ($u, $d, $t); +for(sort {$a <=> $b} grep {$_%2} @ARGV){ + ($u,$d,$t)=($d,$t,$_); # Current three elements + $out=1, last if $u+4==$d+2==$t +} +say(join " ", @ARGV, "-> $out"); diff --git a/challenge-202/wlmb/perl/ch-2.pl b/challenge-202/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..f0c9103cc4 --- /dev/null +++ b/challenge-202/wlmb/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +# Perl weekly challenge 202 +# Task 2: Widest Valley +# +# See https://wlmb.github.io/2023/01/30/PWC202/#task-2-widest-valley +use v5.36; +use List::UtilsBy qw(max_by); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find the widest valley in the sequence N1 N2... + FIN + +my @in=@ARGV; +my $ascending=0; +my @up=my @down=(my $previous=shift); +my @out; +for(@ARGV){ + if($_>$previous){ + @up=@down unless $ascending; + @down=(); + $ascending=1; + } + if($_<$previous){ + push @out, [@up]; + @up=(); + $ascending=0; + } + push @down, $_; + push @up, $_; + $previous=$_; +} +push @out, [@down], [@up]; +my $result=max_by {@$_} @out; +say join " ", @in, "->", @$result; -- cgit From ad1fa45f97615b963881a9b75958aa72c7701566 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 30 Jan 2023 15:05:39 -0500 Subject: Week 202 --- challenge-202/zapwai/blog.txt | 1 + challenge-202/zapwai/perl/ch-1.pl | 28 ++++++++++++++++++++++++ challenge-202/zapwai/perl/ch-2.pl | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 challenge-202/zapwai/blog.txt create mode 100644 challenge-202/zapwai/perl/ch-1.pl create mode 100644 challenge-202/zapwai/perl/ch-2.pl diff --git a/challenge-202/zapwai/blog.txt b/challenge-202/zapwai/blog.txt new file mode 100644 index 0000000000..e961630768 --- /dev/null +++ b/challenge-202/zapwai/blog.txt @@ -0,0 +1 @@ +https://dev.to/zapwai/weekly-challenge-202-59md diff --git a/challenge-202/zapwai/perl/ch-1.pl b/challenge-202/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..f4b2172a09 --- /dev/null +++ b/challenge-202/zapwai/perl/ch-1.pl @@ -0,0 +1,28 @@ +use v5.30.0; +my @array1 = (1,5,3,6); +my @array2 = (2,6,3,5); +my @array3 = (1,2,3,4); +my @array4 = (2,3,5,7); +my @test = (\@array1, \@array2,\@array3,\@array4); +sub are_three_odds { + my @list = @_; + my @ind; + for (0 .. $#list - 2) { + push @ind, $_ if ( ($list[$_] % 2 == 1) && ($list[$_ + 1] % 2 == 1) && ($list[$_ + 2] % 2 == 1) ); + } + return 0 unless (@ind); + foreach (@ind) { + my @set = ($list[$_],$list[$_+1],$list[$_+2]); + @set = sort {$a <=> $b} @set; + if ( ($set[2] - $set[1]) == ($set[1] - $set[0])) { + return 1; + } + } + return 0; +} + +for (0 .. $#test) { + my @array = @{$test[$_]}; + say "Input: (".join(",",@array).")"; + say "Output: " . are_three_odds(@array); +} diff --git a/challenge-202/zapwai/perl/ch-2.pl b/challenge-202/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..978c3c7e0f --- /dev/null +++ b/challenge-202/zapwai/perl/ch-2.pl @@ -0,0 +1,45 @@ +use v5.30.0; +my @array1 = (1, 5, 5, 2, 8); +my @array2 = (2,6,8,5); +my @array3 = (9,8,13,13,2,2,15,17); +my @array4 = (2,1,2,1,3); +my @array5 = (1,3,3,2,1,2,3,3,2); +my @test = (\@array1,\@array2,\@array3,\@array4,\@array5); +for my $ref (@test) { + my @array = @$ref; + my @valley; # push an array ref to each valley here. + my $begin_index = 0; + my $incr_flag = 0; # flag after descent is complete. + for (0 .. $#array - 1) { + if ($incr_flag) { + if ( $array[$_ + 1] < $array[$_] ) { + push @valley, [ @array[$begin_index .. $_] ]; + $begin_index = $_; + for my $i (1 .. $_) { # check if it was level + if ( $array[$_] == $array[$_ - $i] ) { + $begin_index--; + } else { + last; + } + } + $incr_flag = 0; + } + } else { + if ( $array[$_ + 1] > $array[$_] ) { + $incr_flag = 1; + } + } + } + push @valley, [ @array[$begin_index .. $#array] ]; + my $max = 0; + my $ind; + for (0 .. $#valley) { + if (scalar @{$valley[$_]} > $max) { + $max = scalar @{$valley[$_]}; + $ind = $_; + } + } + say "Input: @array"; + say "Output: @{$valley[$ind]}"; + say "-" x 10; +} -- cgit From de730514ed57e48c04d71b156650bc4d3b59b9d9 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 31 Jan 2023 00:28:30 +0100 Subject: Add solutions to 202: Consecutive Odds & Widest Valley by E. Choroba 202/2 also solved in Erlang. --- challenge-202/e-choroba/erlang/widest_valley.erl | 53 ++++++++++++++++++++++++ challenge-202/e-choroba/perl/ch-1.pl | 23 ++++++++++ challenge-202/e-choroba/perl/ch-2.pl | 39 +++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 challenge-202/e-choroba/erlang/widest_valley.erl create mode 100755 challenge-202/e-choroba/perl/ch-1.pl create mode 100755 challenge-202/e-choroba/perl/ch-2.pl diff --git a/challenge-202/e-choroba/erlang/widest_valley.erl b/challenge-202/e-choroba/erlang/widest_valley.erl new file mode 100644 index 0000000000..5924e46287 --- /dev/null +++ b/challenge-202/e-choroba/erlang/widest_valley.erl @@ -0,0 +1,53 @@ +-module(widest_valley). +-export([widest_valley/1]). +-record(valley, {from=1, to=1, deepest=1}). + +widest_valley([]) -> + []; +widest_valley([H]) -> + [H]; +widest_valley([H,T]) -> + [H,T]; +widest_valley(L) -> + widest_valley(L, #valley{}, #valley{}, 1, 2). + +widest_valley(L, _Curr, Widest, _FlatFrom, Pos) when Pos > length(L) -> + lists:sublist(L, Widest#valley.from, + 1 + Widest#valley.to - Widest#valley.from); +widest_valley(L, Curr, Widest, FlatFrom, Pos) -> + Before = lists:nth(Pos - 1, L), + AtPos = lists:nth(Pos, L), + Deepest = lists:nth(Curr#valley.deepest, L), + Curr1 = if Before == Deepest -> + Curr#valley{deepest = if AtPos < Deepest -> + Pos; + true -> + Curr#valley.deepest + end}; + AtPos < Before -> + #valley{from=FlatFrom, deepest=Pos}; + true -> + Curr + end, + FlatFrom1 = if AtPos == Before -> + FlatFrom; + true -> + Pos + end, + Widest1 = + if Pos - Curr1#valley.from > Widest#valley.to - Widest#valley.from + -> Curr1#valley{to=Pos}; + true -> + Widest + end, + widest_valley(L, Curr1, Widest1, FlatFrom1, Pos + 1). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +widest_valley_test() -> + ?assert(widest_valley([1, 5, 5, 2, 8]) == [5, 5, 2, 8]), + ?assert(widest_valley([2, 6, 8, 5]) == [2, 6, 8]), + ?assert(widest_valley([9, 8, 13, 13, 2, 2, 15, 17]) == [13, 13, 2, 2, 15, 17]), + ?assert(widest_valley([2, 1, 2, 1, 3]) == [2, 1, 2]), + ?assert(widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]) == [3, 3, 2, 1, 2, 3, 3]). +-endif. diff --git a/challenge-202/e-choroba/perl/ch-1.pl b/challenge-202/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f1a029ccaf --- /dev/null +++ b/challenge-202/e-choroba/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub consecutive_odds ($array) { + my %elements; + @elements{@$array} = (); + for my $element (keys %elements) { + return 1 + if 1 == $element % 2 + && exists $elements{$element + 2} + && exists $elements{$element + 4}; + } + return 0 +} + +use Test::More tests => 4; + +is consecutive_odds([1, 5, 3, 6]), 1, 'Example 1'; +is consecutive_odds([2, 6, 3, 5]), 0, 'Example 2'; +is consecutive_odds([1, 2, 3, 4]), 0, 'Example 3'; +is consecutive_odds([2, 3, 5, 7]), 1, 'Example 4'; diff --git a/challenge-202/e-choroba/perl/ch-2.pl b/challenge-202/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..51ae8dc7b1 --- /dev/null +++ b/challenge-202/e-choroba/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub widest_valley($profile) { + my @widest = (1, 0); + my $from = 0; + while ($from <= $#$profile) { + my $to = my $deepest = my $flat_from = $from; + while ($to <= $#$profile + && ($deepest >= $to - 1 + || $profile->[$to] >= $profile->[ $to - 1 ]) + ) { + $deepest = $to if $profile->[$to] <= $profile->[$deepest]; + $flat_from = $to unless $profile->[$to] == $profile->[ $to - 1 ]; + @widest = ($from, $to) if $to - $from > $widest[1] - $widest[0]; + ++$to; + } + $from = $to > $#$profile ? $to : $flat_from; + } + return [@$profile[ $widest[0] .. $widest[1] ]] +} + +use Test2::V0; +plan 5 + 4; +is widest_valley([1, 5, 5, 2, 8]), [5, 5, 2, 8], 'Example 1'; +is widest_valley([2, 6, 8, 5]), [2, 6, 8], 'Exapmle 2'; +is widest_valley([9, 8, 13, 13, 2, 2, 15, 17]), + [13, 13, 2, 2, 15, 17], + 'Example 3'; +is widest_valley([1, 3, 3, 2, 1, 2, 3, 3, 2]), [3, 3, 2, 1, 2, 3, 3], + 'Example 4'; +is widest_valley([2, 1, 2, 1, 3]), [2, 1, 2], 'Example 5'; + +is widest_valley([]), [], 'Empty'; +is widest_valley([1, 1]), [1, 1], 'Flat'; +is widest_valley([1, 2]), [1, 2], 'Increasing'; +is widest_valley([2, 1]), [2, 1], 'Decreasing'; -- cgit From bfb8150b8ba3e7b7b5c3218c48e558e611a43bf7 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 31 Jan 2023 02:03:31 +0000 Subject: Initial 202 (Raku) --- challenge-202/mark-anderson/raku/ch-1.raku | 15 ++++++++++++++ challenge-202/mark-anderson/raku/ch-2.raku | 33 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-202/mark-anderson/raku/ch-1.raku create mode 100644 challenge-202/mark-anderson/raku/ch-2.raku diff --git a/challenge-202/mark-anderson/raku/ch-1.raku b/challenge-202/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..8b5d682d98 --- /dev/null +++ b/challenge-202/mark-anderson/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku +use Test; + +ok is-consecutive-odds(11, 12, 13, 55, 33, 12, 45, 77, 95, 1, 4); +nok is-consecutive-odds(11, 12, 13, 55, 34, 12, 45, 77, 96, 1, 4); + +sub is-consecutive-odds(*@a) +{ + for @a.rotor(3 => -2) + { + return 1 if all $_ >>%>> 2 + } + + return 0 +} diff --git a/challenge-202/mark-anderson/raku/ch-2.raku b/challenge-202/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..5e610ce5c3 --- /dev/null +++ b/challenge-202/mark-anderson/raku/ch-2.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +say widest-valley(< 1 2 2 5 4 1 0 2 3 2 1 3 4 3 1 32 11 11 8 >); +say widest-valley(< 1 2 2 5 4 1 0 0 0 0 0 2 3 4 5 32 11 11 8 >); +say widest-valley(< 1 1 1 1 1 2 5 >); +say widest-valley(< 32 11 11 8 >); +say widest-valley(< 11 11 11 8 4 2 4 8 11 11 11 >); +say widest-valley((10..50).roll(1000)); + +sub widest-valley(*@a) +{ + my @decreasing; + my @increasing; + + until @a <= 1 + { + my $k = ([\>=] @a).first({ .not }, :k) // @a.elems; + @decreasing.push: @a.splice(0, $k, @a[$k-1]); + + $k = ([\<=] @a).first({ .not }, :k) || @a.elems; + @increasing.push: @a.splice(0, $k, @a[$k-1]); + } + + @a = @decreasing Z @increasing; + + my $a := gather for @a + { + pop .[0]; + take .comb(/\d+/) + } + + $a[$_] given $a.cache>>.elems.maxpairs.first.key +} -- cgit From a91c92d5683d263845ed2ae08af470a276535ca4 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Tue, 31 Jan 2023 02:09:00 +0000 Subject: Initial 202 (Raku) --- challenge-202/mark-anderson/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-202/mark-anderson/raku/ch-2.raku b/challenge-202/mark-anderson/raku/ch-2.raku index 5e610ce5c3..d29886a5f8 100644 --- a/challenge-202/mark-anderson/raku/ch-2.raku +++ b/challenge-202/mark-anderson/raku/ch-2.raku @@ -17,7 +17,7 @@ sub widest-valley(*@a) my $k = ([\>=] @a).first({ .not }, :k) // @a.elems; @decreasing.push: @a.splice(0, $k, @a[$k-1]); - $k = ([\<=] @a).first({ .not }, :k) || @a.elems; + $k = ([\<=] @a).first({ .not }, :k) // @a.elems; @increasing.push: @a.splice(0, $k, @a[$k-1]); } -- cgit From 7b8bbfbe1ca2c8b1215b72bd7e6ea59b11a8bf1c Mon Sep 17 00:00:00 2001 From: LoneWolfiNTj Date: Tue, 31 Jan 2023 02:41:48 -0800 Subject: Robbie Hatley's Perl solutions for Weekly Challenge 202 --- challenge-202/robbie-hatley/README | 0 challenge-202/robbie-hatley/blog.txt | 1 + challenge-202/robbie-hatley/perl/ch-1.pl | 55 +++++++++++++++ challenge-202/robbie-hatley/perl/ch-2.pl | 111 +++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+) mode change 100755 => 100644 challenge-202/robbie-hatley/README create mode 100644 challenge-202/robbie-hatley/blog.txt create mode 100755 challenge-202/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-202/robbie-hatley/perl/ch-2.pl diff --git a/challenge-202/robbie-hatley/README b/challenge-202/robbie-hatley/README old mode 100755 new mode 100644 diff --git a/challenge-202/robbie-hatley/blog.txt b/challenge-202/robbie-hatley/blog.txt new file mode 100644 index 0000000000..53738017e5 --- /dev/null +++ b/challenge-202/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/01/robbie-hatleys-perl-solutions-to-weekly_31.html \ No newline at end of file diff --git a/challenge-202/robbie-hatley/perl/ch-1.pl b/challenge-202/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..09ee58ac28 --- /dev/null +++ b/challenge-202/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,55 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl Solutions to The Weekly Challenge #202-1 + +# DESCRIPTION OF PROBLEM: + +=pod + +Task 1: Consecutive Odds +Submitted by: Mohammad S Anwar + +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: (1,5,3,6) Output: 1 +Example 2: Input: (2,6,3,5) Output: 0 +Example 3: Input: (1,2,3,4) Output: 0 +Example 4: Input: (2,3,5,7) Output: 1 + +=cut + +# IO NOTES: +# NOTE: Default input is via built-in array of arrays. +# Non-default input can be provided through @ARGV. +# If using @ARGV, arguments should be a space-separated sequence +# of integers, which will be construed as being a single array. +# +# NOTE: Output is to stdout and will be 1 if 3 consecutive odds, else 0. + +# PRELIMINARIES: +use v5.36; +$,=' '; + +# SUBROUTINES: +sub tco (@a){ + for (my $i = 0 ; $i <= $#a-2 ; ++$i){ + if ( !($a[$i+0]%2) ) {$i += 0; next;} + if ( !($a[$i+1]%2) ) {$i += 1; next;} + if ( !($a[$i+2]%2) ) {$i += 2; next;} + return 1;} + return 0;} + +# DEFAULT INPUT: +my @arrays = ([1,5,3,6],[2,6,3,5],[1,2,3,4],[2,3,5,7]); + +# NON-DEFAULT INPUT: +if (@ARGV) {@arrays = ([@ARGV]);} + +# SCRIPT BODY: +for (@arrays){ + my @array = @{$_}; + my $tco = tco(@array); + say ''; + say "array = (@array)"; + say "tco = $tco";} \ No newline at end of file diff --git a/challenge-202/robbie-hatley/perl/ch-2.pl b/challenge-202/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..2c021accaa --- /dev/null +++ b/challenge-202/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,111 @@ +#! /usr/bin/perl + +# Robbie Hatley's Perl Solutions to The Weekly Challenge #202-2 + +# DESCRIPTION OF PROBLEM: + +=pod + +Task 2: Widest Valley +Submitted by: E. Choroba + +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 + +=cut + +# IO NOTES: +# +# NOTE: Input is via either built-in array of arrays, or @ARGV. +# If using @ARGV, arguments should be a space-separated sequence of +# integers, which will be interpreted as being a single array +# +# NOTE: Output will be to stdout and will be the contents of each array +# followed by the left-most widest valley within that array. + +# PRELIMINARIES: +use v5.36; +$"=", "; + +# DEFAULT INPUT: +my @arrays = +( + [1, 5, 5, 2, 8], +