diff options
| -rw-r--r-- | challenge-217/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-217/bob-lied/perl/ch-1.pl | 53 | ||||
| -rw-r--r-- | challenge-217/bob-lied/perl/ch-2.pl | 47 |
3 files changed, 103 insertions, 3 deletions
diff --git a/challenge-217/bob-lied/README b/challenge-217/bob-lied/README index 386b265dbc..3900c8b0ad 100644 --- a/challenge-217/bob-lied/README +++ b/challenge-217/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 215 by Bob Lied +Solutions to weekly challenge 217 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-217/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-217/bob-lied diff --git a/challenge-217/bob-lied/perl/ch-1.pl b/challenge-217/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..1766e07ee1 --- /dev/null +++ b/challenge-217/bob-lied/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/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 Matrix +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a n x n matrix where n >= 2. +# Write a script to find 3rd smallest element in the sorted matrix. +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub thirdSmallest($matrix) +{ + my @smallest; + for my $r ( @$matrix ) + { + for my $c ( 0 .. $r->$#* ) + { + # Put new value at end and shuffle down into place + push @smallest, $r->[$c]; + for ( my $p = $#smallest; $#smallest > 0 && $p > 0 ; $p-- ) + { + if ( $smallest[$p] < $smallest[$p-1] ) + { + @smallest[$p-1,$p] = @smallest[$p, $p-1]; + } + } + pop @smallest if @smallest > 3; + say "smallest=[@smallest]" if $Verbose; + } + } + return $smallest[2]; +} + +sub runTest +{ + use Test2::V0; + + is(thirdSmallest( [[3,1,2], [5,2,4], [0,1,3] ] ), 1, "Example 1"); + is(thirdSmallest( [[2, 1], [4, 5]] ), 4, "Example 2"); + is(thirdSmallest( [[1,0,3], [0,0,0], [1,2,1]] ), 0, "Example 3"); + + done_testing; +} diff --git a/challenge-217/bob-lied/perl/ch-2.pl b/challenge-217/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..d58ce7fcff --- /dev/null +++ b/challenge-217/bob-lied/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/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 Max Number +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given a list of positive integers. +# Write a script to concatenate the integers to form the highest possible value. +# Example 1: Input: @list = (1, 23), 231 +# Example 2: Input: @list = (10, 3, 2), 3210 +# Example 3: Input: @list = (31, 2, 4, 10), 431210 +# Example 4: Input: @list = (5, 11, 4, 1, 2), 542111 +# Example 5: Input: @list = (1, 10), 110 +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub maxNumber(@list) +{ + my @sorted = sort { "$b$a" cmp "$a$b" } @list; + say "sorted: [@sorted]" if $Verbose; + return 0 + join "", @sorted; +} + +sub runTest +{ + use Test2::V0; + + is( maxNumber(1, 23 ), 231, 'Example 1'); + is( maxNumber(10, 3, 2 ), 3210, 'Example 2'); + is( maxNumber(31, 2, 4, 10 ), 431210, 'Example 3'); + is( maxNumber(5, 11, 4, 1, 2), 542111, 'Example 4'); + is( maxNumber(1, 10 ), 110, 'Example 5'); + is( maxNumber(1, 10, 100 ), 110100, 'Example 6'); + is( maxNumber(7, 0, 0 ), 700, 'With a 0'); + is( maxNumber(0, 0, 0 ), 0, 'All zero'); + + done_testing; +} |
