aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-21 18:07:03 +0100
committerGitHub <noreply@github.com>2021-10-21 18:07:03 +0100
commit030c49b931daa4828424c9c8ca0cdadf58155642 (patch)
treeba2edccd6dda42d84b7df9ae14462a10cffa646e
parent85971bca6d5b8e0cad0d512a494bb0aac5c49869 (diff)
parent246074e3e59150f329a63cd5c4dc0089900d3d2b (diff)
downloadperlweeklychallenge-club-030c49b931daa4828424c9c8ca0cdadf58155642.tar.gz
perlweeklychallenge-club-030c49b931daa4828424c9c8ca0cdadf58155642.tar.bz2
perlweeklychallenge-club-030c49b931daa4828424c9c8ca0cdadf58155642.zip
Merge pull request #5065 from polettix/polettix/pwc135
Add polettix's solution to challenge-135
-rw-r--r--challenge-135/polettix/blog.txt1
-rw-r--r--challenge-135/polettix/blog1.txt1
-rw-r--r--challenge-135/polettix/perl/ch-1.pl14
-rw-r--r--challenge-135/polettix/perl/ch-2.pl18
-rw-r--r--challenge-135/polettix/raku/ch-1.raku10
-rw-r--r--challenge-135/polettix/raku/ch-2.raku13
6 files changed, 57 insertions, 0 deletions
diff --git a/challenge-135/polettix/blog.txt b/challenge-135/polettix/blog.txt
new file mode 100644
index 0000000000..028ee3317e
--- /dev/null
+++ b/challenge-135/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/10/20/pwc135-middle-3-digits/
diff --git a/challenge-135/polettix/blog1.txt b/challenge-135/polettix/blog1.txt
new file mode 100644
index 0000000000..90c2fe5497
--- /dev/null
+++ b/challenge-135/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2021/10/21/pwc135-validate-sedol/
diff --git a/challenge-135/polettix/perl/ch-1.pl b/challenge-135/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..6e4aca9b23
--- /dev/null
+++ b/challenge-135/polettix/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+sub middle_three_digits ($x) {
+ die "not an integer\n" unless $x =~ m{\A(?: 0 | -? [1-9]\d* )\z}mxs;
+ $x = -$x if $x < 0;
+ my $l = length $x;
+ die "too short\n" if $l < 3;
+ die "even number of digits\n" unless $l % 2;
+ return substr $x, ($l - 3) / 2, 3;
+}
+say middle_three_digits(shift // 1234567);
diff --git a/challenge-135/polettix/perl/ch-2.pl b/challenge-135/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..3c6d2ee4ef
--- /dev/null
+++ b/challenge-135/polettix/perl/ch-2.pl
@@ -0,0 +1,18 @@
+
+#!/usr/bin/env perl
+use v5.24;
+use warnings;
+use experimental 'signatures';
+no warnings 'experimental::signatures';
+use List::Util 'sum';
+sub validate_SEDOL ($s) {
+ state $weights = [1, 3, 1, 7, 3, 9, 1];
+ return 0 if $s !~ m{\A [0-9B-DF-HJ-NP-TV-Z]{6} [0-9] \z}mxs;
+ my @s = split m{}mxs, $s;
+ my $sum = sum map {
+ my $n = $s[$_] le '9' ? $s[$_] + 0 : ord($s[$_]) - ord('A') + 10;
+ $weights->[$_] * $n;
+ } 0 .. 6;
+ return $sum % 10 ? 0 : 1;
+}
+say validate_SEDOL(shift // 'B0YBKL9');
diff --git a/challenge-135/polettix/raku/ch-1.raku b/challenge-135/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..2afda14596
--- /dev/null
+++ b/challenge-135/polettix/raku/ch-1.raku
@@ -0,0 +1,10 @@
+#!/usr/bin/env raku
+use v6;
+sub middle-three-digits (Int:D $x is copy) {
+ $x = -$x if $x < 0;
+ my $l = $x.chars;
+ die "too short\n" if $l < 3;
+ die "even number of digits\n" if $l %% 2;
+ $x.substr(($l - 3) / 2, 3);
+}
+put middle-three-digits((@*ARGS[0] || 1234567).Int);
diff --git a/challenge-135/polettix/raku/ch-2.raku b/challenge-135/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..1c980f55d2
--- /dev/null
+++ b/challenge-135/polettix/raku/ch-2.raku
@@ -0,0 +1,13 @@
+#!/usr/bin/env raku
+use v6;
+sub validate-SEDOL (Str() $s) {
+ state @weights = 1, 3, 1, 7, 3, 9, 1;
+ return 0 if $s !~~ /^ <[0..9 B..D F..H J..N P..T V..Z]> ** 6 <[ 0..9 ]> $/;
+ my $sum = (0 .. 6).map({
+ my $c = $s.substr($_, 1);
+ my $n = $c le '9' ?? $c + 0 !! $c.ord - 'A'.ord + 10;
+ @weights[$_] * $n;
+ }).sum;
+ return $sum % 10 ?? 0 !! 1;
+}
+put validate-SEDOL(@*ARGS[0] || 2936921);