aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-02-13 05:42:28 +0000
committerGitHub <noreply@github.com>2023-02-13 05:42:28 +0000
commit6e770d9c77f25c511ec6ea477b0366dd49daf68b (patch)
treec4fe7d6cde4675a43ec58c5de7c98f341a75e852
parentb77caa940de8043ed4c0cf15e01eccfff58576dd (diff)
parenta2d3c9e93ade6b9735dd78018281d385eec8a923 (diff)
downloadperlweeklychallenge-club-6e770d9c77f25c511ec6ea477b0366dd49daf68b.tar.gz
perlweeklychallenge-club-6e770d9c77f25c511ec6ea477b0366dd49daf68b.tar.bz2
perlweeklychallenge-club-6e770d9c77f25c511ec6ea477b0366dd49daf68b.zip
Merge pull request #7555 from polettix/polettix/pwc203
Add polettix's solution to challenge-203
-rw-r--r--challenge-203/polettix/blog.txt1
-rw-r--r--challenge-203/polettix/blog1.txt1
-rw-r--r--challenge-203/polettix/perl/ch-1.pl46
-rw-r--r--challenge-203/polettix/perl/ch-2.pl26
-rw-r--r--challenge-203/polettix/raku/ch-1.raku7
-rw-r--r--challenge-203/polettix/raku/ch-2.raku11
6 files changed, 92 insertions, 0 deletions
diff --git a/challenge-203/polettix/blog.txt b/challenge-203/polettix/blog.txt
new file mode 100644
index 0000000000..fad6941e2b
--- /dev/null
+++ b/challenge-203/polettix/blog.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2023/02/09/pwc203-special-quadruplets/
diff --git a/challenge-203/polettix/blog1.txt b/challenge-203/polettix/blog1.txt
new file mode 100644
index 0000000000..8ebf035315
--- /dev/null
+++ b/challenge-203/polettix/blog1.txt
@@ -0,0 +1 @@
+https://etoobusy.polettix.it/2023/02/10/pwc203-copy-directory/
diff --git a/challenge-203/polettix/perl/ch-1.pl b/challenge-203/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..6f799fe90a
--- /dev/null
+++ b/challenge-203/polettix/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util 'sum';
+
+say special_quadruplets(@ARGV);
+
+sub special_quadruplets (@nums) {
+ my $it = combinations_iterator(4, @nums);
+ my $count = 0;
+ while (my ($c, undef) = $it->()) {
+ ++$count if sum($c->@[0..2]) == $c->[3];
+ }
+ return $count;
+}
+
+sub combinations_iterator ($k, @items) {
+ my @indexes = (0 .. ($k - 1));
+ my $n = @items;
+ return sub {
+ return unless @indexes;
+ my (@combination, @remaining);
+ my $j = 0;
+ for my $i (0 .. ($n - 1)) {
+ if ($j < $k && $i == $indexes[$j]) {
+ push @combination, $items[$i];
+ ++$j;
+ }
+ else {
+ push @remaining, $items[$i];
+ }
+ }
+ for my $incc (reverse(-1, 0 .. ($k - 1))) {
+ if ($incc < 0) {
+ @indexes = (); # finished!
+ }
+ elsif ((my $v = $indexes[$incc]) < $incc - $k + $n) {
+ $indexes[$_] = ++$v for $incc .. ($k - 1);
+ last;
+ }
+ }
+ return (\@combination, \@remaining);
+ }
+}
diff --git a/challenge-203/polettix/perl/ch-2.pl b/challenge-203/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..ecaa623656
--- /dev/null
+++ b/challenge-203/polettix/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use English '-no_match_vars';
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+use File::Spec::Functions qw< splitpath splitdir catdir catpath >;
+use File::Path qw< make_path >;
+
+copy_directory(@ARGV);
+
+sub copy_directory ($from, $to) {
+ my ($fv, $fds) = splitpath($from, 'no-file');
+ my ($tv, $tds) = splitpath($to, 'no-file');
+ opendir my $dh, $from or die "opendir('$from'): $OS_ERROR";
+ for my $item (readdir($dh)) {
+ next if ($item eq '.') || ($item eq '..');
+ my $source = catpath($fv, $fds, $item);
+ next unless -d $source;
+ my (undef, undef, $mode) = stat($source);
+ my $target = catpath($tv, $tds, $item);
+ make_path($target, {mode => $mode});
+ __SUB__->($source, $target);
+ }
+}
diff --git a/challenge-203/polettix/raku/ch-1.raku b/challenge-203/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..8d66579dd0
--- /dev/null
+++ b/challenge-203/polettix/raku/ch-1.raku
@@ -0,0 +1,7 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN (*@args) { put special-quadruplets(@args) }
+
+sub special-quadruplets (@nums) {
+ combinations(@nums, 4).grep({$_[0..2].sum == $_[3]}).elems
+}
diff --git a/challenge-203/polettix/raku/ch-2.raku b/challenge-203/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..c8995907ef
--- /dev/null
+++ b/challenge-203/polettix/raku/ch-2.raku
@@ -0,0 +1,11 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN ($from, $to) { copy-directory($from, $to) }
+
+sub copy-directory (IO::Path() $from, IO::Path() $to) {
+ for $from.dir -> $source {
+ next unless $source.d;
+ my $target = $to.child($source.basename).mkdir($source.mode);
+ samewith($source, $target);
+ }
+}