diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-15 02:59:42 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-01-15 02:59:42 +0000 |
| commit | e5b8e09d76891f87a79bdef3444725ec2882dade (patch) | |
| tree | f6b4fab88bf400868b20b13bf7b799363a9af1b1 | |
| parent | f89b725f3ae9d78e0bc206e5bf5e1b8a3189c7bc (diff) | |
| download | perlweeklychallenge-club-e5b8e09d76891f87a79bdef3444725ec2882dade.tar.gz perlweeklychallenge-club-e5b8e09d76891f87a79bdef3444725ec2882dade.tar.bz2 perlweeklychallenge-club-e5b8e09d76891f87a79bdef3444725ec2882dade.zip | |
- Added solutions by Packy Anderson.
24 files changed, 2896 insertions, 2624 deletions
diff --git a/challenge-251/packy-anderson/README.md b/challenge-251/packy-anderson/README.md index 36982b3432..a796e42c76 100644 --- a/challenge-251/packy-anderson/README.md +++ b/challenge-251/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Two-Hundred Fifty Perl Weekly Challenges! Two-Hundred Fifty problems so clear...](https://packy.dardan.com/b/G4) +[Luck Concatenate Numbers Tonight!](https://packy.dardan.com/b/GL) diff --git a/challenge-251/packy-anderson/blog.txt b/challenge-251/packy-anderson/blog.txt new file mode 100644 index 0000000000..0e3f4b270a --- /dev/null +++ b/challenge-251/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2024/01/08/perl-weekly-challenge-luck-concatenate-numbers-tonight diff --git a/challenge-251/packy-anderson/perl/ch-1.pl b/challenge-251/packy-anderson/perl/ch-1.pl new file mode 100644 index 0000000000..677958602d --- /dev/null +++ b/challenge-251/packy-anderson/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.38; + +sub concatenationValue(@ints) { + return 0 if @ints == 0; # no elements + return $ints[0] if @ints == 1; # one element + + my $first = shift @ints; # first element + my $last = pop @ints; # last element + my $concat = "$first$last"+0; # concatenate and convert + + return $concat + concatenationValue(@ints); +} + +sub solution(@ints) { + say 'Input: @ints = (' . join(', ', @ints) . ')'; + my $value = concatenationValue(@ints); + say "Output: $value"; +} + +say "Example 1:"; +solution(6, 12, 25, 1); + +say "\nExample 2:"; +solution(10, 7, 31, 5, 2, 2); + +say "\nExample 3:"; +solution(1, 2, 10); diff --git a/challenge-251/packy-anderson/perl/ch-2.pl b/challenge-251/packy-anderson/perl/ch-2.pl new file mode 100644 index 0000000000..17c9008360 --- /dev/null +++ b/challenge-251/packy-anderson/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl +use v5.38; + +use Array::Utils qw( intersect ); +use List::Util qw( min max ); + +sub maxCols(@matrix) { + my @rotated; # rotate cols to rows + foreach my $row ( @matrix ) { + foreach my $k ( 0 .. $#{$row} ) { + my $v = $row->[$k]; + push @{$rotated[$k]}, $v; + } + } + # return the max of the now rows! + return map { max( @$_ ) } @rotated; +} + +sub luckyNumber(@matrix) { + my @minRows = map { min( @$_ ) } @matrix; + my @maxCols = maxCols(@matrix); + # intersect() returns an array, so get the first element + return (intersect( @minRows, @maxCols ))[0] // -1; +} + +sub formatMatrix(@matrix) { + my $indent = 17; + my @output; + foreach my $row ( @matrix ) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', map { sprintf "%2d", $_ } @$row); + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} +sub solution(@matrix) { + say 'Input: $matrix = ' . formatMatrix(@matrix); + my $lucky = luckyNumber(@matrix); + say "Output: $lucky"; +} + +say "Example 1:"; +solution([ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]); + +say "\nExample 2:"; +solution([ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12]); + +say "\nExample 3:"; +solution([7, 8], + [1, 2]); diff --git a/challenge-251/packy-anderson/python/ch-1.py b/challenge-251/packy-anderson/python/ch-1.py new file mode 100644 index 0000000000..4e6bab831c --- /dev/null +++ b/challenge-251/packy-anderson/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +def concatenationValue(ints): + if len(ints) == 0: # no elements + return 0 + if len(ints) == 1: # one element + return ints[0] + + first = ints.pop(0); # first element + last = ints.pop(-1); # last element + concat = int(f"{first}{last}") # concatenate and convert + + return concat + concatenationValue(ints) + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(ints): + print(f'Input: @ints = ({comma_join(ints)})') + value = concatenationValue(ints) + print(f'Output: {value}') + +print('Example 1:') +solution([6, 12, 25, 1]) + +print('\nExample 2:') +solution([10, 7, 31, 5, 2, 2]) + +print('\nExample 3:') +solution([1, 2, 10]) diff --git a/challenge-251/packy-anderson/python/ch-2.py b/challenge-251/packy-anderson/python/ch-2.py new file mode 100644 index 0000000000..1c5e8ca761 --- /dev/null +++ b/challenge-251/packy-anderson/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +def maxCols(matrix): + # initialize rotated with empty row for each column + rotated = [ [] for col in matrix[0] ] + for row in matrix: + for k, v in enumerate(row): + rotated[k].append(v) + # return the max of the now rows! + return [ max(row) for row in rotated ] + +def luckyNumber(matrix): + mRows = [ min(row) for row in matrix ] + mCols = maxCols(matrix) + intersection = list( set(mRows) & set(mCols) ) + if intersection: + return intersection[0] + else: + return -1 + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def formatMatrix(matrix, indent=17): + output = [] + for row in matrix: + output_row = ' ' * indent + ' [' + output_row += ', '.join(map(lambda i: str(i), row)) + output_row += ']' + output.append(output_row) + + return( + "[\n" + ",\n".join(output) + "\n" + + ' ' * indent + ']' + ) + +def solution(matrix): + print(f'Input: $matrix = {formatMatrix(matrix)}') + print(f'Output: {luckyNumber(matrix)}') + +print('Example 1:') +solution([ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]) + +print('\nExample 2:') +solution([ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]) + +print('\nExample 3:') +solution([ [7, 8], + [1, 2] ]) diff --git a/challenge-251/packy-anderson/raku/ch-1.raku b/challenge-251/packy-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..66eaae73d6 --- /dev/null +++ b/challenge-251/packy-anderson/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku +use v6; + +sub concatenationValue(@ints) { + return 0 if @ints.elems == 0; # no elements + return @ints[0] if @ints.elems == 1; # one element + + my $first = @ints.shift; # first element + my $last = @ints.pop; # last element + my $concat = "$first$last".Int; # concatenate and convert + + return $concat + concatenationValue(@ints); +} + +sub solution(@ints) { + say 'Input: @ints = (' ~ @ints.join(', ') ~ ')'; + my $value = concatenationValue(@ints); + say "Output: $value"; +} + +say "Example 1:"; +solution([6, 12, 25, 1]); + +say "\nExample 2:"; +solution([10, 7, 31, 5, 2, 2]); + +say "\nExample 3:"; +solution([1, 2, 10]); diff --git a/challenge-251/packy-anderson/raku/ch-2.raku b/challenge-251/packy-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..831a0d927f --- /dev/null +++ b/challenge-251/packy-anderson/raku/ch-2.raku @@ -0,0 +1,52 @@ +#!/usr/bin/env raku +use v6; + +sub maxCols(@matrix) { + my @rotated; # rotate cols to rows + for @matrix -> @row { + for @row.kv -> $k, $v { + @rotated[$k].push($v); + } + } + # return the max of the now rows! + return @rotated.map({ $_.max() }); +} + +sub luckyNumber(@matrix) { + my @minRows = @matrix.map({ $_.min() }); + my @maxCols = maxCols(@matrix); + return ( @minRows (&) @maxCols ) // -1; +} + +sub formatMatrix(@matrix, $indent) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.map({ sprintf "%2d", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} +sub solution(@matrix) { + say 'Input: $matrix = ' ~ formatMatrix(@matrix, 17); + my $lucky = luckyNumber(@matrix); + say "Output: $lucky"; +} + +say "Example 1:"; +solution([ [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] ]); + +say "\nExample 2:"; +solution([ [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] ]); + +say "\nExample 3:"; +solution([ [7, 8], + [1, 2] ]); diff --git a/stats/pwc-current.json b/stats/pwc-current.json index a7314ef1ac..4c5e4431fc 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,18 +1,211 @@ { + "series" : [ + { + "data" : [ + { + "y" : 2, + "drilldown" : "Adam Russell", + "name" : "Adam Russell" + }, + { + "y" : 3, + "drilldown" : "Arne Sommer", + "name" : "Arne Sommer" + }, + { + "drilldown" : "Athanasius", + "name" : "Athanasius", + "y" : 4 + }, + { + "drilldown" : "BarrOff", + "name" : "BarrOff", + "y" : 2 + }, + { + "name" : "Bob Lied", + "drilldown" : "Bob Lied", + "y" : 2 + }, + { + "drilldown" : "Bruce Gray", + "name" : "Bruce Gray", + "y" : 2 + }, + { + "name" : "Cheok-Yin Fung", + "drilldown" : "Cheok-Yin Fung", + "y" : 2 + }, + { + "y" : 3, + "drilldown" : "Dave Jacoby", + "name" : "Dave Jacoby" + }, + { + "drilldown" : "David Ferrone", + "name" : "David Ferrone", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "E. Choroba", + "name" : "E. Choroba" + }, + { + "y" : 5, + "drilldown" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas" + }, + { + "name" : "Jan Krnavek", + "drilldown" : "Jan Krnavek", + "y" : 2 + }, + { + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey", + "y" : 3 + }, + { + "y" : 6, + "drilldown" : "Laurent Rosenfeld", + "name" : "Laurent Rosenfeld" + }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 4 + }, + { + "y" : 10, + "name" : "Luca Ferrari", + "drilldown" : "Luca Ferrari" + }, + { + "name" : "Mark Anderson", + "drilldown" : "Mark Anderson", + "y" : 2 + }, + { + "name" : "Matthew Neleigh", + "drilldown" : "Matthew Neleigh", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Mustafa Aydin", + "name" : "Mustafa Aydin" + }, + { + "y" : 2, + "name" : "Nelo Tovar", + "drilldown" : "Nelo Tovar" + }, + { + "name" : "Niels van Dijke", + "drilldown" : "Niels van Dijke", + "y" : 2 + }, + { + "drilldown" : "Packy Anderson", + "name" : "Packy Anderson", + "y" : 5 + }, + { + "y" : 3, + "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith" + }, + { + "y" : 2, + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "name" : "Robbie Hatley", + "drilldown" : "Robbie Hatley", + "y" : 3 + }, + { + "drilldown" : "Robert Ransbottom", + "name" : "Robert Ransbottom", + "y" : 2 + }, + { + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West", + "y" : 5 + }, + { + "name" : "Scott Sotka", + "drilldown" : "Scott Sotka", + "y" : 2 + }, + { + "y" : 3, + "drilldown" : "Simon Green", + "name" : "Simon Green" + }, + { + "y" : 2, + "drilldown" : "Simon Proctor", + "name" : "Simon Proctor" + }, + { + "y" : 3, + "name" : "Stephen G. Lynn", + "drilldown" : "Stephen G. Lynn" + }, + { + "drilldown" : "Thomas Kohler", + "name" : "Thomas Kohler", + "y" : 4 + }, + { + "y" : 4, + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke" + }, + { + "y" : 3, + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan" + } + ], + "colorByPoint" : 1, + "name" : "The Weekly Challenge - 251" + } + ], + "tooltip" : { + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "followPointer" : 1, + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + }, + "legend" : { + "enabled" : 0 + }, + "chart" : { + "type" : "column" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, "drilldown" : { "series" : [ { - "id" : "Adam Russell", - "name" : "Adam Russell", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "Adam Russell", + "name" : "Adam Russell" }, { - "id" : "Arne Sommer", "name" : "Arne Sommer", "data" : [ [ @@ -23,9 +216,12 @@ "Blog", 1 ] - ] + ], + "id" : "Arne Sommer" }, { + "name" : "Athanasius", + "id" : "Athanasius", "data" : [ [ "Perl", @@ -35,11 +231,10 @@ "Raku", 2 ] - ], - "name" : "Athanasius", - "id" : "Athanasius" + ] }, { + "name" : "BarrOff", "id" : "BarrOff", "data" : [ [ @@ -50,37 +245,36 @@ "Raku", 1 ] - ], - "name" : "BarrOff" + ] }, { - "name" : "Bob Lied", "data" : [ [ "Perl", 2 ] ], - "id" : "Bob Lied" + "id" : "Bob Lied", + "name" : "Bob Lied" }, { - "id" : "Bruce Gray", "name" : "Bruce Gray", "data" : [ [ "Raku", 2 ] - ] + ], + "id" : "Bruce Gray" }, { + "name" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "name" : "Cheok-Yin Fung", "id" : "Cheok-Yin Fung" }, { @@ -94,31 +288,31 @@ 1 ] ], - "name" : "Dave Jacoby", - "id" : "Dave Jacoby" + "id" : "Dave Jacoby", + "name" : "Dave Jacoby" }, { "id" : "David Ferrone", - "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] - ] + ], + "name" : "David Ferrone" }, { - "id" : "E. Choroba", - "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] - ] + ], + "id" : "E. Choroba", + "name" : "E. Choroba" }, { - "id" : "Jaldhar H. Vyas", + "name" : "Jaldhar H. Vyas", "data" : [ [ "Perl", @@ -133,20 +327,20 @@ 1 ] ], - "name" : "Jaldhar H. Vyas" + "id" : "Jaldhar H. Vyas" }, { - "name" : "Jan Krnavek", + "id" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] ], - "id" : "Jan Krnavek" + "name" : "Jan Krnavek" }, { - "name" : "Jorg Sommrey", + "id" : "Jorg Sommrey", "data" : [ [ "Perl", @@ -157,11 +351,10 @@ 1 ] ], - "id" : "Jorg Sommrey" + "name" : "Jorg Sommrey" }, { "id" : "Laurent Rosenfeld", - "name" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -175,10 +368,10 @@ "Blog", 2 ] - ] + ], + "name" : "Laurent Rosenfeld" }, { - "id" : "Lubos Kolouch", "name" : "Lubos Kolouch", "data" : [ [ @@ -189,10 +382,12 @@ "Raku", 2 ] - ] + ], + "id" : "Lubos Kolouch" }, { "name" : "Luca Ferrari", + "id" : "Luca Ferrari", "data" : [ [ "Raku", @@ -202,32 +397,31 @@ "Blog", 8 ] - ], - "id" : "Luca Ferrari" + ] }, { - "name" : "Mark Anderson", + "id" : "Mark Anderson", "data" : [ [ "Raku", 2 ] ], - "id" : "Mark Anderson" + "name" : "Mark Anderson" }, { "name" : "Matthew Neleigh", + "id" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] - ], - "id" : "Matthew Neleigh" + ] }, { - "id" : "Mustafa Aydin", "name" : "Mustafa Aydin", + "id" : "Mustafa Aydin", "data" : [ [ "Raku", @@ -246,17 +440,34 @@ "name" : "Nelo Tovar" }, { - "id" : "Niels van Dijke", "data" : [ [ "Perl", 2 ] ], + "id" : "Niels van Dijke", "name" : "Niels van Dijke" }, { - "id" : "Peter Campbell Smith", + "data" : [ + [ + "Perl", + 2 + ], + [ + "Raku", + 2 + ], + [ + "Blog", + 1 + ] + ], + "id" : "Packy Anderson", + "name" : "Packy Anderson" + }, + { "name" : "Peter Campbell Smith", "data" : [ [ @@ -267,17 +478,18 @@ "Blog", 1 ] - ] + ], + "id" : "Peter Campbell Smith" }, { + "id" : "Peter Meszaros", "data" : [ [ "Perl", 2 ] ], - "name" : "Peter Meszaros", - "id" : "Peter Meszaros" + "name" : "Peter Meszaros" }, { "name" : "Robbie Hatley", @@ -295,16 +507,15 @@ }, { "name" : "Robert Ransbottom", + "id" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] - ], - "id" : "Robert Ransbottom" + ] }, { - "name" : "Roger Bell_West", "data" : [ [ "Perl", @@ -319,7 +530,8 @@ 1 ] ], - "id" : "Roger Bell_West" + "id" : "Roger Bell_West", + "name" : "Roger Bell_West" }, { "id" : "Scott Sotka", @@ -332,8 +544,8 @@ "name" : "Scott Sotka" }, { - "id" : "Simon Green", "name" : "Simon Green", + "id" : "Simon Green", "data" : [ [ "Perl", @@ -346,14 +558,14 @@ ] }, { + "id" : "Simon Proctor", "data" : [ [ "Raku", 2 ] ], - "name" : "Simon Proctor", - "id" : "Simon Proctor" + "name" : "Simon Proctor" }, { "data" : [ @@ -366,10 +578,12 @@ 1 ] ], - "name" : "Stephen G. Lynn", - "id" : "Stephen G. Lynn" + "id" : "Stephen G. Lynn", + "name" : "Stephen G. Lynn" }, { + "name" : "Thomas Kohler", + "id" : "Thomas Kohler", "data" : [ [ "Perl", @@ -379,11 +593,10 @@ "Blog", 2 ] - ], - "name" : "Thomas Kohler", - "id" : "Thomas Kohler" + ] }, { + "name" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -394,12 +607,11 @@ 2 ] ], - "name" : "Ulrich Rieke", "id" : "Ulrich Rieke" }, { - "id" : "W. Luis Mochan", "name" : "W. Luis Mochan", + "id" : "W. Luis Mochan", "data" : [ [ "Perl", @@ -413,211 +625,22 @@ } ] }, + "xAxis" : { + "type" : "category" + }, + "subtitle" : { + "text" : "[Champions: 34] Last updated at 2024-01-15 02:54:56 GMT" + }, "plotOptions" : { "series" : { - "borderWidth" : 0, "dataLabels" : { - "enabled" : 1, - "format" : "{point.y}" - } - } - }, - "series" : [ - { - "colorByPoint" : 1, - "data" : [ - { - "y" : 2, - "drilldown" : "Adam Russell", - "name" : "Adam Russell" - }, - { - "y" : 3, - "name" : "Arne Sommer", - "drilldown" : "Arne Sommer" - }, - { - "y" : 4, - "name" : "Athanasius", - "drilldown" : "Athanasius" - }, - { - "name" : "BarrOff", - "drilldown" : "BarrOff", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "Bob Lied", - "name" : "Bob Lied" - }, - { - "drilldown" : "Bruce Gray", - "name" : "Bruce Gray", - "y" : 2 - }, - { - "y" : 2, - "drilldown" : "Cheok-Yin Fung", - "name" : "Cheok-Yin Fung" - }, - { - "name" : "Dave Jacoby", - "drilldown" : "Dave Jacoby", - "y" : 3 - }, - { - "name" : "David Ferrone", - "drilldown" : "David Ferrone", - "y" : 2 - }, - { - "y" : 2, - "name" : "E. Choroba", - "drilldown" : "E. Choroba" - }, - { - "drilldown" : "Jaldhar H. Vyas", - "name" : "Jaldhar H. Vyas", - "y" : 5 - }, - { - "drilldown" : "Jan Krnavek", - "name" : "Jan Krnavek", - "y" : 2 - }, - { - "drilldown" : "Jorg Sommrey", - "name" : "Jorg Sommrey", - "y" : 3 - }, - { - "name" : "Laurent Rosenfeld", - "drilldown" : |
