diff options
| author | Matthew Neleigh <87619159+mattneleigh@users.noreply.github.com> | 2024-06-05 13:04:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-05 13:04:21 -0400 |
| commit | b7ddc0df59586129822cd79732dee50ddabbf18c (patch) | |
| tree | e78636a66f5b729b049459837b524e701dc22082 | |
| parent | 74e6d81b2e785bd2f7dd4398ea5e2733618ac4b3 (diff) | |
| parent | 7aeb2014a04d815bc006a3c337b73426a10ea591 (diff) | |
| download | perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.tar.gz perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.tar.bz2 perlweeklychallenge-club-b7ddc0df59586129822cd79732dee50ddabbf18c.zip | |
Merge branch 'manwar:master' into pwc272
92 files changed, 5139 insertions, 3557 deletions
diff --git a/challenge-270/e-choroba/perl/ch-2.pl b/challenge-270/e-choroba/perl/ch-2.pl index 0632f5713e..9396cc80ff 100755 --- a/challenge-270/e-choroba/perl/ch-2.pl +++ b/challenge-270/e-choroba/perl/ch-2.pl @@ -3,7 +3,7 @@ use warnings; use strict; use experimental qw( signatures ); -sub distribute_elements($x, $y, $ints) { +sub equalize_array($x, $y, $ints) { my @agenda = ([0, [sort { $a <=> $b } @$ints]]); my %seen; while (1) { @@ -14,9 +14,7 @@ sub distribute_elements($x, $y, $ints) { my @e = sort { $a <=> $b } $elements->[0] + 1, @$elements[1 .. $#$elements]; - $seen{"@e"} = $price + $x, - push @agenda, [$price + $x, \@e] if ! exists $seen{"@e"} - || $seen{"@e"} > $price + $x; + push @agenda, [$price + $x, \@e] unless $seen{"@e"}++; if (@$elements > 2 && 2 * $x > $y) { for my $i (1, $#$elements) { @@ -26,10 +24,7 @@ sub distribute_elements($x, $y, $ints) { && $_ != $i, 0 .. $#$elements]; - $seen{"@e"} = $price + $y, - push @agenda, [$price + $y, \@e] - if ! exists $seen{"@e"} - || $seen{"@e"} > $price + $y; + push @agenda, [$price + $y, \@e] unless $seen{"@e"}++; } } @@ -39,27 +34,27 @@ sub distribute_elements($x, $y, $ints) { use Test::More tests => 2 + 17; -is distribute_elements(3, 2, [4, 1]), 9, 'Example 1'; -is distribute_elements(2, 1, [2, 3, 3, 3, 5]), 6, 'Example 2'; - -is distribute_elements(3, 1, [1, 2, 2]), 2, '3 1 [1 2 2]'; -is distribute_elements(4, 1, [1, 2, 2, 2, 2]), 3, '4 1 [1 2 2 2 2]'; -is distribute_elements(20, 1, [1, 2, 3, 3]), 21, '20 1 [1 2 3 3]'; -is distribute_elements(20, 1, [1, 2, 4, 4]), 22, '20 1 [1 2 4 4]'; -is distribute_elements(1, 7, [2, 2, 3, 5]), 8, '1 7 [2 2 3 5]'; -is distribute_elements(7, 1, [2, 2, 3, 5]), 4, '7 1 [2 2 3 5]'; -is distribute_elements(2, 3, [1, 1, 5]), 12, '2 3 [1 1 5]'; -is distribute_elements(17, 1, [1, 9, 9]), 16, '17 1 [1 9 9]'; -is distribute_elements(8, 9, [6, 6, 4, 2]), 34, '8 9 [6 6 4 2]'; -is distribute_elements(6, 1, [2, 5, 5, 6]), 5, '6 1 [2 5 5 6]'; -is distribute_elements(9, 1, [1, 2, 3, 3, 7]), 12, '9 1 [1 2 3 3 7]'; -is distribute_elements(7, 3, [1, 4, 7, 7, 7]), 21, '7 3 [1 4 7 7 7]'; -is distribute_elements(9, 1, [7, 1, 1, 4, 8]), 12, '9 1 [7 1 1 4 8]'; -is distribute_elements(1, 1, [1, 4, 3, 4, 5, 4, 2, 3, 7]), 15, +is equalize_array(3, 2, [4, 1]), 9, 'Example 1'; +is equalize_array(2, 1, [2, 3, 3, 3, 5]), 6, 'Example 2'; + +is equalize_array(3, 1, [1, 2, 2]), 2, '3 1 [1 2 2]'; +is equalize_array(4, 1, [1, 2, 2, 2, 2]), 3, '4 1 [1 2 2 2 2]'; +is equalize_array(20, 1, [1, 2, 3, 3]), 21, '20 1 [1 2 3 3]'; +is equalize_array(20, 1, [1, 2, 4, 4]), 22, '20 1 [1 2 4 4]'; +is equalize_array(1, 7, [2, 2, 3, 5]), 8, '1 7 [2 2 3 5]'; +is equalize_array(7, 1, [2, 2, 3, 5]), 4, '7 1 [2 2 3 5]'; +is equalize_array(2, 3, [1, 1, 5]), 12, '2 3 [1 1 5]'; +is equalize_array(17, 1, [1, 9, 9]), 16, '17 1 [1 9 9]'; +is equalize_array(8, 9, [6, 6, 4, 2]), 34, '8 9 [6 6 4 2]'; +is equalize_array(6, 1, [2, 5, 5, 6]), 5, '6 1 [2 5 5 6]'; +is equalize_array(9, 1, [1, 2, 3, 3, 7]), 12, '9 1 [1 2 3 3 7]'; +is equalize_array(7, 3, [1, 4, 7, 7, 7]), 21, '7 3 [1 4 7 7 7]'; +is equalize_array(9, 1, [7, 1, 1, 4, 8]), 12, '9 1 [7 1 1 4 8]'; +is equalize_array(1, 1, [1, 4, 3, 4, 5, 4, 2, 3, 7]), 15, '1 1 [1 4 3 4 5 4 2 3 7]'; -is distribute_elements(4, 1, [1, 4, 1, 2, 5, 7, 1, 5]), 15, +is equalize_array(4, 1, [1, 4, 1, 2, 5, 7, 1, 5]), 15, '4 1 [1 4 1 2 5 7 1 5]'; -is distribute_elements(6, 10, [2, 3, 5, 1, 2, 1, 1, 7]), 170, +is equalize_array(6, 10, [2, 3, 5, 1, 2, 1, 1, 7]), 170, '6 10 [2 3 5 1 2 1 1 7]'; -is distribute_elements(4, 4, [4, 4, 1, 7, 2, 7, 1, 1, 4]), 64, +is equalize_array(4, 4, [4, 4, 1, 7, 2, 7, 1, 1, 4]), 64, '4 4 [4 4 1 7 2 7 1 1 4]'; diff --git a/challenge-271/e-choroba/perl/ch-1.pl b/challenge-271/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4e96f45bbc --- /dev/null +++ b/challenge-271/e-choroba/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use PDL; + +sub maximum_ones($matrix) { + my $p = pdl($matrix); + my $sum = $p->sumover; + my $max = $sum->max; + return ($sum == $max)->which->unpdl->[0] + 1 +} + +use Test2::V0 qw{ plan is }; # Conflict with PDL::float. +plan(3 + 1); + +is maximum_ones([[0, 1], [1, 0]]), 1, 'Example 1'; +is maximum_ones([[0, 0, 0], [1, 0, 1]]), 2, 'Example 2'; +is maximum_ones([[0, 0], [1, 1], [0, 0]]), 2, 'Example 3'; + + +is maximum_ones([[0, 0, 0, 0], + [1, 1, 0, 1], + [0, 0, 1, 0], + [1, 0, 0, 0], + [0, 1, 1, 1]]), + 2, 'Example 4'; diff --git a/challenge-271/e-choroba/perl/ch-2.pl b/challenge-271/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..561be4b901 --- /dev/null +++ b/challenge-271/e-choroba/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub sort_by_1_bits(@ints) { + map $_->[1], + sort { $a->[0] <=> $b->[0] + || + $a->[1] <=> $b->[1] + } map [unpack('%32b*', pack 'N', $_), $_], + @ints +} + +use Test2::V0; +plan(2); + +is [sort_by_1_bits(0, 1, 2, 3, 4, 5, 6, 7, 8)], [0, 1, 2, 4, 8, 3, 5, 6, 7], + 'Example 1'; +is [sort_by_1_bits(1024, 512, 256, 128, 64)], [64, 128, 256, 512, 1024], + 'Example 2'; diff --git a/challenge-272/chazzka/README b/challenge-272/chazzka/README new file mode 100644 index 0000000000..fd45943659 --- /dev/null +++ b/challenge-272/chazzka/README @@ -0,0 +1 @@ +Solution by chazzka diff --git a/challenge-272/chazzka/coconut/ch-2.coco b/challenge-272/chazzka/coconut/ch-2.coco new file mode 100644 index 0000000000..f66b849fdb --- /dev/null +++ b/challenge-272/chazzka/coconut/ch-2.coco @@ -0,0 +1,6 @@ +def string_score(s, accum = 0): + match s: + case [_]: + return accum + case [head, sec] + tail: + return string_score([sec] + tail, accum + abs(ord(sec) - ord(head)))
\ No newline at end of file diff --git a/challenge-272/chazzka/coconut/test/matching_test.coco b/challenge-272/chazzka/coconut/test/matching_test.coco new file mode 100644 index 0000000000..de3748ff84 --- /dev/null +++ b/challenge-272/chazzka/coconut/test/matching_test.coco @@ -0,0 +1,14 @@ +import unittest +import importlib +solution = importlib.import_module("ch-2") + + +class TestStringMethods(unittest.TestCase): + def test_matcher(self): + solution.string_score("hello") |> self.assertEqual$(13) + solution.string_score("perl") |> self.assertEqual$(30) + solution.string_score("raku") |> self.assertEqual$(37) + solution.string_score("coconut") |> self.assertEqual$(45) + +if __name__ == '__main__': + unittest.main()
\ No newline at end of file diff --git a/challenge-272/dave-jacoby/perl/ch-1.pl b/challenge-272/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..abaa466aae --- /dev/null +++ b/challenge-272/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ bitwise fc postderef say signatures state }; + +use List::Util qw{max}; + +my @examples = ( + + "1.1.1.1", "255.101.1.0" +); +for my $example (@examples) { + my $output = defang_ipv4($example); + say <<"END"; + Input: \$ip = "$example" + Output: "$output" +END +} + +sub defang_ipv4 ( $address ) { + $address =~ s/\./[.]/gmx; + return $address; +} diff --git a/challenge-272/dave-jacoby/perl/ch-2.pl b/challenge-272/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..79650b52d9 --- /dev/null +++ b/challenge-272/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = (qw{ hello perl raku weekly linux Perl PERL }); + +for my $example (@examples) { + my $output = string_score($example); + + say <<"END"; + Input: \$str = "$example" + Output: $output +END +} + +sub string_score ($str) { + my @str = split //, $str; + return sum0 + map { abs ord( $str[$_] ) - ord( $str[ 1 + $_ ] ) } + 0 .. -2 + scalar @str; +} diff --git a/challenge-272/deadmarshal/blog.txt b/challenge-272/deadmarshal/blog.txt new file mode 100644 index 0000000000..dbb200ed12 --- /dev/null +++ b/challenge-272/deadmarshal/blog.txt @@ -0,0 +1 @@ +https://deadmarshal.blogspot.com/2024/06/twc272.html diff --git a/challenge-272/deadmarshal/java/Ch1.java b/challenge-272/deadmarshal/java/Ch1.java new file mode 100644 index 0000000000..6f589b0407 --- /dev/null +++ b/challenge-272/deadmarshal/java/Ch1.java @@ -0,0 +1,7 @@ +public class Ch1 { + public static void main(String[] args) { + System.out.println("1.1.1.1".replaceAll("\\.", "[.]")); + System.out.println("255.101.1.0".replaceAll("\\.", "[.]")); + } +} + |
