diff options
24 files changed, 2315 insertions, 1925 deletions
diff --git a/challenge-266/packy-anderson/README.md b/challenge-266/packy-anderson/README.md index 784366b854..3f4d321553 100644 --- a/challenge-266/packy-anderson/README.md +++ b/challenge-266/packy-anderson/README.md @@ -16,4 +16,4 @@ ## Blog Post -[Completing a Third of an Appearance](https://packy.dardan.com/b/KV) +[A Matrix of Uncommon X Words](https://packy.dardan.com/b/KX) diff --git a/challenge-266/packy-anderson/blog.txt b/challenge-266/packy-anderson/blog.txt new file mode 100644 index 0000000000..a43da805a2 --- /dev/null +++ b/challenge-266/packy-anderson/blog.txt @@ -0,0 +1 @@ +https://packy.dardan.com/2024/04/23/perl-weekly-challenge-a-matrix-of-uncommon-x-words diff --git a/challenge-266/packy-anderson/perl/ch-1.pl b/challenge-266/packy-anderson/perl/ch-1.pl new file mode 100644 index 0000000000..78ebc28a2d --- /dev/null +++ b/challenge-266/packy-anderson/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +use v5.38; + +sub occursOnce($line) { + # create a hash counting the words + my %all; + $all{$_}++ for split(/\s+/, $line); + # create a list of words that occur once in the hash + return grep { $all{$_} == 1 } keys %all; +} + +sub uncommonWords($line1, $line2) { + return occursOnce( + join(' ', occursOnce($line1), occursOnce($line2)) + ); +} + +sub solution($line1, $line2) { + say "Input: \$line1 = '$line1'"; + say " \$line2 = '$line2'"; + my @results = uncommonWords($line1, $line2); + say "Output: ('" . join("', '",@results) . "')"; +} + +say "Example 1:"; +solution('Mango is sweet', 'Mango is sour'); + +say "\nExample 2:"; +solution('Mango Mango', 'Orange'); + +say "\nExample 3:"; +solution('Mango is Mango', 'Orange is Orange'); diff --git a/challenge-266/packy-anderson/perl/ch-2.pl b/challenge-266/packy-anderson/perl/ch-2.pl new file mode 100644 index 0000000000..3cd14e8b26 --- /dev/null +++ b/challenge-266/packy-anderson/perl/ch-2.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl +use v5.38; + +sub isDiagonal($x, $y, $N) { + return ( + $N == 1 || $N == 2 || $x == $y || $x + $y == $N - 1 + ); +} + +sub isXMatrix(@matrix) { + my $N = scalar @matrix; + foreach my $y ( 0 .. $#matrix ) { + my @row = @{$matrix[$y]}; + foreach my $x ( 0 .. $#row ) { + my $value = $row[$x]; + # fail if diagonal values are zero or + # non-diagonal values are non-zero + return 0 + unless isDiagonal($x, $y, $N) == ($value != 0); + } + } + return 1; +} + +sub formatMatrix($matrix, $indent=17) { + my @output; + foreach my $row (@$matrix) { + my $output_row = q{ } x $indent . " ["; + $output_row .= join(', ', map { sprintf "%1d", $_ } @$row) . ']'; + push @output, $output_row; + } + return "[\n" + . join(",\n", @output) + . "\n" + . q{ } x $indent . "]"; +} + +sub solution(@matrix) { + say 'Input: $matrix = ' . formatMatrix(\@matrix); + say 'Output: ' . (isXMatrix(@matrix) ? 'True' : 'False'); +} + +say "Example 1:"; +solution( + [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], +); + +say "\nExample 2:"; +solution( + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +); + +say "\nExample 3:"; +solution( + [1, 0, 2], + [0, 3, 0], + [4, 0, 5], +); + +say "\nExample 4:"; +solution( + [1, 0, 0, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +); + +say "\nExample 5:"; +solution( + [1, 0, 1, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +); + +say "\nExample 6:"; +solution( + [1, 1], + [1, 1], +); + +say "\nExample 7:"; +solution( + [1, 0], + [1, 1], +); diff --git a/challenge-266/packy-anderson/python/ch-1.py b/challenge-266/packy-anderson/python/ch-1.py new file mode 100644 index 0000000000..ea2c608816 --- /dev/null +++ b/challenge-266/packy-anderson/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +from collections import Counter + +def occursOnce(line): + # create a Counter of all words + all = Counter(line.split()) + # create a list of words that occur once in the Counter + return [ word for word in list(all) if all[word] == 1 ] + +def uncommonWords(line1, line2): + return occursOnce( + ' '.join(occursOnce(line1) + occursOnce(line2)) + ) + +def quoted_comma_join(arr): + return "'" + "', '".join(arr) + "'" + +def solution(line1, line2): + print(f"Input: $line1 = '{line1}'") + print(f" $line2 = '{line2}'") + uncommon = uncommonWords(line1, line2) + print(f'Output: ({quoted_comma_join(uncommon)})') + +print('Example 1:') +solution('Mango is sweet', 'Mango is sour') + +print('\nExample 2:') +solution('Mango Mango', 'Orange') + +print('\nExample 3:') +solution('Mango is Mango', 'Orange is Orange') diff --git a/challenge-266/packy-anderson/python/ch-2.py b/challenge-266/packy-anderson/python/ch-2.py new file mode 100644 index 0000000000..dc2c0393bc --- /dev/null +++ b/challenge-266/packy-anderson/python/ch-2.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python + +def isDiagonal(x, y, N): + return ( + N == 1 or N == 2 or x == y or x + y == N - 1 + ) + +def isXMatrix(matrix): + N = len(matrix) + for y, row in enumerate(matrix): + for x, value in enumerate(row): + # fail if diagonal values are zero or + # non-diagonal values are non-zero + if isDiagonal(x, y, N) != (value != 0): + return False + return True + +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: {isXMatrix(matrix)}') + +print('Example 1:') +solution([ + [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], +]) + +print('\nExample 2:') +solution([ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +]) + +print('\nExample 3:') +solution([ + [1, 0, 2], + [0, 3, 0], + [4, 0, 5], +]) + +print('\nExample 4:') +solution([ + [1, 0, 0, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +]) + +print('\nExample 5:') +solution([ + [1, 0, 1, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +]) + +print('\nExample 6:') +solution([ + [1, 1], + [1, 1] +]) + +print('\nExample 7:') +solution([ + [1, 0], + [1, 1] +]) diff --git a/challenge-266/packy-anderson/raku/ch-1.raku b/challenge-266/packy-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..453a91d2e6 --- /dev/null +++ b/challenge-266/packy-anderson/raku/ch-1.raku @@ -0,0 +1,32 @@ +#!/usr/bin/env raku +use v6; + +sub occursOnce($line) { + # create a Bag of all words + my $all = $line.comb(/\w+/).Bag; + # create a list of words that occur once in the Bag + return $all.keys.grep({ $all{$_} == 1 }); +} + +sub uncommonWords($line1, $line2) { + # create a Bag of words that occur once in each line + my $all = occursOnce($line1).Bag ⊎ occursOnce($line2).Bag; + # return a list of words that occur once in that Bag + return $all.keys.grep({ $all{$_} == 1 }); +} + +sub solution($line1, $line2) { + say "Input: \$line1 = '$line1'"; + say " \$line2 = '$line2'"; + my @results = uncommonWords($line1, $line2); + say "Output: ('" ~ @results.join("', '") ~ "')"; +} + +say "Example 1:"; +solution('Mango is sweet', 'Mango is sour'); + +say "\nExample 2:"; +solution('Mango Mango', 'Orange'); + +say "\nExample 3:"; +solution('Mango is Mango', 'Orange is Orange'); diff --git a/challenge-266/packy-anderson/raku/ch-2.raku b/challenge-266/packy-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..5fa2b6b9fd --- /dev/null +++ b/challenge-266/packy-anderson/raku/ch-2.raku @@ -0,0 +1,92 @@ +#!/usr/bin/env raku +use v6; + +sub isDiagonal($x, $y, $N) { + return ( + $N == 1 || $N == 2 || $x == $y || $x + $y == $N - 1 + ); +} + +sub isXMatrix(@matrix) { + my $N = @matrix.elems; + for @matrix.kv -> $y, @row { + for @row.kv -> $x, $value { + # fail if diagonal values are zero or + # non-diagonal values are non-zero + return False + unless isDiagonal($x, $y, $N) == ($value != 0); + } + } + return True; +} + +sub formatMatrix(@matrix, $indent=17) { + my @output; + for @matrix -> @row { + my $output_row = q{ } x $indent ~ " ["; + $output_row ~= @row.map({ sprintf "%1d", $_ }) + .join(', ') ~ "]"; + @output.push($output_row); + } + return "[\n" + ~ @output.join(",\n") + ~ "\n" + ~ q{ } x $indent ~ "]"; +} + +sub solution(@matrix) { + say 'Input: $matrix = ' ~ formatMatrix(@matrix); + say 'Output: ' ~ isXMatrix(@matrix); +} + +say "Example 1:"; +solution([ + [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], +]); + +say "\nExample 2:"; +solution([ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +]); + +say "\nExample 3:"; +solution([ + [1, 0, 2], + [0, 3, 0], + [4, 0, 5], +]); + +say "\nExample 4:"; +solution([ + [1, 0, 0, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +]); + +say "\nExample 5:"; +solution([ + [1, 0, 1, 0, 1], + [0, 1, 0, 1, 0], + [0, 0, 1, 0, 0], + [0, 1, 0, 1, 0], + [1, 0, 0, 0, 1], +]); + +say "\nExample 6:"; +solution([ + [1, 1], + [1, 1], +]); + +say "\nExample 7:"; +solution([ + [1, 0], + [1, 1], +]); diff --git a/stats/pwc-current.json b/stats/pwc-current.json index c653c257f1..52afd2bb56 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -1,4 +1,226 @@ { + "subtitle" : { + "text" : "[Champions: 36] Last updated at 2024-04-29 02:19:39 GMT" + }, + "yAxis" : { + "title" : { + "text" : "Total Solutions" + } + }, + "legend" : { + "enabled" : 0 + }, + "title" : { + "text" : "The Weekly Challenge - 266" + }, + "xAxis" : { + "type" : "category" + }, + "plotOptions" : { + "series" : { + "dataLabels" : { + "enabled" : 1, + "format" : "{point.y}" + }, + "borderWidth" : 0 + } + }, + "tooltip" : { + "followPointer" : 1, + "pointFormat" : "<span style='color:{point.color}'>{point.name}</span>: <b>{point.y:f}</b><br/>", + "headerFormat" : "<span style='font-size:11px'>{series.name}</span><br/>" + }, + "chart" : { + "type" : "column" + }, + "series" : [ + { + "data" : [ + { + "name" : "Ali Moradi", + "drilldown" : "Ali Moradi", + "y" : 5 + }, + { + "name" : "Arne Sommer", + "y" : 3, + "drilldown" : "Arne Sommer" + }, + { + "name" : "Athanasius", + "drilldown" : "Athanasius", + "y" : 4 + }, + { + "name" : "BarrOff", + "drilldown" : "BarrOff", + "y" : 2 + }, + { + "drilldown" : "Bob Lied", + "y" : 2, + "name" : "Bob Lied" + }, + { + "name" : "Bruce Gray", + "drilldown" : "Bruce Gray", + "y" : 2 + }, + { + "name" : "Cheok-Yin Fung", + "drilldown" : "Cheok-Yin Fung", + "y" : 2 + }, + { + "drilldown" : "Dave Jacoby", + "y" : 2, + "name" : "Dave Jacoby" + }, + { + "name" : "David Ferrone", + "drilldown" : "David Ferrone", + "y" : 2 + }, + { + "drilldown" : "E. Choroba", + "y" : 2, + "name" : "E. Choroba" + }, + { + "y" : 2, + "drilldown" : "Feng Chang", + "name" : "Feng Chang" + }, + { + "name" : "Jaldhar H. Vyas", + "drilldown" : "Jaldhar H. Vyas", + "y" : 5 + }, + { + "drilldown" : "James Smith", + "y" : 2, + "name" : "James Smith" + }, + { + "name" : "Jan Krnavek", + "y" : 2, + "drilldown" : "Jan Krnavek" + }, + { + "y" : 3, + "drilldown" : "Jorg Sommrey", + "name" : "Jorg Sommrey" + }, + { + "drilldown" : "Lance Wicks", + "y" : 1, + "name" : "Lance Wicks" + }, + { + "name" : "Laurent Rosenfeld", + "y" : 6, + "drilldown" : "Laurent Rosenfeld" + }, + { + "name" : "Lubos Kolouch", + "drilldown" : "Lubos Kolouch", + "y" : 2 + }, + { + "y" : 11, + "drilldown" : "Luca Ferrari", + "name" : "Luca Ferrari" + }, + { + "y" : 2, + "drilldown" : "Mark Anderson", + "name" : "Mark Anderson" + }, + { + "name" : "Matthew Neleigh", + "y" : 2, + "drilldown" : "Matthew Neleigh" + }, + { + "drilldown" : "Matthias Muth", + "y" : 2, + "name" : "Matthias Muth" + }, + { + "name" : "Nelo Tovar", + "drilldown" : "Nelo Tovar", + "y" : 2 + }, + { + "y" : 2, + "drilldown" : "Niels van Dijke", + "name" : "Niels van Dijke" + }, + { + "name" : "Packy Anderson", + "drilldown" : "Packy Anderson", + "y" : 5 + }, + { + "name" : "Peter Campbell Smith", + "drilldown" : "Peter Campbell Smith", + "y" : 3 + }, + { + "y" : 2, + "drilldown" : "Peter Meszaros", + "name" : "Peter Meszaros" + }, + { + "name" : "Reinier Maliepaard", + "drilldown" : "Reinier Maliepaard", + "y" : 3 + }, + { + "drilldown" : "Robbie Hatley", + "y" : 3, + "name" : "Robbie Hatley" + }, + { + "name" : "Robert Ransbottom", + "drilldown" : "Robert Ransbottom", + "y" : 2 + }, + { + "name" : "Roger Bell_West", + "drilldown" : "Roger Bell_West", + "y" : 5 + }, + { + "name" : "Simon Green", + "drilldown" : "Simon Green", + "y" : 3 + }, + { + "y" : 4, + "drilldown" : "Thomas Kohler", + "name" : "Thomas Kohler" + }, + { + "name" : "Ulrich Rieke", + "drilldown" : "Ulrich Rieke", + "y" : 3 + }, + { + "name" : "W. Luis Mochan", + "drilldown" : "W. Luis Mochan", + "y" : 3 + }, + { + "name" : "Wanderdoc", + "drilldown" : "Wanderdoc", + "y" : 2 + } + ], + "name" : "The Weekly Challenge - 266", + "colorByPoint" : 1 + } + ], "drilldown" : { "series" : [ { @@ -20,6 +242,7 @@ "name" : "Ali Moradi" }, { + "id" : "Arne Sommer", "data" : [ [ "Raku", @@ -30,11 +253,10 @@ 1 ] ], - "id" : "Arne Sommer", "name" : "Arne Sommer" }, { - "id" : "Athanasius", + "name" : "Athanasius", "data" : [ [ "Perl", @@ -45,90 +267,89 @@ 2 ] ], - "name" : "Athanasius" + "id" : "Athanasius" }, { "id" : "BarrOff", + "name" : "BarrOff", "data" : [ [ "Raku", 2 ] - ], - "name" : "BarrOff" + ] }, { + "name" : "Bob Lied", "data" : [ [ "Perl", 2 ] ], - "id" : "Bob Lied", - "name" : "Bob Lied" + "id" : "Bob Lied" }, { + "id" : "Bruce Gray", "name" : "Bruce Gray", "data" : [ [ "Raku", 2 ] - ], - "id" : "Bruce Gray" + ] }, { - "id" : "Cheok-Yin Fung", "data" : [ [ "Perl", 2 ] ], - "name" : "Cheok-Yin Fung" + "name" : "Cheok-Yin Fung", + "id" : "Cheok-Yin Fung" }, { - "name" : "Dave Jacoby", + "id" : "Dave Jacoby", "data" : [ [ "Perl", 2 ] ], - "id" : "Dave Jacoby" + "name" : "Dave Jacoby" }, { + "name" : "David Ferrone", "data" : [ [ "Perl", 2 ] ], - "id" : "David Ferrone", - "name" : "David Ferrone" + "id" : "David Ferrone" }, { - "id" : "E. Choroba", + "name" : "E. Choroba", "data" : [ [ "Perl", 2 ] ], - "name" : "E. Choroba" + "id" : "E. Choroba" }, { - "id" : "Feng Chang", "data" : [ [ "Raku", 2 ] ], - "name" : "Feng Chang" + "name" : "Feng Chang", + "id" : "Feng Chang" }, { - "id" : "Jaldhar H. Vyas", "data" : [ [ "Perl", @@ -143,27 +364,28 @@ 1 ] ], - "name" : "Jaldhar H. Vyas" + "name" : "Jaldhar H. Vyas", + "id" : "Jaldhar H. Vyas" }, { + "name" : "James Smith", "data" : [ [ "Perl", 2 ] ], - "name" : "James Smith", "id" : "James Smith" }, { + "id" : "Jan Krnavek", "name" : "Jan Krnavek", "data" : [ [ "Raku", 2 ] - ], - "id" : "Jan Krnavek" + ] }, { "id" : "Jorg Sommrey", @@ -180,16 +402,17 @@ "name" : "Jorg Sommrey" }, { + "name" : "Lance Wicks", "data" : [ [ "Perl", 1 ] ], - "id" : "Lance Wicks", - "name" : "Lance Wicks" + "id" : "Lance Wicks" }, { + "id" : "Laurent Rosenfeld", "data" : [ [ "Perl", @@ -204,18 +427,17 @@ 2 ] ], - "name" : "Laurent Rosenfeld", - "id" : "Laurent Rosenfeld" + "name" : "Laurent Rosenfeld" }, { + "id" : "Lubos Kolouch", "data" : [ [ "Perl", 2 ] ], - "name" : "Lubos Kolouch", - "id" : "Lubos Kolouch" + "name" : "Lubos Kolouch" }, { "name" : "Luca Ferrari", @@ -232,43 +454,43 @@ "id" : "Luca Ferrari" }, { + "name" : "Mark Anderson", "data" : [ [ "Raku", 2 ] ], - "name" : "Mark Anderson", "id" : "Mark Anderson" }, { + "name" : "Matthew Neleigh", "data" : [ [ "Perl", 2 ] ], - "name" : "Matthew Neleigh", "id" : "Matthew Neleigh" }, { + "name" : "Matthias Muth", "data" : [ [ "Perl", 2 ] ], - "id" : "Matthias Muth", - "name" : "Matthias Muth" + "id" : "Matthias Muth" }, { + "name" : "Nelo Tovar", "data" : [ [ "Perl", 2 ] ], - "name" : "Nelo Tovar", "id" : "Nelo Tovar" }, { @@ -278,35 +500,52 @@ 2 ] ], - "id" : "Niels van Dijke", - "name" : "Niels van Dijke" + "name" : "Niels van Dijke", + "id" : "Niels van Dijke" }, { + "id" : "Packy Anderson", + "name" : "Packy Anderson", "data" : [ [ "Perl", 2 ], [ + "Raku", + 2 + ], + [ "Blog", 1 ] - ], - "name" : "Peter Campbell Smith", - "id" : "Peter Campbell Smith" + ] }, { + "id" : "Peter Campbell Smith", "data" : [ [ "Perl", 2 + ], + [ + "Blog", + 1 ] ], + "name" : "Peter Campbell Smith" + }, + { "name" : "Peter Meszaros", + "data" : [ + [ + "Perl", + 2 + ] + ], "id" : "Peter Meszaros" }, { - "id" : "Reinier Maliepaard", "data" : [ [ "Perl", @@ -317,7 +556,8 @@ 1 ] ], - "name" : "Reinier Maliepaard" + "name" : "Reinier Maliepaard", + "id" : "Reinier Maliepaard" }, { "data" : [ @@ -330,18 +570,18 @@ 1 ] ], - "id" : "Robbie Hatley", - "name" : "Robbie Hatley" + "name" : "Robbie Hatley", + "id" : "Robbie Hatley" }, { + "id" : "Robert Ransbottom", "name" : "Robert Ransbottom", "data" : [ [ "Raku", 2 ] - ], - "id" : "Robert Ransbottom" + ] }, { "data" : [ @@ -362,6 +602,8 @@ "id" : "Roger Bell_West" }, { + "id" : "Simon Green", + "name" : "Simon Green", "data" : [ [ "Perl", @@ -371,11 +613,10 @@ "Blog", 1 ] - ], - "id" : "Simon Green", - "name" : "Simon Green" + ] }, { + "id" : "Thomas Kohler", "data" : [ [ "Perl", @@ -386,10 +627,10 @@ 2 ] ], - "name" : "Thomas Kohler", - "id" : "Thomas Kohler" + "name" : "Thomas Kohler" }, { + "id" : "Ulrich Rieke", "data" : [ [ "Perl", @@ -400,7 +641,6 @@ 1 ] ], - "id" : "Ulrich Rieke", "name" : "Ulrich Rieke" }, { @@ -418,232 +658,15 @@ "id" : "W. Luis Mochan" }, { + "id" : "Wanderdoc", "name" : "Wanderdoc", "data" : [ [ "Perl", 2 ] - ], - "id" : "Wanderdoc" + ] } ] - }, - "xAxis" : { - "type" : "category" - }, - "series" : [ - { - "colorByPoint" : 1, - "name" : "The Weekly Challenge - 266", - "data" : [ - { - "y" : 5, - "drilldown" : "Ali Moradi", - "name" : "Ali Morad |
