aboutsummaryrefslogtreecommitdiff
path: root/challenge-249
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
committerLuis Mochan <mochan@fis.unam.mx>2023-12-27 14:57:27 -0600
commitf9c88dbbb1e13bfb63f40d0dd3b8630850655d7b (patch)
treecb922fb2f44a7ec7bd10cd13b3db9eb2d4fd14b5 /challenge-249
parent23f378718ed93b9b8d9e51fe18f07a55f61dc9c1 (diff)
parent4ece120b2f4c7b25b00eaad66524fb5d99fa58c3 (diff)
downloadperlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.gz
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.bz2
perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.zip
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-249')
-rw-r--r--challenge-249/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-249/arne-sommer/raku/ch-1.raku28
-rwxr-xr-xchallenge-249/arne-sommer/raku/ch-2.raku25
-rwxr-xr-xchallenge-249/arne-sommer/raku/di-string-match25
-rwxr-xr-xchallenge-249/arne-sommer/raku/equal-pairs28
-rw-r--r--challenge-249/dave-jacoby/blog.txt1
-rw-r--r--challenge-249/dave-jacoby/perl/ch-1.pl38
-rw-r--r--challenge-249/dave-jacoby/perl/ch-2.pl43
-rwxr-xr-xchallenge-249/e-choroba/perl/ch-1.pl75
-rwxr-xr-xchallenge-249/e-choroba/perl/ch-2.pl53
-rwxr-xr-xchallenge-249/e-choroba/python/ch-1.py20
-rwxr-xr-xchallenge-249/e-choroba/python/ch-2.py23
-rwxr-xr-xchallenge-249/eric-cheung/python/ch-1.py16
-rwxr-xr-xchallenge-249/eric-cheung/python/ch-2.py24
-rw-r--r--challenge-249/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-249/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-249/jeanluc2020/perl/ch-1.pl79
-rwxr-xr-xchallenge-249/jeanluc2020/perl/ch-2.pl62
-rwxr-xr-xchallenge-249/jeanluc2020/python/ch-1.py72
-rwxr-xr-xchallenge-249/jeanluc2020/python/ch-2.py59
-rw-r--r--challenge-249/luca-ferrari/blog-1.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-2.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-3.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-4.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-5.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-6.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-7.txt1
-rw-r--r--challenge-249/luca-ferrari/blog-8.txt1
-rw-r--r--challenge-249/luca-ferrari/postgresql/ch-1.plperl30
-rw-r--r--challenge-249/luca-ferrari/postgresql/ch-1.sql40
-rw-r--r--challenge-249/luca-ferrari/postgresql/ch-2.plperl35
-rw-r--r--challenge-249/luca-ferrari/postgresql/ch-2.sql16
-rw-r--r--challenge-249/luca-ferrari/python/ch-1.py34
-rw-r--r--challenge-249/luca-ferrari/python/ch-2.py37
-rw-r--r--challenge-249/luca-ferrari/raku/ch-1.p622
-rw-r--r--challenge-249/luca-ferrari/raku/ch-2.p623
-rw-r--r--challenge-249/mark-anderson/raku/ch-1.raku12
-rw-r--r--challenge-249/mark-anderson/raku/ch-2.raku22
-rw-r--r--challenge-249/packy-anderson/README.md82
-rw-r--r--challenge-249/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-249/packy-anderson/perl/ch-1.pl47
-rwxr-xr-xchallenge-249/packy-anderson/perl/ch-2.pl39
-rwxr-xr-xchallenge-249/packy-anderson/python/ch-1.py47
-rwxr-xr-xchallenge-249/packy-anderson/python/ch-2.py36
-rwxr-xr-xchallenge-249/packy-anderson/raku/ch-1.raku46
-rwxr-xr-xchallenge-249/packy-anderson/raku/ch-2.raku39
-rw-r--r--challenge-249/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-249/peter-campbell-smith/perl/ch-1.pl35
-rwxr-xr-xchallenge-249/peter-campbell-smith/perl/ch-2.pl77
-rwxr-xr-xchallenge-249/peter-meszaros/perl/ch-1.pl68
-rwxr-xr-xchallenge-249/peter-meszaros/perl/ch-2.pl86
-rw-r--r--challenge-249/pokgopun/go/ch-1.go88
-rw-r--r--challenge-249/pokgopun/go/ch-2.go159
-rw-r--r--challenge-249/pokgopun/python/ch-1.py57
-rw-r--r--challenge-249/pokgopun/python/ch-2.py73
-rwxr-xr-xchallenge-249/roger-bell-west/javascript/ch-1.js59
-rwxr-xr-xchallenge-249/roger-bell-west/javascript/ch-2.js71
-rw-r--r--challenge-249/roger-bell-west/kotlin/ch-1.kt32
-rw-r--r--challenge-249/roger-bell-west/kotlin/ch-2.kt45
-rwxr-xr-xchallenge-249/roger-bell-west/lua/ch-1.lua61
-rwxr-xr-xchallenge-249/roger-bell-west/lua/ch-2.lua79
-rwxr-xr-xchallenge-249/roger-bell-west/perl/ch-1.pl25
-rwxr-xr-xchallenge-249/roger-bell-west/perl/ch-2.pl29
-rw-r--r--challenge-249/roger-bell-west/postscript/ch-1.ps215
-rw-r--r--challenge-249/roger-bell-west/postscript/ch-2.ps276
-rwxr-xr-xchallenge-249/roger-bell-west/python/ch-1.py25
-rwxr-xr-xchallenge-249/roger-bell-west/python/ch-2.py35
-rwxr-xr-xchallenge-249/roger-bell-west/raku/ch-1.p623
-rwxr-xr-xchallenge-249/roger-bell-west/raku/ch-2.p627
-rwxr-xr-xchallenge-249/roger-bell-west/ruby/ch-1.rb30
-rwxr-xr-xchallenge-249/roger-bell-west/ruby/ch-2.rb39
-rwxr-xr-xchallenge-249/roger-bell-west/rust/ch-1.rs31
-rwxr-xr-xchallenge-249/roger-bell-west/rust/ch-2.rs39
-rw-r--r--challenge-249/roger-bell-west/scala/ch-1.scala33
-rw-r--r--challenge-249/roger-bell-west/scala/ch-2.scala46
-rw-r--r--challenge-249/roger-bell-west/tests.yaml44
-rw-r--r--challenge-249/steve-g-lynn/blog.txt1
-rwxr-xr-xchallenge-249/steve-g-lynn/perl/ch-1.pl44
-rwxr-xr-xchallenge-249/steve-g-lynn/perl/ch-2.pl32
-rwxr-xr-xchallenge-249/ulrich-rieke/cpp/ch-1.cpp55
-rwxr-xr-xchallenge-249/ulrich-rieke/cpp/ch-2.cpp63
-rwxr-xr-xchallenge-249/ulrich-rieke/haskell/ch-1.hs30
-rwxr-xr-xchallenge-249/ulrich-rieke/haskell/ch-2.hs22
-rwxr-xr-xchallenge-249/ulrich-rieke/perl/ch-1.pl33
-rwxr-xr-xchallenge-249/ulrich-rieke/perl/ch-2.pl45
-rwxr-xr-xchallenge-249/ulrich-rieke/raku/ch-1.raku34
-rwxr-xr-xchallenge-249/ulrich-rieke/raku/ch-2.raku36
-rwxr-xr-xchallenge-249/ulrich-rieke/rust/ch-1.rs30
-rwxr-xr-xchallenge-249/ulrich-rieke/rust/ch-2.rs42
-rw-r--r--challenge-249/zapwai/perl/ch-1.pl23
-rw-r--r--challenge-249/zapwai/perl/ch-2.pl34
91 files changed, 3673 insertions, 49 deletions
diff --git a/challenge-249/arne-sommer/blog.txt b/challenge-249/arne-sommer/blog.txt
new file mode 100644
index 0000000000..9c3ec53f8a
--- /dev/null
+++ b/challenge-249/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/equal-di.html
diff --git a/challenge-249/arne-sommer/raku/ch-1.raku b/challenge-249/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..96989560ee
--- /dev/null
+++ b/challenge-249/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,28 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@ints where @ints.elems %% 2 && @ints.elems > 0 && all(@ints) ~~ Int, :v(:$verbose));
+
+my @output;
+my @sorted = @ints>>.Int.sort;
+
+say ":Sorted: { @sorted.join(",") }" if $verbose;
+
+while @sorted
+{
+ my $first = @sorted.shift;
+ my $second = @sorted.shift;
+
+ if $first == $second
+ {
+ @output.push: ($first, $second);
+ say ":Pair: $first,$second" if $verbose;
+ }
+ else
+ {
+ say ":Non-pair: $first,$second" if $verbose;
+ say "()";
+ exit;
+ }
+}
+
+say @output.map({ "($_[0], $_[1])"}).join(", ");
diff --git a/challenge-249/arne-sommer/raku/ch-2.raku b/challenge-249/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..9f90401bc1
--- /dev/null
+++ b/challenge-249/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($s where $s ~~ /^<[ID]>+$/, :v(:$verbose));
+
+my @output;
+my @integers = (0 .. $s.chars);
+
+for $s.comb -> $char
+{
+ if $char eq "I"
+ {
+ @output.push: @integers.shift;
+ say ":I -> lowest integer { @output.tail }" if $verbose;
+ }
+ else
+ {
+ @output.push: @integers.pop;
+ say ":D -> highest integer { @output.tail }" if $verbose;
+ }
+}
+
+@output.push: @integers[0];
+say ": -> remaining integer { @output.tail }" if $verbose;
+
+say "({ @output.join(", ") })";
diff --git a/challenge-249/arne-sommer/raku/di-string-match b/challenge-249/arne-sommer/raku/di-string-match
new file mode 100755
index 0000000000..9f90401bc1
--- /dev/null
+++ b/challenge-249/arne-sommer/raku/di-string-match
@@ -0,0 +1,25 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($s where $s ~~ /^<[ID]>+$/, :v(:$verbose));
+
+my @output;
+my @integers = (0 .. $s.chars);
+
+for $s.comb -> $char
+{
+ if $char eq "I"
+ {
+ @output.push: @integers.shift;
+ say ":I -> lowest integer { @output.tail }" if $verbose;
+ }
+ else
+ {
+ @output.push: @integers.pop;
+ say ":D -> highest integer { @output.tail }" if $verbose;
+ }
+}
+
+@output.push: @integers[0];
+say ": -> remaining integer { @output.tail }" if $verbose;
+
+say "({ @output.join(", ") })";
diff --git a/challenge-249/arne-sommer/raku/equal-pairs b/challenge-249/arne-sommer/raku/equal-pairs
new file mode 100755
index 0000000000..96989560ee
--- /dev/null
+++ b/challenge-249/arne-sommer/raku/equal-pairs
@@ -0,0 +1,28 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@ints where @ints.elems %% 2 && @ints.elems > 0 && all(@ints) ~~ Int, :v(:$verbose));
+
+my @output;
+my @sorted = @ints>>.Int.sort;
+
+say ":Sorted: { @sorted.join(",") }" if $verbose;
+
+while @sorted
+{
+ my $first = @sorted.shift;
+ my $second = @sorted.shift;
+
+ if $first == $second
+ {
+ @output.push: ($first, $second);
+ say ":Pair: $first,$second" if $verbose;
+ }
+ else
+ {
+ say ":Non-pair: $first,$second" if $verbose;
+ say "()";
+ exit;
+ }
+}
+
+say @output.map({ "($_[0], $_[1])"}).join(", ");
diff --git a/challenge-249/dave-jacoby/blog.txt b/challenge-249/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..d526ba7bd9
--- /dev/null
+++ b/challenge-249/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2023/12/26/i-did-weekly-challenge-249.html
diff --git a/challenge-249/dave-jacoby/perl/ch-1.pl b/challenge-249/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..fa9ffc48d6
--- /dev/null
+++ b/challenge-249/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+my @examples = (
+
+ [ 3, 2, 3, 2, 2, 2 ],
+ [ 1, 2, 3, 4 ],
+);
+
+for my $example (@examples) {
+ my $input = join ', ', $example->@*;
+ my @output = equal_pairs( $example->@* );
+ my $output = join ', ',
+ map { qq{($_)} } map { join ', ', $_->@* } @output;
+
+ say <<~"END";
+ Input: \$ints = ($input)
+ Output: ($output)
+ END
+}
+
+sub equal_pairs (@input) {
+ my @output;
+ my %hash;
+ for my $i (@input) {
+ if ( $hash{$i} ) {
+ push @output, [ $i, $i ];
+ delete $hash{$i};
+ }
+ else {
+ $hash{$i} = 1;
+ }
+ }
+ return @output;
+}
diff --git a/challenge-249/dave-jacoby/perl/ch-2.pl b/challenge-249/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..171fe13288
--- /dev/null
+++ b/challenge-249/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use Algorithm::Permute;
+
+my @examples = ( "IDID", "III", "DDI" );
+
+for my $e (@examples) {
+ my @output = di_string_match($e);
+ my $output = join "\n ", sort
+ map { qq{($_)} }
+ map { join ', ', $_->@* } @output;
+
+ say <<~"END";
+ Input: \$str = $e
+
+ Output: $output
+ END
+}
+
+sub di_string_match ($str) {
+ my @output;
+ my @s = 0 .. length $str;
+ my $p = Algorithm::Permute->new( [@s] );
+OUTER: while ( my @perm = $p->next ) {
+ for my $i ( 0 .. -1 + length $str ) {
+ my $l = substr $str, $i, 1;
+ if ( $l eq 'I' ) {
+ next OUTER unless $perm[$i] < $perm[ $i + 1 ];
+ }
+ elsif ( $l eq 'D' ) {
+ next OUTER unless $perm[$i] > $perm[ $i + 1 ];
+ }
+ }
+ push @output, \@perm;
+ }
+
+ return @output;
+}
+
diff --git a/challenge-249/e-choroba/perl/ch-1.pl b/challenge-249/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..41f1d964ab
--- /dev/null
+++ b/challenge-249/e-choroba/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub equal_pairs_count(@ints) {
+ my %seen;
+ ++$seen{$_} for @ints;
+ return [map $seen{$_} % 2 ? return []
+ : ([$_, $_]) x ($seen{$_} / 2),
+ keys %seen]
+}
+
+sub equal_pairs_odd(@ints) {
+ my %odd;
+ my @pairs;
+ for my $i (@ints) {
+ if (exists $odd{$i}) {
+ delete $odd{$i};
+ push @pairs, [$i, $i];
+ } else {
+ undef $odd{$i};
+ }
+ }
+ return keys %odd ? [] : \@pairs
+}
+
+
+use Test2::V0 -srand => srand;
+plan 2 + 1;
+
+my $type = 'count';
+*equal_pairs = *equal_pairs_count{CODE};
+for (1, 2) {
+ subtest $type => sub {
+ plan 5;
+
+ is equal_pairs(3, 2, 3, 2, 2, 2),
+ bag { item $_ for [2, 2], [2, 2], [3, 3]; },
+ 'Example 1';
+
+ is equal_pairs(1, 2, 3, 4), [], 'Example 2';
+
+
+ is equal_pairs(-1, -1, -2, -2),
+ bag { item $_ for [-1, -1], [-2, -2]; },
+ 'Negative numbers';
+
+ is equal_pairs(1, 1, 1, 1, 2, 2, 2, 2),
+ bag { item $_ for [1, 1], [1, 1], [2, 2], [2, 2]; },
+ 'More than once';
+
+ is equal_pairs(1, 1, 1, 1, 2, 2, 2, 2, 1),
+ [],
+ 'More than once odd';
+ };
+
+ no warnings 'redefine';
+ $type = 'odd';
+ *equal_pairs = *equal_pairs_odd{CODE};
+}
+
+use Benchmark qw{ cmpthese };
+
+my @l = map int rand 1000, 1 .. 100_000;
+is equal_pairs_odd(@l), equal_pairs_count(@l), 'same';
+cmpthese(-3, {
+ odd => sub { equal_pairs_odd(@l) },
+ count => sub { equal_pairs_count(@l) },
+});
+