diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2025-09-04 22:01:29 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2025-09-04 22:01:29 -0400 |
| commit | 07478ae44a3ce3173c60e45044efa9742c5fb7c8 (patch) | |
| tree | 8e623c440be8730fa33d062df84fb964c6731f1a | |
| parent | 1d93bdf03f934443562da1fb88731936fa396f09 (diff) | |
| download | perlweeklychallenge-club-07478ae44a3ce3173c60e45044efa9742c5fb7c8.tar.gz perlweeklychallenge-club-07478ae44a3ce3173c60e45044efa9742c5fb7c8.tar.bz2 perlweeklychallenge-club-07478ae44a3ce3173c60e45044efa9742c5fb7c8.zip | |
DAJ 337 blogged
| -rw-r--r-- | challenge-337/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-337/dave-jacoby/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-337/dave-jacoby/perl/ch-2.pl | 60 |
3 files changed, 100 insertions, 0 deletions
diff --git a/challenge-337/dave-jacoby/blog.txt b/challenge-337/dave-jacoby/blog.txt new file mode 100644 index 0000000000..a9b8fd0375 --- /dev/null +++ b/challenge-337/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/09/05/i-never-go-far-without-a-little-big-star-weekly-challenge-337.html diff --git a/challenge-337/dave-jacoby/perl/ch-1.pl b/challenge-337/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..5c79d38024 --- /dev/null +++ b/challenge-337/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + [ 6, 5, 4, 8 ], + [ 7, 7, 7, 7 ], + [ 5, 4, 3, 2, 1 ], + [ -1, 0, 3, -2, 1 ], + [ 0, 1, 1, 2, 0 ], + +); + +for my $in (@examples) { + my @input = $in->@*; + my $input = join ', ', @input; + my @output = smaller_than_current(@input); + my $output = join ', ', @output; + say <<"END"; + Input: \@num1 = ($input) + Output: $output +END + +} + +sub smaller_than_current (@num1) { + my @num2; + for my $i ( 0 .. $#num1 ) { + my $n = $num1[$i]; + my @copy = @num1; + delete $copy[$i]; + @copy = grep { defined } @copy; + push @num2, scalar grep { $n >= $_ } @copy; + } + return @num2; +} diff --git a/challenge-337/dave-jacoby/perl/ch-2.pl b/challenge-337/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..b1311c63f2 --- /dev/null +++ b/challenge-337/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + { row => 2, col => 3, locations => [ [ 0, 1 ], [ 1, 1 ] ] }, + { row => 2, col => 2, locations => [ [ 1, 1 ], [ 0, 0 ] ] }, + { row => 3, col => 3, locations => [ [ 0, 0 ], [ 1, 2 ], [ 2, 1 ] ] }, + { row => 1, col => 5, locations => [ [ 0, 2 ], [ 0, 4 ] ] }, + { + row => 4, + col => 2, + locations => [ [ 1, 0 ], [ 3, 1 ], [ 2, 0 ], [ 0, 1 ] ] + }, +); + +for my $input (@examples) { + my $row = $input->{row}; + my $col = $input->{col}; + my $locs = join ',', map { qq{[$_]} } + map { join ',', $_->@* } $input->{locations}->@*; + my $output = odd_matrix($input); + say <<"END"; + Input: \$row = $row, + \$col = $col, + \@locations = ($locs) + Output: $output +END +} + +sub odd_matrix($input) { + my $row = $input->{row}; + my $col = $input->{col}; + my @matrix; + for my $r ( 0 .. -1 + $row ) { + for my $c ( 0 .. -1 + $col ) { + $matrix[$r][$c] = 0; + } + } + my @locations = $input->{locations}->@*; + for my $loc (@locations) { + my ( $x, $y ) = $loc->@*; + for my $c ( 0 .. -1 + $col ) { # rows + $matrix[$x][$c]++; + } + + for my $r ( 0 .. -1 + $row ) { # columns + $matrix[$r][$y]++; + } + } + # display_matrix( \@matrix ); + return scalar grep { ( $_ % 2 ) } map { $_->@* } @matrix; +} + +sub display_matrix ($matrix) { + say join "\n", '', map { join ' ', "\t", '[', $_->@*, ']' } $matrix->@*; +} |
