aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-10-31 17:52:30 +0000
committerGitHub <noreply@github.com>2022-10-31 17:52:30 +0000
commit43b4edb64a0fadb7896927ce9a68df1aae0ec9a2 (patch)
tree280f53ade4ec82fb2839dd943ced535a9a2d5814
parentf9e6c54d3113cbe89568cc4c3b851081af55eafc (diff)
parentd8c3c697068dcb1e7d51d1894f1fa4136f6849be (diff)
downloadperlweeklychallenge-club-43b4edb64a0fadb7896927ce9a68df1aae0ec9a2.tar.gz
perlweeklychallenge-club-43b4edb64a0fadb7896927ce9a68df1aae0ec9a2.tar.bz2
perlweeklychallenge-club-43b4edb64a0fadb7896927ce9a68df1aae0ec9a2.zip
Merge pull request #7008 from polettix/polettix/pwc188
Add polettix's solution to challenge-188
-rw-r--r--challenge-188/polettix/blog.txt1
-rw-r--r--challenge-188/polettix/blog1.txt1
-rw-r--r--challenge-188/polettix/perl/ch-1.pl22
-rw-r--r--challenge-188/polettix/perl/ch-2.pl14
-rw-r--r--challenge-188/polettix/raku/ch-1.raku20
-rw-r--r--challenge-188/polettix/raku/ch-2.raku8
6 files changed, 66 insertions, 0 deletions
diff --git a/challenge-188/polettix/blog.txt b/challenge-188/polettix/blog.txt
new file mode 100644
index 0000000000..c27386f6c9
--- /dev/null
+++ b/challenge-188/polettix/blog.txt
@@ -0,0 +1 @@
+http://localhost:54000/2022/10/27/pwc188-divisible-pairs/
diff --git a/challenge-188/polettix/blog1.txt b/challenge-188/polettix/blog1.txt
new file mode 100644
index 0000000000..304d1e4d4e
--- /dev/null
+++ b/challenge-188/polettix/blog1.txt
@@ -0,0 +1 @@
+http://localhost:54000/2022/10/28/pwc188-total-zero/
diff --git a/challenge-188/polettix/perl/ch-1.pl b/challenge-188/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..692811859f
--- /dev/null
+++ b/challenge-188/polettix/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+say divisible_pairs(2, 4, 5, 1, 6);
+say divisible_pairs(4, 7, 2, 4, 5);
+say divisible_pairs(4, 7, 2, 6, 10, 1, 5, 3);
+
+sub divisible_pairs ($k, @list) {
+ my %rest_for;
+ $rest_for{$_ % $k}++ for @list;
+ my $handshakes = sub ($n) { int($n * ($n - 1) / 2) };
+ my $n = $handshakes->($rest_for{0} // 0);
+ for my $i (1 .. $k / 2) {
+ my $j = $k - $i;
+ $n += $j == $i ? $handshakes->($rest_for{$i} // 0)
+ : ($rest_for{$i} // 0) * ($rest_for{$j} // 0);
+ }
+ return $n;
+}
diff --git a/challenge-188/polettix/perl/ch-2.pl b/challenge-188/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..d13131374c
--- /dev/null
+++ b/challenge-188/polettix/perl/ch-2.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+
+my $x = shift // 5;
+my $y = shift // 4;
+say total_zero($x, $y);
+
+sub total_zero ($A, $B, $n = 0) {
+ ($A, $B, $n) = ($B % $A, $A, $n + int($B / $A)) while $A;
+ return $n;
+}
diff --git a/challenge-188/polettix/raku/ch-1.raku b/challenge-188/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..e61f73812e
--- /dev/null
+++ b/challenge-188/polettix/raku/ch-1.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN {
+ put divisible-pairs(2, [4, 5, 1, 6]);
+ put divisible-pairs(4, [7, 2, 4, 5]);
+ put divisible-pairs(4, [7, 2, 6, 10, 1, 5, 3]);
+}
+
+sub divisible-pairs ($k, @list) {
+ my %rest-for;
+ for @list -> $e { %rest-for{$e % $k}++ }
+ sub handshakes ($n) { (($n * ($n - 1)) / 2).Int }
+ my $n = handshakes(%rest-for{0} // 0);
+ for 1 .. $k / 2 -> $i {
+ my $j = $k - $i;
+ $n += $j == $i ?? handshakes(%rest-for{$i} // 0)
+ !! (%rest-for{$i} // 0) * (%rest-for{$j} // 0);
+ }
+ return $n;
+}
diff --git a/challenge-188/polettix/raku/ch-2.raku b/challenge-188/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..40d87b2124
--- /dev/null
+++ b/challenge-188/polettix/raku/ch-2.raku
@@ -0,0 +1,8 @@
+#!/usr/bin/env raku
+use v6;
+sub MAIN ($x = 5, $y = 4) { put total-zero($x, $y) }
+
+sub total-zero ($A is copy, $B is copy, $n is copy = 0) {
+ ($A, $B, $n) = $B % $A, $A, $n + ($B / $A).Int while $A;
+ return $n;
+}