From bd6138d2bf51a8413e0c4fc1c2a93bdf95f9fee3 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Fri, 12 May 2023 14:31:30 -0500 Subject: Week 216 checkpoint --- challenge-216/bob-lied/README | 6 +- challenge-216/bob-lied/perl/SubstringIterator.pm | 61 +++++++++++ challenge-216/bob-lied/perl/ch-1.pl | 49 +++++++++ challenge-216/bob-lied/perl/ch-2.pl | 125 +++++++++++++++++++++++ 4 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 challenge-216/bob-lied/perl/SubstringIterator.pm create mode 100644 challenge-216/bob-lied/perl/ch-1.pl create mode 100644 challenge-216/bob-lied/perl/ch-2.pl diff --git a/challenge-216/bob-lied/README b/challenge-216/bob-lied/README index 386b265dbc..102026d384 100644 --- a/challenge-216/bob-lied/README +++ b/challenge-216/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 215 by Bob Lied +Solutions to weekly challenge 216 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-215/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-215/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-216/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-216/bob-lied diff --git a/challenge-216/bob-lied/perl/SubstringIterator.pm b/challenge-216/bob-lied/perl/SubstringIterator.pm new file mode 100644 index 0000000000..3db65ecce0 --- /dev/null +++ b/challenge-216/bob-lied/perl/SubstringIterator.pm @@ -0,0 +1,61 @@ +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# SubstringIterator.pm +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# Description: +# Iterate over all possible substrings of a string. Each iteration returns +# a pair: the substring, and the original string with the substring removed. +# +# We start with the longest possible substring (the string itself) and return +# new substrings in descending size. +# +# When the iterator is finished, it returns undef as the substring. +#============================================================================= + +package SubstringIterator; + +use v5.36; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(); +our @EXPORT_OK = qw(); + +sub new +{ + my $class = shift; + $class = ref($class) || $class; + my $self = { + _target => $_[0], + _length => 0, + _pos => 0, + _size => 0, + }; + + $self->{_size} = $self->{_length} = length($self->{_target}); + bless $self, $class; + return $self; +} + +sub next($self) +{ + my ($target, $length, $pos, $size) = $self->@{ qw(_target _length _pos _size) }; + + return (undef, undef) if $length == 0; + + my $sub = substr( $target, $pos, $length ); + my $before = substr( $target, 0, $pos); + my $after = substr( $target, $pos + $length, $size - $length); + my @retval = ( $sub, "$before$after" ); + + if ( $self->{_pos}++ == $size ) + { + $self->{_length}--; + } + + return @retval; +} + +1; diff --git a/challenge-216/bob-lied/perl/ch-1.pl b/challenge-216/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..9442c24fe2 --- /dev/null +++ b/challenge-216/bob-lied/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Task 1 Registration Number +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a list of words and a random registration number. +# Write a script to find all the words in the given list that has every +# letter in the given registration number. +# Example 1 +# Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD' +# Output: ('abcd') +# The only word that matches every alphabets in the given registration +# number is 'abcd'. +# Example 2 +# Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB' +# Output: ('job', 'bjorg') +# Example 3 +# Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2' +# Output: ('crack', 'rac') +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub regNumber($num, @words) +{ + (my $mustHave = lc($num)) =~ s/[^a-z]*//g; + [ grep { $mustHave =~ m/^[$_]+$/ } @words ]; +} + +sub runTest +{ + use Test2::V0; + + is(regNumber('AB1 2CD', qw(abc abcd bcd) ), ['abcd' ], "Example 1"); + is(regNumber('007 JB' , qw(job james bjorg)), ['job','bjorg'], "Example 2"); + is(regNumber('C7 RA2' , qw(crack road rac) ), ['crack','rac'], "Example 3"); + + done_testing; +} + diff --git a/challenge-216/bob-lied/perl/ch-2.pl b/challenge-216/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..f4124f98ff --- /dev/null +++ b/challenge-216/bob-lied/perl/ch-2.pl @@ -0,0 +1,125 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Task 2 Word Stickers +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a list of word stickers and a target word. +# Write a script to find out how many word stickers are needed to make up +# the given target word. +# Example 1: +# Input: @stickers = ('perl','raku','python'), $word = 'peon' +# Output: 2 +# We just need 2 stickers i.e. 'perl' and 'python'. +# 'pe' from 'perl' and +# 'on' from 'python' to get the target word. +# Example 2: +# Input: @stickers = ('love','hate','angry'), $word = 'goat' +# Output: 3 +# We need 3 stickers i.e. 'angry', 'love' and 'hate'. +# 'g' from 'angry' +# 'o' from 'love' and +# 'at' from 'hate' to get the target word. +# Example 3: +# Input: @stickers = ('come','nation','delta'), $word = 'accommodation' +# Output: 4 +# We just need 2 stickers of 'come' and one each of 'nation' & 'delta'. +# 'a' from 'delta' +# 'ccommo' from 2 stickers 'come' +# 'd' from the same sticker 'delta' and +# 'ation' from 'nation' to get the target word. +# Example 4: +# Input: @stickers = ('come','country','delta'), $word = 'accommodation' +# Output: 0 +# as there's no "i" in the inputs. +# +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub allSubstrings($str) +{ + my @substr; + + my $size = length($str); + for my $len ( 1 .. $size ) + { + for ( my $pos = 0; $pos + $len <= $size ; $pos++ ) + { + push @substr, substr($str, $pos, $len); + } + } + + return \@substr; +} + +sub wordSticker($target, @stickerWord) +{ + # Normalize everything to lowercase. + $target = lc($target); + @stickerWord = map { lc } @stickerWord; + + # Do all letters in $word occur somewhere in @sticker? + # Conversely, does word consist entirely of letters from @sticker + my $available = join("", @stickerWord); + return 0 unless $target =~ m/^[$available]+$/; + + # Gather up a set of all possible substrings from the sticker words + my %sticker; + for my $w ( @stickerWord) + { + my $s = allSubstrings($w); + $sticker{$_} = $w for @$s; + } + + # Worst case is one letter at a time. + my $minScore = length($target); + + sticker($target, \%sticker, 0, [], ""); + + + return -1; +} + +sub sticker($target, $stickers, $scoreSoFar, $path, $indent) +{ + my $len = length($target); + if ( $len == 0 ) { return $scoreSoFar } + elsif ( $len == 1 ) { return $scoreSoFar + 1 } + + # Maybe we get lucky and the entire target word is one sticker. + if ( exists $stickers->{$target} ) + { + return $scoreSoFar + 1; + } + + # Take each possible substring from the target. If the substring + # can be made from a sticker, remove it and recurse on the smaller + # target. + +} + +sub runTest +{ + use Test2::V0; + + is( allSubstrings("a"), [ qw(a) ], "Substr 1"); + is( allSubstrings("ab"), [ qw(a b ab) ], "Substr 2"); + is( allSubstrings("abc"), [ qw(a b c ab bc abc) ], "Substr 3"); + is( allSubstrings("abcd"), [ qw(a b c d ab bc cd abc bcd abcd) ], "Substr 4"); + + is(wordSticker("peon" , qw(perl raku python) ), 2, "Example 1"); + is(wordSticker("goat" , qw(love hate angry ) ), 3, "Example 2"); + is(wordSticker("accommodation", qw(come nation delta) ), 4, "Example 3"); + is(wordSticker("accommodation", qw(come country delta)), 0, "Example 4"); + + done_testing; +} -- cgit From d3f1d667545675572756c7792cd26de29b3a6a5e Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Fri, 12 May 2023 14:36:18 -0500 Subject: Move to lib directory --- challenge-216/bob-lied/perl/SubstringIterator.pm | 61 ---------------------- .../bob-lied/perl/lib/SubstringIterator.pm | 61 ++++++++++++++++++++++ 2 files changed, 61 insertions(+), 61 deletions(-) delete mode 100644 challenge-216/bob-lied/perl/SubstringIterator.pm create mode 100644 challenge-216/bob-lied/perl/lib/SubstringIterator.pm diff --git a/challenge-216/bob-lied/perl/SubstringIterator.pm b/challenge-216/bob-lied/perl/SubstringIterator.pm deleted file mode 100644 index 3db65ecce0..0000000000 --- a/challenge-216/bob-lied/perl/SubstringIterator.pm +++ /dev/null @@ -1,61 +0,0 @@ -# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: -#============================================================================= -# SubstringIterator.pm -#============================================================================= -# Copyright (c) 2023, Bob Lied -#============================================================================= -# Description: -# Iterate over all possible substrings of a string. Each iteration returns -# a pair: the substring, and the original string with the substring removed. -# -# We start with the longest possible substring (the string itself) and return -# new substrings in descending size. -# -# When the iterator is finished, it returns undef as the substring. -#============================================================================= - -package SubstringIterator; - -use v5.36; - -require Exporter; -our @ISA = qw(Exporter); -our @EXPORT = qw(); -our @EXPORT_OK = qw(); - -sub new -{ - my $class = shift; - $class = ref($class) || $class; - my $self = { - _target => $_[0], - _length => 0, - _pos => 0, - _size => 0, - }; - - $self->{_size} = $self->{_length} = length($self->{_target}); - bless $self, $class; - return $self; -} - -sub next($self) -{ - my ($target, $length, $pos, $size) = $self->@{ qw(_target _length _pos _size) }; - - return (undef, undef) if $length == 0; - - my $sub = substr( $target, $pos, $length ); - my $before = substr( $target, 0, $pos); - my $after = substr( $target, $pos + $length, $size - $length); - my @retval = ( $sub, "$before$after" ); - - if ( $self->{_pos}++ == $size ) - { - $self->{_length}--; - } - - return @retval; -} - -1; diff --git a/challenge-216/bob-lied/perl/lib/SubstringIterator.pm b/challenge-216/bob-lied/perl/lib/SubstringIterator.pm new file mode 100644 index 0000000000..3db65ecce0 --- /dev/null +++ b/challenge-216/bob-lied/perl/lib/SubstringIterator.pm @@ -0,0 +1,61 @@ +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# SubstringIterator.pm +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# Description: +# Iterate over all possible substrings of a string. Each iteration returns +# a pair: the substring, and the original string with the substring removed. +# +# We start with the longest possible substring (the string itself) and return +# new substrings in descending size. +# +# When the iterator is finished, it returns undef as the substring. +#============================================================================= + +package SubstringIterator; + +use v5.36; + +require Exporter; +our @ISA = qw(Exporter); +our @EXPORT = qw(); +our @EXPORT_OK = qw(); + +sub new +{ + my $class = shift; + $class = ref($class) || $class; + my $self = { + _target => $_[0], + _length => 0, + _pos => 0, + _size => 0, + }; + + $self->{_size} = $self->{_length} = length($self->{_target}); + bless $self, $class; + return $self; +} + +sub next($self) +{ + my ($target, $length, $pos, $size) = $self->@{ qw(_target _length _pos _size) }; + + return (undef, undef) if $length == 0; + + my $sub = substr( $target, $pos, $length ); + my $before = substr( $target, 0, $pos); + my $after = substr( $target, $pos + $length, $size - $length); + my @retval = ( $sub, "$before$after" ); + + if ( $self->{_pos}++ == $size ) + { + $self->{_length}--; + } + + return @retval; +} + +1; -- cgit From 3cca94c5bda21a83cac04bdb7d8fc2a9206c6c1c Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Tue, 16 May 2023 08:02:29 -0500 Subject: checkpoint --- challenge-216/bob-lied/perl/ch-2.pl | 34 ++++++++++++++++++---- .../bob-lied/perl/lib/SubstringIterator.pm | 9 +++--- challenge-216/bob-lied/perl/t/SubstringIterator.t | 22 ++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 challenge-216/bob-lied/perl/t/SubstringIterator.t diff --git a/challenge-216/bob-lied/perl/ch-2.pl b/challenge-216/bob-lied/perl/ch-2.pl index f4124f98ff..69c8fe5ef6 100644 --- a/challenge-216/bob-lied/perl/ch-2.pl +++ b/challenge-216/bob-lied/perl/ch-2.pl @@ -38,6 +38,9 @@ use v5.36; +use lib "lib"; +use SubstringIterator; + use Getopt::Long; my $Verbose = 0; my $DoTest = 0; @@ -83,27 +86,46 @@ sub wordSticker($target, @stickerWord) # Worst case is one letter at a time. my $minScore = length($target); - sticker($target, \%sticker, 0, [], ""); + stickerize($target, \%sticker, 0); return -1; } -sub sticker($target, $stickers, $scoreSoFar, $path, $indent) +sub stickerize($target, $stickers, $scoreSoFar) { + state %cache; + if ( exists $stickers->{$target} ) + { + return $scoreSoFar + $cache{$target}; + } + + # Base cases for recursion. If the string is of length one, we + # know that there is a sticker for it because we already verified + # that all letters in the target occur somewhere among the stickers. my $len = length($target); if ( $len == 0 ) { return $scoreSoFar } - elsif ( $len == 1 ) { return $scoreSoFar + 1 } + elsif ( $len == 1 ) { return $soreSoFar + ($cache{$target} = 1) } - # Maybe we get lucky and the entire target word is one sticker. - if ( exists $stickers->{$target} ) + if ( exists $cache{$target} ) { - return $scoreSoFar + 1; + return $scoreSoFar + $cache{$target} } # Take each possible substring from the target. If the substring # can be made from a sticker, remove it and recurse on the smaller # target. + my $ssi = SubstringIterator->new($target); + + my $score = $len; + my ($pre, $ss, $post) = $ssi->next(); + while ( defined $ss ) + { + say "SSI: pre=[$pre] ss=[$ss] post=[$post]" if $Verbose; + $score = map { stickerize($_, $stickers) } $pre, $ss, $post; + ($pre, $ss, $post) = $ssi->next(); + } + } diff --git a/challenge-216/bob-lied/perl/lib/SubstringIterator.pm b/challenge-216/bob-lied/perl/lib/SubstringIterator.pm index 3db65ecce0..06486b1029 100644 --- a/challenge-216/bob-lied/perl/lib/SubstringIterator.pm +++ b/challenge-216/bob-lied/perl/lib/SubstringIterator.pm @@ -6,7 +6,7 @@ #============================================================================= # Description: # Iterate over all possible substrings of a string. Each iteration returns -# a pair: the substring, and the original string with the substring removed. +# a triplet: the substring, and the prefix and suffix around it. # # We start with the longest possible substring (the string itself) and return # new substrings in descending size. @@ -43,16 +43,17 @@ sub next($self) { my ($target, $length, $pos, $size) = $self->@{ qw(_target _length _pos _size) }; - return (undef, undef) if $length == 0; + return (undef, undef, undef) if $length == 0; my $sub = substr( $target, $pos, $length ); my $before = substr( $target, 0, $pos); my $after = substr( $target, $pos + $length, $size - $length); - my @retval = ( $sub, "$before$after" ); + my @retval = ( $before, $sub, $after ); - if ( $self->{_pos}++ == $size ) + if ( ($self->{_pos}++ + $length) == $size ) { $self->{_length}--; + $self->{_pos} = 0; } return @retval; diff --git a/challenge-216/bob-lied/perl/t/SubstringIterator.t b/challenge-216/bob-lied/perl/t/SubstringIterator.t new file mode 100644 index 0000000000..b4c0700dde --- /dev/null +++ b/challenge-216/bob-lied/perl/t/SubstringIterator.t @@ -0,0 +1,22 @@ +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +# +#=============================================================================== +# +# FILE: SubstringIterator.t +# +# DESCRIPTION: Unit test for SubstringIterator +#=============================================================================== + +use v5.36; + +use Test2::V0; + +use SubstringIterator; + +my $ssi = SubstringIterator->new("a"); +isa_ok( $ssi, [ qw(SubstringIterator) ] ); + +is ( $ssi->next(), "a", "Length one first"); +is ( $ssi->next(), undef, "Length one last"); + +done_testing(); -- cgit From e630cd99f1c212d2f5780c094fbcccedb3c4d1e7 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Sun, 21 May 2023 20:53:32 -0500 Subject: checkpoint --- challenge-216/bob-lied/perl/t/SubstringIterator.t | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/challenge-216/bob-lied/perl/t/SubstringIterator.t b/challenge-216/bob-lied/perl/t/SubstringIterator.t index b4c0700dde..5d8edf584c 100644 --- a/challenge-216/bob-lied/perl/t/SubstringIterator.t +++ b/challenge-216/bob-lied/perl/t/SubstringIterator.t @@ -16,7 +16,18 @@ use SubstringIterator; my $ssi = SubstringIterator->new("a"); isa_ok( $ssi, [ qw(SubstringIterator) ] ); -is ( $ssi->next(), "a", "Length one first"); -is ( $ssi->next(), undef, "Length one last"); +my @ret; +@ret = $ssi->next(); is ( \@ret, [ '', 'a', '' ], "Length one first"); +@ret = $ssi->next(); is ( \@ret, [ undef,undef,undef ], "Length one last"); + +$ssi = SubstringIterator->new("abc"); +@ret = $ssi->next(); is ( \@ret, [ '', 'abc', '' ], "Length 3 first"); +@ret = $ssi->next(); is ( \@ret, [ '', 'ab', 'c' ], "Length 3 2"); +@ret = $ssi->next(); is ( \@ret, [ 'a', 'bc', '' ], "Length 3 3"); +@ret = $ssi->next(); is ( \@ret, [ '', 'a', 'bc' ], "Length 3 4"); +@ret = $ssi->next(); is ( \@ret, [ 'a', 'b', 'c' ], "Length 3 5"); +@ret = $ssi->next(); is ( \@ret, [ 'ab', 'c', '' ], "Length 3 6"); +@ret = $ssi->next(); is ( \@ret, [ undef,undef,undef ], "Length one last"); + done_testing(); -- cgit From 59324a70dbfb391f0b91ba0900c5efc3204cea8d Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Fri, 14 Jul 2023 09:25:17 -0500 Subject: Week 219 task 1 solution --- challenge-219/bob-lied/README | 6 +++--- challenge-219/bob-lied/perl/ch-1.pl | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 challenge-219/bob-lied/perl/ch-1.pl diff --git a/challenge-219/bob-lied/README b/challenge-219/bob-lied/README index 3900c8b0ad..a5ad186116 100644 --- a/challenge-219/bob-lied/README +++ b/challenge-219/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 217 by Bob Lied +Solutions to weekly challenge 219 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-217/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-217/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-219/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-219/bob-lied diff --git a/challenge-219/bob-lied/perl/ch-1.pl b/challenge-219/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1383abdad7 --- /dev/null +++ b/challenge-219/bob-lied/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Task 1 Sorted Squares +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a list of numbers. +# Write a script to square each number in the list and return the sorted +# list, increasing order. +# Example 1 Input: @list = (-2, -1, 0, 3, 4) Output: (0, 1, 4, 9, 16) +# Example 2 Input: @list = (5, -4, -1, 3, 6) Output: (1, 9, 16, 25, 36) +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say "(", join(", ", sortedSquares(@ARGV)->@*), ")"; + +sub sortedSquares(@numList) +{ + [ sort { $a <=> $b } map { $_*$_ } @numList ]; +} + +sub runTest +{ + use Test2::V0; + + is( sortedSquares(-2,-1,0,3,4), [0,1,4,9,16], "Exmple 1"); + is( sortedSquares(5,-4,-1,3,6), [1,9,16,25,36], "Exmple 1"); + + done_testing; +} -- cgit From c180b297a4a0e4c8d295a7ebb6a21a123ff38bac Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 17 Jul 2023 01:49:33 +0000 Subject: Challenge 226 Solutions (Raku) --- challenge-226/mark-anderson/raku/ch-1.raku | 10 ++++++++++ challenge-226/mark-anderson/raku/ch-2.raku | 13 +++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 challenge-226/mark-anderson/raku/ch-1.raku create mode 100644 challenge-226/mark-anderson/raku/ch-2.raku diff --git a/challenge-226/mark-anderson/raku/ch-1.raku b/challenge-226/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..529ad97813 --- /dev/null +++ b/challenge-226/mark-anderson/raku/ch-1.raku @@ -0,0 +1,10 @@ +#!/usr/bin/env raku +use Test; + +is shuffle-string('lacelengh', [3,2,0,5,4,8,6,7,1]), 'challenge'; +is shuffle-string('rulepark', [4,7,3,1,0,5,2,6]), 'perlraku'; + +sub shuffle-string($s, @a) +{ + (@a Z=> $s.comb).sort>>.value.join +} diff --git a/challenge-226/mark-anderson/raku/ch-2.raku b/challenge-226/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..b79da74dff --- /dev/null +++ b/challenge-226/mark-anderson/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +is zero-array([1,5,0,3,5]), 3; +is zero-array([0]), 0; +is zero-array([2,1,4,0,3]), 4; + +sub zero-array(@a, $c=0) +{ + @a .= grep(*.so); + return $c unless @a; + zero-array(@a >>->> @a.min, $c.succ) +} -- cgit From 4ed6cd15d3cebf02511c09d804ff9971cb4f7fca Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 16 Jul 2023 23:04:37 -0600 Subject: Solve PWC 226 --- challenge-226/wlmb/blog.txt | 1 + challenge-226/wlmb/perl/ch-1.pl | 18 ++++++++++++++++++ challenge-226/wlmb/perl/ch-2.pl | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 challenge-226/wlmb/blog.txt create mode 100755 challenge-226/wlmb/perl/ch-1.pl create mode 100755 challenge-226/wlmb/perl/ch-2.pl diff --git a/challenge-226/wlmb/blog.txt b/challenge-226/wlmb/blog.txt new file mode 100644 index 0000000000..a529b66670 --- /dev/null +++ b/challenge-226/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/07/16/PWC226/ diff --git a/challenge-226/wlmb/perl/ch-1.pl b/challenge-226/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..2a656a5a26 --- /dev/null +++ b/challenge-226/wlmb/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +# Perl weekly challenge 226 +# Task 1: Shuffle String +# +# See https://wlmb.github.io/2023/07/16/PWC226/#task-1-shuffle-string +use v5.36; +die <<~"FIN" unless @ARGV >= 2; + Usage: $0 string p0 [p1.. ] + to reorder string sending its first character to position + p0 in output, the second to p1 and so on + FIN +my $in = shift; # input string +my @in = split "", $in; # as array of characters +die "Can't have more positions than input characters" unless @ARGV <= @in; +my @out; # output characters +$out[$_] = shift @in for @ARGV; # copy input to output array +my $out = join "", @out; +say "$in @ARGV -> $out"; diff --git a/challenge-226/wlmb/perl/ch-2.pl b/challenge-226/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..7de081d374 --- /dev/null +++ b/challenge-226/wlmb/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +# Perl weekly challenge 226 +# Task 2: Zero Array +# +# See https://wlmb.github.io/2023/07/16/PWC226/#task-2-zero-array +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N0 [N1 ...] + to find how many cycles are required to zero the array N0 N1... + FIN +my @sorted = grep {$_} sort {$a <=> $b} my @in=@ARGV; +my $count=0; +while(@sorted){ + ++$count; + my $first=shift @sorted; + $sorted[$_]-=$first for 0..@sorted-1; + shift @sorted while @sorted and $sorted[0]==0; +} +say "@in -> $count"; -- cgit From bbe945c24027c1940f683a2a29e264275f1cf9bd Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 17 Jul 2023 03:18:44 -0400 Subject: Challenge 226 solutions in Perl by Packy Anderson --- challenge-226/packy-anderson/README | 6 +++++ challenge-226/packy-anderson/perl/task-1.pl | 20 ++++++++++++++++ challenge-226/packy-anderson/perl/task-2.pl | 37 +++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 challenge-226/packy-anderson/README create mode 100755 challenge-226/packy-anderson/perl/task-1.pl create mode 100755 challenge-226/packy-anderson/perl/task-2.pl diff --git a/challenge-226/packy-anderson/README b/challenge-226/packy-anderson/README new file mode 100644 index 0000000000..efde9d38cf --- /dev/null +++ b/challenge-226/packy-anderson/README @@ -0,0 +1,6 @@ +# Solutions by Packy Anderson + +## Perl + +* [Task 1](perl/task-1.pl) +* [Task 2](perl/task-2.pl) \ No newline at end of file diff --git a/challenge-226/packy-anderson/perl/task-1.pl b/challenge-226/packy-anderson/perl/task-1.pl new file mode 100755 index 0000000000..789323434c --- /dev/null +++ b/challenge-226/packy-anderson/perl/task-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use v5.36; + +sub shuffle_string { + my($string, $indices) = @_; + my @chars = split //, $string; # split input string into characters + my @result; + foreach my $index ( @$indices ) { + my $char = shift @chars; # get the next character + $result[$index] = $char; # put the character at that index in the result + } + say "Input: \$string = '$string', \@indices = (" . join(',', @$indices) . ")"; + say "Output: '" . join(q{}, @result) . "'"; +} + +say "Task 1: Shuffle String"; +say "\nExample 1"; +shuffle_string('lacelengh', [3,2,0,5,4,8,6,7,1]); +say "\nExample 2"; +shuffle_string('rulepark', [4,7,3,1,0,5,2,6]); diff --git a/challenge-226/packy-anderson/perl/task-2.pl b/challenge-226/packy-anderson/perl/task-2.pl new file mode 100755 index 0000000000..87271cf504 --- /dev/null +++ b/challenge-226/packy-anderson/perl/task-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use v5.36; + +use List::Util qw( min ); + +sub min_positive { + my @ints = grep { $_ > 0 } @_; # only consider positive numbers + return min @ints; # find smallest, undef if empty list +} + +sub zero_array { + my @ints = @_; + say "Input: \@ints = (" . join(', ', @ints) . ")"; + my @operations; + while ( my $min = min_positive(@ints) ) { + my $op_num = scalar(@operations) + 1; + foreach my $int ( @ints ) { + $int -= $min if $int > 0; + } + push @operations, "operation $op_num: pick $min => (" . join(', ', @ints) . ")"; + } + say "Output: " . scalar(@operations); + if (@operations) { + say ""; + say join "\n", @operations; + } +} + +say "Task 2: Zero Array"; +say "\nExample 1"; +zero_array(1, 5, 0, 3, 5); + +say "\nExample 2"; +zero_array(0); + +say "\nExample 3"; +zero_array(2, 1, 4, 0, 3); -- cgit From 0d1816b8182914236243d82f208774fcd132baf5 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Mon, 17 Jul 2023 04:24:07 -0400 Subject: Challenge 226 solutions in Raku by Packy Anderson --- challenge-226/packy-anderson/README | 7 +++++- challenge-226/packy-anderson/raku/task-1.raku | 18 ++++++++++++++ challenge-226/packy-anderson/raku/task-2.raku | 34 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 challenge-226/packy-anderson/raku/task-1.raku create mode 100755 challenge-226/packy-anderson/raku/task-2.raku diff --git a/challenge-226/packy-anderson/README b/challenge-226/packy-anderson/README index efde9d38cf..e0f3edaf1e 100644 --- a/challenge-226/packy-anderson/README +++ b/challenge-226/packy-anderson/README @@ -3,4 +3,9 @@ ## Perl * [Task 1](perl/task-1.pl) -* [Task 2](perl/task-2.pl) \ No newline at end of file +* [Task 2](perl/task-2.pl) + +## Raku + +* [Task 1](raku/task-1.raku) +* [Task 2](raku/task-2.raku) diff --git a/challenge-226/packy-anderson/raku/task-1.raku b/challenge-226/packy-anderson/raku/task-1.raku new file mode 100755 index 0000000000..8835f8a582 --- /dev/null +++ b/challenge-226/packy-anderson/raku/task-1.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku + +sub shuffle_string ($string, @indices) { + my @chars = $string.split("", :skip-empty); + my @result; + for @indices -> $index { + my $char = shift @chars; # get the next character + @result[$index] = $char; # put the character at that index in the result + } + say "Input: \$string = '$string', \@indices = (" ~ @indices.join(',') ~ ")"; + say "Output: '" ~ @result.join('') ~ "'"; +} + +say "Task 1: Shuffle String"; +say "\nExample 1"; +shuffle_string('lacelengh', (3,2,0,5,4,8,6,7,1)); +say "\nExample 2"; +shuffle_string('rulepark', (4,7,3,1,0,5,2,6)); diff --git a/challenge-226/packy-anderson/raku/task-2.raku b/challenge-226/packy-anderson/raku/task-2.raku new file mode 100755 index 0000000000..1065c220c0 --- /dev/null +++ b/challenge-226/packy-anderson/raku/task-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku + +sub min_positive (@ints) { + my @positive = @ints.grep({ $_ > 0 }); # only consider positive numbers + return unless @positive.elems; # return early if no positive nums + return @positive.reduce(&min); # find smallest +} + +sub zero_array (@ints) { + say "Input: \@ints = (" ~ @ints.join(', ') ~ ")"; + my @operations; + while ( my $min = min_positive(@ints) ) { + my $op_num = @operations.elems + 1; + for @ints <-> $int { + $int -= $min if $int > 0; + } + @operations.push("operation $op_num: pick $min => (" ~ @ints.join(', ') ~ ")"); + } + say "Output: " ~ @operations.elems; + if (@operations) { + say ""; + say @operations.join("\n"); + } +} + +say "Task 2: Zero Array"; +say "\nExample 1"; +zero_array([1, 5, 0, 3, 5]); + +say "\nExample 2"; +zero_array([0]); + +say "\nExample 3"; +zero_array([2, 1, 4, 0, 3]); -- cgit From 63754f47cfab4ebaf2df81a3634e1c993d51798a Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Mon, 17 Jul 2023 22:23:53 +1000 Subject: pwc226 solution in perl --- challenge-226/pokgopun/perl/ch-1.pl | 30 +++++++++++++++++++++++++++ challenge-226/pokgopun/perl/ch-2.pl | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 challenge-226/pokgopun/perl/ch-1.pl create mode 100644 challenge-226/pokgopun/perl/ch-2.pl diff --git a/challenge-226/pokgopun/perl/ch-1.pl b/challenge-226/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..7cec054fc9 --- /dev/null +++ b/challenge-226/pokgopun/perl/ch-1.pl @@ -0,0 +1,30 @@ +### Task 1: Shuffle String +### Submitted by: Mohammad S Anwar +### You are given a string and an array of indices of same length as string. +### +### Write a script to return the string after re-arranging the indices in the correct order. +### +### Example 1 +### Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1) +### Output: 'challenge' +### Example 2 +### Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6) +### Output: 'perlraku' +# +use strict; +use warnings; + +sub sortString{ + my @str = split //, shift; + my %m; + $m{$_[$_]} = $str[$_] foreach 0..$#_; + return join("", @m{0..$#str}); +} + +foreach (["lacelengh",3,2,0,5,4,8,6,7,1], ["rulepark",4,7,3,1,0,5,2,6]){ + printf "Input: \$string = '%s', \@indices = (%s)\nOutput: '%s'\n\n", + $_->[0], + join(",", @{$_}[1..$#{$_}]), + sortString(@$_); +} + diff --git a/challenge-226/pokgopun/perl/ch-2.pl b/challenge-226/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..3b74071a2f --- /dev/null +++ b/challenge-226/pokgopun/perl/ch-2.pl @@ -0,0 +1,41 @@ +### Task 2: Zero Array +### Submitted by: Mohammad S Anwar +### You are given an array of non-negative integers, @ints. +### +### Write a script to return the minimum number of operations to make every element equal zero. +### +### In each operation, you are required to pick a positive number less than or equal to the smallest element in the array, then subtract that from each positive element in the array. +### +### +### Example 1: +### Input: @ints = (1, 5, 0, 3, 5) +### Output: 3 +### +### operation 1: pick 1 => (0, 4, 0, 2, 4) +### operation 2: pick 2 => (0, 2, 0, 0, 2) +### operation 3: pick 2 => (0, 0, 0, 0, 0) +### Example 2: +### Input: @ints = (0) +### Output: 0 +### Example 3: +### Input: @ints = (2, 1, 4, 0, 3) +### Output: 4 +### +### operation 1: pick 1 => (1, 0, 3, 0, 2) +### operation 2: pick 1 => (0, 0, 2, 0, 1) +### operation 3: pick 1 => (0, 0, 1, 0, 0) +### operation 4: pick 1 => (0, 0, 0, 0, 0) +# +use strict; +use warnings; + +sub countReduct{ + my $count = shift; + @_ = sort grep{$_>0} @_; + return $count unless @_; + return countReduct(++$count, map{$_ - $_[0]} @_[1..$#_]); + } +foreach ([1, 5, 0, 3, 5], [0], [2, 1, 4, 0, 3]){ + printf "Input: \@ints = (%s)\nOutput: %d\n\n", join(", ",@$_), countReduct(0,@$_); +} + -- cgit From 36bc0bbe60cf10a1d27fa5edff7312f0f2af8eb1 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 17 Jul 2023 10:00:16 -0400 Subject: Week 226 --- challenge-226/zapwai/perl/ch-1.pl | 11 +++++++++++ challenge-226/zapwai/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 challenge-226/zapwai/perl/ch-1.pl create mode 100644 challenge-226/zapwai/perl/ch-2.pl diff --git a/challenge-226/zapwai/perl/ch-1.pl b/challenge-226/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..e43a821142 --- /dev/null +++ b/challenge-226/zapwai/perl/ch-1.pl @@ -0,0 +1,11 @@ +use v5.30; +my $string = 'lacelengh'; +my @indices = (3,2,0,5,4,8,6,7,1); +say "Input: \$string = $string, \@indices = (" . join(",",@indices) . ")"; +my @S = split "", $string; +my @R; +for my $i (0 .. $#indices) { + $R[$indices[$i]] = $S[$i]; +} +my $ans = join "", @R; +say "Output: $ans"; diff --git a/challenge-226/zapwai/perl/ch-2.pl b/challenge-226/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..733533af0c --- /dev/null +++ b/challenge-226/zapwai/perl/ch-2.pl @@ -0,0 +1,30 @@ +use v5.30; +my @ints = (1,5,0,3,5); +say "Input: \@ints = (" . join(",",@ints) . ")"; +my $cnt = 0; +unless (is_zero(@ints)) { + do { + $cnt++; + slim(\@ints); + } until (is_zero(@ints)); +} +say "Output: $cnt"; + +sub is_zero { + for (0 .. $#_) { + return 0 if ($_[$_] != 0); + } + return 1; +} + +sub slim { + my $r = shift; + my $min = 100; + foreach (@$r) { + next if ($_ == 0); + $min = $_ if ($_ < $min); + } + for my $i (0 .. $#$r) { + $$r[$i] = $$r[$i] - $min unless ($$r[$i] == 0); + } +} -- cgit From 701cc5ab9d4b65039e215ee40278a54f6425151c Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 17 Jul 2023 15:19:00 +0000 Subject: w226 - Task 1 &2 --- challenge-226/perlboy1967/perl/ch1.pl | 34 +++++++++++++++++++++++++++ challenge-226/perlboy1967/perl/ch2.pl | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 challenge-226/perlboy1967/perl/ch1.pl create mode 100755 challenge-226/perlboy1967/perl/ch2.pl diff --git a/challenge-226/perlboy1967/perl/ch1.pl b/challenge-226/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..101492ab79 --- /dev/null +++ b/challenge-226/perlboy1967/perl/ch1.pl @@ -0,0 +1,34 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 226 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-226 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Shuffle String +Submitted by: Mohammad S Anwar + +You are given a string and an array of indices of same length as string. + +Write a script to return the string after re-arranging the indices in the correct order. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub shuffleString ($@) { + my ($i,@c) = (0, split //, shift); + my @o; map {$o[$_] = $c[$i++]} @_; + return join '', @o; +} + +is(shuffleString('lacelengh', 3,2,0,5,4,8,6,7,1),'challenge'); +is(shuffleString('rulepark', 4,7,3,1,0,5,2,6),'perlraku'); + +done_testing; diff --git a/challenge-226/perlboy1967/perl/ch2.pl b/challenge-226/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..d3d39dd8fb --- /dev/null +++ b/challenge-226/perlboy1967/perl/ch2.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 226 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-226 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Zero Array +Submitted by: Mohammad S Anwar + +You are given an array of non-negative integers, @ints. + +Write a script to return the minimum number of operations to make every element equal zero. + +|| In each operation, you are required to pick a positive number less than or equal +|| to the smallest element in the array, then subtract that from each positive element +|| in the array. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub zeroArray (@) { + @_ = grep {$_ > 0} sort {$a <=> $b} @_; + my $n = 0; + while (@_) { + my $i = shift; + @_ = grep {$_ > 0} map {$_ -= $i} @_; + $n++; + } + return $n; +} + +is(zeroArray(1,5,0,3,5),3); +is(zeroArray(0),0); +is(zeroArray(2,1,4,0,3),4); + +done_testing; -- cgit From b3548ad4a10f3bc763482d63f8ee6d30162c7697 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 17 Jul 2023 09:03:10 +0200 Subject: Task 1 done Task 2 done Task 1 PLPerl done Task 2 PLPerl done Task 1 PLPGSQL done Task 2 done Blog references --- challenge-226/luca-ferrari/blog-1.txt | 1 + challenge-226/luca-ferrari/blog-2.txt | 1 + challenge-226/luca-ferrari/blog-3.txt | 1 + challenge-226/luca-ferrari/blog-4.txt | 1 + challenge-226/luca-ferrari/blog-5.txt | 1 + challenge-226/luca-ferrari/blog-6.txt | 1 + challenge-226/luca-ferrari/postgresql/ch-1.plperl | 26 ++++++++++++ challenge-226/luca-ferrari/postgresql/ch-1.sql | 35 +++++++++++++++ challenge-226/luca-ferrari/postgresql/ch-2.plperl | 52 +++++++++++++++++++++++ challenge-226/luca-ferrari/postgresql/ch-2.sql | 48 +++++++++++++++++++++ challenge-226/luca-ferrari/raku/ch-1.p6 | 19 +++++++++ challenge-226/luca-ferrari/raku/ch-2.p6 | 23 ++++++++++ 12 files changed, 209 insertions(+) create mode 100644 challenge-226/luca-ferrari/blog-1.txt create mode 100644 challenge-226/luca-ferrari/blog-2.txt create mode 100644 challenge-226/luca-ferrari/blog-3.txt create mode 100644 challenge-226/luca-ferrari/blog-4.txt create mode 100644 challenge-226/luca-ferrari/blog-5.txt create mode 100644 challenge-226/luca-ferrari/blog-6.txt create mode 100644 challenge-226/luca-ferrari/postgresql/ch-1.plperl create mode 100644 challenge-226/luca-ferrari/postgresql/ch-1.sql create mode 100644 challenge-226/luca-ferrari/postgresql/ch-2.plperl create mode 100644 challenge-226/luca-ferrari/postgresql/ch-2.sql create mode 100644 challenge-226/luca-ferrari/raku/ch-1.p6 create mode 100644 challenge-226/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-226/luca-ferrari/blog-1.txt b/challenge-226/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..4e0cf4347d --- /dev/null +++ b/challenge-226/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task1 diff --git a/challenge-226/luca-ferrari/blog-2.txt b/challenge-226/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..d7bb7b7348 --- /dev/null +++ b/challenge-226/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task2 diff --git a/challenge-226/luca-ferrari/blog-3.txt b/challenge-226/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..fd47d1b612 --- /dev/null +++ b/challenge-226/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task1plperl diff --git a/challenge-226/luca-ferrari/blog-4.txt b/challenge-226/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..4d012ed630 --- /dev/null +++ b/challenge-226/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task2plperl diff --git a/challenge-226/luca-ferrari/blog-5.txt b/challenge-226/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..b7261833f8 --- /dev/null +++ b/challenge-226/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task1plpgsql diff --git a/challenge-226/luca-ferrari/blog-6.txt b/challenge-226/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a882357d91 --- /dev/null +++ b/challenge-226/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/07/17/PerlWeeklyChallenge226.html#task2plpgsql diff --git a/challenge-226/luca-ferrari/postgresql/ch-1.plperl b/challenge-226/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..11c1e51098 --- /dev/null +++ b/challenge-226/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,26 @@ +-- +-- Perl Weekly Challenge 226 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc226; + +CREATE OR REPLACE FUNCTION +pwc226.task1_plperl( text, int[] ) +RETURNS text +AS $CODE$ + my ( $string, $indexes ) = @_; + my ( $index ) = 0; + my $letters = {}; + + for ( split( //, $string ) ) { + $letters->{ $indexes->[ $index++ ] } = $_; + } + + my @chars; + push @chars, $letters->{ $_ } for ( sort( $indexes->@* ) ); + return join( '', @chars ); + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-226/luca-ferrari/postgresql/ch-1.sql b/challenge-226/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..effcc70f2d --- /dev/null +++ b/challenge-226/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,35 @@ +-- +-- Perl Weekly Challenge 226 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc226; + +CREATE OR REPLACE FUNCTION +pwc226.task1_plpgsql( word text, indexes int[] ) +RETURNS text +AS $CODE$ +DECLARE + i int := 1; + final_word text := ''; +BEGIN + CREATE TEMPORARY TABLE IF NOT EXISTS word( letter char, original_index int ); + TRUNCATE word; + + INSERT INTO word( letter, original_index ) + SELECT l, row_number() over () + FROM regexp_split_to_table( word, '' ) l; + + FOREACH i IN ARRAY indexes LOOP + SELECT final_word || l.letter + INTO final_word + FROM word l + WHERE l.original_index = i; + END LOOP; + + RETURN final_word; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-226/luca-ferrari/postgresql/ch-2.plperl b/challenge-226/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..903a5c58ba --- /dev/null +++ b/challenge-226/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,52 @@ +-- +-- Perl Weekly Challenge 226 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc226; + +CREATE OR REPLACE FUNCTION +pwc226.task2_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $numbers ) = @_; + my $moves = 0; + + # inner function to get the min value + # non zero in the array + my $min = sub { + my $min = undef; + for ( $_[0]->@* ) { + next if $_ == 0; + $min = $_ if ! $min || $_ < $min; + } + + return $min; + }; + + # inner function to see if the array + # if full of zeros + my $is_empty = sub { + my ( $array ) = @_; + return scalar( grep( { $_ == 0 } $array->@* ) ) == scalar( $array->@* ); + }; + + + + + while ( ! $is_empty->( $numbers ) ) { + my $removing = $min->( $numbers ); + $moves++; + + for my $index ( 0 .. $numbers->@* ) { + next if $numbers->[ $index ] == 0; + $numbers->[ $index ] -= $removing; + } + } + + + return $moves; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-226/luca-ferrari/postgresql/ch-2.sql b/challenge-226/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..98906f8b9f --- /dev/null +++ b/challenge-226/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,48 @@ +-- +-- Perl Weekly Challenge 226 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc226; + +CREATE OR REPLACE FUNCTION +pwc226.task2_plpgsql( nums int[] ) +RETURNS int +AS $CODE$ +DECLARE + moves int := 0; + removing int := 0; + i int; +BEGIN + + FOUND := true; + + WHILE FOUND LOOP + -- get the nex min value + SELECT min( n ) + INTO removing + FROM unnest( nums ) n + WHERE n > 0; + + -- stop (?) + IF NOT FOUND OR removing IS NULL THEN + EXIT; + ELSE + moves := moves + 1; + END IF; + + FOR i IN 1 .. array_length( nums, 1 ) LOOP + IF nums[ i ] = 0 THEN + CONTINUE; + END IF; + + nums[ i ] = nums[ i ] - removing; + END LOOP; + END LOOP; + + RETURN moves; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-226/luca-ferrari/raku/ch-1.p6 b/challenge-226/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..426efd2a2c --- /dev/null +++ b/challenge-226/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#!raku + +# +# Perl Weekly Challenge 226 +# Task 1 +# +# See +# + +sub MAIN( Str $string, *@indexes + where { @indexes.grep( * ~~ Int ).elems == @indexes.elems && @indexes.elems == $string.chars } + ) { + my $index = 0; + my %letters; + %letters{ @indexes[ $index++ ] } = $_ for $string.comb; + + %letters{ @indexes.sort }.join.say; + +} diff --git a/challenge-226/luca-ferrari/raku/ch-2.p6 b/challenge-226/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..002a936e1d --- /dev/null +++ b/challenge-226/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,23 @@ +#!raku + +# +# Perl Weekly Challenge 226 +# Task 2 +# +# See +# + +sub MAIN( *@numbers is copy where { @numbers.grep( * ~~ Int && * >= 0 ).elems == @numbers.elems } ) { + my $moves; + + while ( @numbers.grep( * == 0 ).elems != @numbers.elems ) { + my $removing = @numbers.grep( * > 0 ).min; + $moves++; + for 0 ..^ @numbers.elems { + next if ! @numbers[ $_ ]; + @numbers[ $_ ] -= $removing; + } + } + + $moves.say; +} -- cgit From e24b1ace4f5574fc8fd9a0856798d88643af8361 Mon Sep 17 00:00:00 2001 From: Bob Lied Date: Mon, 17 Jul 2023 12:51:50 -0500 Subject: Week 219 task 2 --- challenge-219/bob-lied/perl/ch-2.pl | 130 ++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 challenge-219/bob-lied/perl/ch-2.pl diff --git a/challenge-219/bob-lied/perl/ch-2.pl b/challenge-219/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..3aec5f604e --- /dev/null +++ b/challenge-219/bob-lied/perl/ch-2.pl @@ -0,0 +1,130 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Task 2 Travel Expenditure +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given two list, @costs and @days. +# The list @costs contains the cost of three different types of travel cards +# you can buy. For example @costs = (5, 30, 90) +# +# Index 0 element represent the cost of 1 day travel card. +# Index 1 element represent the cost of 7 days travel card. +# Index 2 element represent the cost of 30 days travel card. +# +# The list @days contains the day number you want to travel in the year. +# For example: @days = (1, 3, 4, 5, 6) +# +# The above example means you want to travel on day 1, day 3, day 4, day 5 +# and day 6 of the year. +# +# Write a script to find the minimum travel cost. +# +# Example 1: Input: @costs = (2, 7, 25) +# @days = (1, 5, 6, 7, 9, 15) +# Output: 11 +# On day 1, we buy a one day pass for 2 which would cover the day 1. +# On day 5, we buy seven days pass for 7 which would cover days 5 - 9. +# On day 15, we buy a one day pass for 2 which would cover the day 15. +# So the total cost is 2 + 7 + 2 => 11. +# +# Example 2: Input: @costs = (2, 7, 25) +# @days = (1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31) +# Output: 20 +# On day 1, we buy a seven days pass for 7 which would cover days 1 - 7. +# On day 10, we buy a seven days pass for 7 which would cover days 10 - 14. +# On day 20, we buy a one day pass for 2 which would cover day 20. +# On day 30, we buy a one day pass for 2 which would cover day 30. +# On day 31, we buy a one day pass for 2 which would cover day 31. +# So the total cost is 7 + 7 + 2 + 2 + 2 => 20. +#============================================================================= + +use v5.36; + +use List::Util qw/min max/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +use constant { _1_DAY_PASS => 0, _7_DAY_PASS => 1, _30_DAY_PASS => 2 }; + +my $MinCost; + +sub _travel($pass, $days, $costSoFar, $passesUsed, $indent) +{ + return if @$days == 0; + return if $costSoFar >= $MinCost; + + + for my $passLength ( keys %{$pass} ) + { + my $remainingDays = [ grep { $_ >= $days->[0] + $passLength } @$days ]; + + say "${indent}TRY pass=$passLength sofar=$costSoFar days=[$days->@*] --> [$remainingDays->@*]" if $Verbose; + + if ( scalar(@$remainingDays) == 0 ) + { + # Total coverage achieved. + my $total = $costSoFar + $pass->{$passLength}; + print "${indent}FINISH cost=$total " if $Verbose; + if ( $total < $MinCost ) + { + $MinCost = $total; + say "${indent}BETTER Min=$MinCost seq=[@$passesUsed $passLength]" if $Verbose; + } + } + else + { + _travel($pass, $remainingDays, $costSoFar + $pass->{$passLength}, [ $passesUsed->@*, $passLength ], "| $indent"); + } + } +} + +sub travel +{ + my %args = @_; + my $days = [ sort { $a <=> $b } $args{days}->@* ]; + + my %pass = ( 1 => $args{costs}->[ _1_DAY_PASS], + 7 => $args{costs}->[ _7_DAY_PASS], + 30 => $args{costs}->[_30_DAY_PASS] ); + + # Start by assuming maximum possible cost. + $MinCost = max( values %pass ) * scalar(@$days); + + _travel( \%pass, $days, 0, [], ""); + + return $MinCost; +} + + +sub runTest +{ + use Test2::V0; + + is( travel( costs => [2, 7, 25], + days => [1, 5, 6, 7, 9, 15] ), + 11, "Example 1"); + is( travel( costs => [2, 7, 25], + days => [1, 2, 3, 5, 7, 10, 11, 12, 13, 14, 20, 30, 31] ), + 20, "Example 2"); + + is( travel( costs => [2, 7, 25], + days => [1 .. 30] ), + 25, "One month" ); + + is( travel( costs => [2, 7, 25], + days => [1 .. 30, 101 .. 130] ), + 50, "Two months" ); + + is( travel( costs => [2, 7, 25], + days => [1 .. 30, 101 .. 130, 200] ), + 52, "Two months and a day" ); + + done_testing; +} -- cgit From 7175690b762a97dc9ddf9a5893c177e09182fda3 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 17 Jul 2023 19:41:21 +0100 Subject: add solution week 226 task 1 in perl --- challenge-226/steven-wilson/perl/ch-01.pl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenge-226/steven-wilson/perl/ch-01.pl diff --git a/challenge-226/steven-wilson/perl/ch-01.pl b/challenge-226/steven-wilson/perl/ch-01.pl new file mode 100644 index 0000000000..3abd2b581b --- /dev/null +++ b/challenge-226/steven-wilson/perl/ch-01.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More; + +my $t_s1 = "lacelengh"; +my @t_i1 = (3,2,0,5,4,8,6,7,1); +my $t_s2 = "rulepark"; +my @t_i2 = (4,7,3,1,0,5,2,6); +cmp_ok( reorder_string( $t_s1, @t_i1 ), "eq", "challenge", "Example 1"); +cmp_ok( reorder_string( $t_s2, @t_i2 ), "eq", "perlraku", "Example 2"); +done_testing(); + +sub reorder_string { + my ($s, @i) = @_; + my @return_s; + for ( 0 .. (length ($s) - 1) ){ + $return_s[ $i[$_] ] = substr( $s, $_, 1 ); + } + local $" = ''; + return "@return_s"; +} + -- cgit From d85757e84a6aa5a5905026c7370dd39dd3b112a1 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 17 Jul 2023 20:49:55 +0000 Subject: Task 2 - Simplified code (one-liner version) --- challenge-226/perlboy1967/perl/ch2a.pl | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 challenge-226/perlboy1967/perl/ch2a.pl diff --git a/challenge-226/perlboy1967/perl/ch2a.pl b/challenge-226/perlboy1967/perl/ch2a.pl new file mode 100755 index 0000000000..9cf5f99338 --- /dev/null +++ b/challenge-226/perlboy1967/perl/ch2a.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 226 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-226 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Zero Array +Submitted by: Mohammad S Anwar + +You are given an array of non-negative integers, @ints. + +Write a script to return the minimum number of operations to make every element equal zero. + +|| In each operation, you are required to pick a positive number less than or equal +|| to the smallest element in the array, then subtract that from each positive element +|| in the array. + +=cut + +use v5.16; + +use common::sense; + +use List::Util qw(uniq); + +use Test::More; + +sub zeroArray (@) { + scalar grep {$_ > 0} uniq sort {$a <=> $b} @_; +} + +is(zeroArray(1,5,0,3,5),3); +is(zeroArray(0),0); +is(zeroArray(2,1,4,0,3),4); + +done_testing; -- cgit From 656827e4ac316e9fcc14b2900e4a9190add34341 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 17 Jul 2023 20:54:28 +0000 Subject: Drop 'sort()' - Not needed --- challenge-226/perlboy1967/perl/ch2a.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-226/perlboy1967/perl/ch2a.pl b/challenge-226/perlboy1967/perl/ch2a.pl index 9cf5f99338..abdb7db7d2 100755 --- a/challenge-226/perlboy1967/perl/ch2a.pl +++ b/challenge-226/perlboy1967/perl/ch2a.pl @@ -29,7 +29,7 @@ use List::Util qw(uniq); use Test::More; sub zeroArray (@) { - scalar grep {$_ > 0} uniq sort {$a <=> $b} @_; + scalar grep {$_ > 0} uniq @_; } is(zeroArray(1,5,0,3,5),3); -- cgit From ac1ceaeb89d4a637478cc0bcd402fdd62bf31464 Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Mon, 17 Jul 2023 18:10:16 -0400 Subject: Challenge 226 by Jaldhar H. Vyas. --- challenge-226/jaldhar-h-vyas/blog.txt | 1 + challenge-226/jaldhar-h-vyas/perl/ch-1.pl | 14 ++++++++++++++ challenge-226/jaldhar-h-vyas/perl/ch-2.pl | 14 ++++++++++++++ challenge-226/jaldhar-h-vyas/raku/ch-1.raku | 15 +++++++++++++++ challenge-226/jaldhar-h-vyas/raku/ch-2.raku | 15 +++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 challenge-226/jaldhar-h-vyas/blog.txt create mode 100755 challenge-226/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-226/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-226/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-226/jaldhar-h-vyas/raku/ch-2.raku diff --git a/challenge-226/jaldhar-h-vyas/blog.txt b/challenge-226/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..a8cf3ad0c1 --- /dev/null +++ b/challenge-226/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/07/perl_weekly_challenge_week_226.html diff --git a/challenge-226/jaldhar-h-vyas/perl/ch-1.pl b/challenge-226/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..e0e87b1ab9 --- /dev/null +++ b/challenge-226/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my $string = shift @ARGV; +my @indices = @ARGV; +my @letters = split //, $string; +my @result; + +for my $i (0 .. scalar @indices - 1) { + $result[$indices[$i]] = $letters[$i]; +} + +say join q{}, @result; diff --git a/challenge-226/jaldhar-h-vyas/perl/ch-2.pl b/challenge-226/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..f3acb28dea --- /dev/null +++ b/challenge-226/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @ints = @ARGV; +my $ops = 0; + +while (scalar grep {$_ != 0} @ints) { + my $pick = [sort { $a <=> $b } grep { $_ != 0 } @ints]->[0]; + @ints = map { $_ == 0 ? 0 : $_ - $pick } @ints; + $ops++; +} + +say $ops; diff --git a/challenge-226/jaldhar-h-vyas/raku/ch-1.raku b/challenge-226/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..768220ec6a --- /dev/null +++ b/challenge-226/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/raku + +sub MAIN( + $string, + *@indices +) { + my @letters = $string.comb; + my @result; + + for @letters Z @indices -> ($letter, $index) { + @result[$index] = $letter; + } + + @result.join.say; +} \ No newline at end of file diff --git a/challenge-226/jaldhar-h-vyas/raku/ch-2.raku b/challenge-226/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..89927ba413 --- /dev/null +++ b/challenge-226/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/usr/bin/raku + +sub MAIN( + *@ints +) { + my $ops = 0; + + until @ints.all == 0 { + my $pick = @ints.grep({ $_ != 0 }).min; + @ints = @ints.map({ $_ == 0 ?? 0 !! $_ - $pick }); + $ops++; + } + + say $ops; +} \ No newline at end of file -- cgit From de0487a6cf01004fb38370fe5bec2b4c51ddc23e Mon Sep 17 00:00:00 2001 From: arnesom Date: Tue, 18 Jul 2023 00:28:41 +0200 Subject: Arne Sommer --- challenge-226/arne-sommer/blog.txt | 1 + challenge-226/arne-sommer/raku/ch-1.raku | 16 ++++++++++++++++ challenge-226/arne-sommer/raku/ch-2.raku | 5 +++++ challenge-226/arne-sommer/raku/shuffle-string | 16 ++++++++++++++++ challenge-226/arne-sommer/raku/shuffle-string-slice | 8 ++++++++ challenge-226/arne-sommer/raku/zero-array | 19 +++++++++++++++++++ challenge-226/arne-sommer/raku/zero-array-sans-zero | 21 +++++++++++++++++++++ 7 files changed, 86 insertions(+) create mode 100644 challenge-226/arne-sommer/blog.txt create mode 100755 challenge-226/arne-sommer/raku/ch-1.raku create mode 100755 challenge-226/arne-sommer/raku/ch-2.raku create mode 100755 challenge-226/arne-sommer/raku/shuffle-string create mode 100755 challenge-226/arne-sommer/raku/shuffle-string-slice create mode 100755 challenge-226/arne-sommer/raku/zero-array create mode 100755 challenge-226/arne-sommer/raku/zero-array-sans-zero diff --git a/challenge-226/arne-sommer/blog.txt b/challenge-226/arne-sommer/blog.txt new file mode 100644 index 0000000000..8c4d3d6984 --- /dev/null +++ b/challenge-226/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/zero-shuffle.html diff --git a/challenge-226/arne-sommer/raku/ch-1.raku b/challenge-226/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..d7ee199ebb --- /dev/null +++ b/challenge-226/arne-sommer/raku/ch-1.raku @@ -0,0 +1,16 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string where $string.chars > 0, + *@indices where @indices.elems == @indices.unique.elems == $string.chars + && all(@indices) ~~ UInt && all(@indices) < $string.chars); + +my @string = $string.comb; + +my @out; + +for ^@string.elems -> $i +{ + @out[@indices[$i]] = @string[$i] +} + +say @out.join; diff --git a/challenge-226/arne-sommer/raku/ch-2.raku b/challenge-226/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..7d1b007de1 --- /dev/null +++ b/challenge-226/arne-sommer/raku/ch-2.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ints where all(@ints) ~~ UInt); + +say @ints.grep( * > 0).unique.elems; diff --git a/challenge-226/arne-sommer/raku/shuffle-string b/challenge-226/arne-sommer/raku/shuffle-string new file mode 100755 index 0000000000..d7ee199ebb --- /dev/null +++ b/challenge-226/arne-sommer/raku/shuffle-string @@ -0,0 +1,16 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string where $string.chars > 0, + *@indices where @indices.elems == @indices.unique.elems == $string.chars + && all(@indices) ~~ UInt && all(@indices) < $string.chars); + +my @string = $string.comb; + +my @out; + +for ^@string.elems -> $i +{ + @out[@indices[$i]] = @string[$i] +} + +say @out.join; diff --git a/challenge-226/arne-sommer/raku/shuffle-string-slice b/challenge-226/arne-sommer/raku/shuffle-string-slice new file mode 100755 index 0000000000..b9aeda5c1d --- /dev/null +++ b/challenge-226/arne-sommer/raku/shuffle-string-slice @@ -0,0 +1,8 @@ +#! /usr/bin/env raku + +unit sub MAIN ($string where $string.chars > 0, + *@indices where @indices.elems == $string.chars); + +my @string = $string.comb; + +say @string[@indices].join; diff --git a/challenge-226/arne-sommer/raku/zero-array b/challenge-226/arne-sommer/raku/zero-array new file mode 100755 index 0000000000..b50cf1d703 --- /dev/null +++ b/challenge-226/arne-sommer/raku/zero-array @@ -0,0 +1,19 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ints where all(@ints) ~~ UInt, + :v(:$verbose)); + +my $operations = 0; + +while @ints.sum > 0 +{ + $operations++; + my $smallest = @ints.grep( * > 0).min; + my @new = @ints.map({ $_ > 0 ?? $_ - $smallest !! 0 }); + + say ":Ints @ints[] -> smallest: $smallest -> @new[]" if $verbose; + + @ints = @new; +} + +say $operations; \ No newline at end of file diff --git a/challenge-226/arne-sommer/raku/zero-array-sans-zero b/challenge-226/arne-sommer/raku/zero-array-sans-zero new file mode 100755 index 0000000000..acdba448bc --- /dev/null +++ b/challenge-226/arne-sommer/raku/zero-array-sans-zero @@ -0,0 +1,21 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@ints where all(@ints) ~~ UInt, + :v(:$verbose)); + +my $operations = 0; + +@ints = @ints.grep( * > 0 ); + +while @ints.elems > 0 +{ + my $smallest = @ints.min; + my @new = @ints.map({ $_ - $smallest }); + + say ":Ints @ints[] - smallest: $smallest -> @new[]" if $verbose; + $operations++; + + @ints = @new.grep( * > 0 ); +} + +say $operations; -- cgit From ea0800aa408563129c40cfba8cd3a1383a5d988f Mon Sep 17 00:00:00 2001 From: "e. alvarez" <55966724+ealvar3z@users.noreply.github.com> Date: Mon, 17 Jul 2023 16:07:45 -0700 Subject: submit th