aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-135/perlboy1967/perl/ch-1.pl48
-rwxr-xr-xchallenge-135/perlboy1967/perl/ch-2.pl51
2 files changed, 99 insertions, 0 deletions
diff --git a/challenge-135/perlboy1967/perl/ch-1.pl b/challenge-135/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..e7dbcb9c6a
--- /dev/null
+++ b/challenge-135/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/bin/perl
+
+=pod
+
+Perl Weekly Challenge - 135
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-135/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+TASK #1 › Middle 3-digits
+Submitted by: Mohammad S Anwar
+
+You are given an integer.
+
+Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error.
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+use Test::More;
+
+# Prototype(s)
+sub middle3numbers($);
+
+is middle3numbers(1234567),345;
+is middle3numbers(1020304050607),'040';
+is middle3numbers(-123),123;
+is middle3numbers(1),'too short';
+is middle3numbers(1010),'even number of digits';
+is middle3numbers('Perl5'),'not an integer number';
+
+done_testing;
+
+
+sub middle3numbers($) {
+ my $n = $_[0];
+
+ return 'not an integer number' if $n !~ m#^[-]?\d+$#;
+
+ $n = abs($n);
+ my $l = length($n);
+ return 'too short' if $l < 3;
+ return 'even number of digits' if $l % 2 == 0;
+ return substr($n,int($l/2)-1,3);
+}
diff --git a/challenge-135/perlboy1967/perl/ch-2.pl b/challenge-135/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..8d4861195d
--- /dev/null
+++ b/challenge-135/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/bin/perl
+
+=pod
+
+Perl Weekly Challenge - 135
+ - https://perlweeklychallenge.org/blog/perl-weekly-challenge-135/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+SK #2 › Validate SEDOL
+Submitted by: Mohammad S Anwar
+
+You are given 7-characters alphanumeric SEDOL.
+
+Write a script to validate the given SEDOL. Print 1 if it is a valid SEDOL otherwise 0.
+
+For more information about SEDOL, please checkout the wikipedia page.
+
+=cut
+
+use v5.16;
+use strict;
+use warnings;
+
+use List::Util qw(sum);
+
+use Test::More;
+
+# Prototype(s)
+sub isSEDOL($);
+
+is isSEDOL(2936921),1;
+is isSEDOL(1234567),0;
+is isSEDOL('B0YBKL9'),1;
+is isSEDOL('A00000'),0;
+is isSEDOL('B000010'),1;
+
+done_testing;
+
+
+sub isSEDOL($) {
+ if (uc $_[0] =~ m#^([0-9B-DF-HJ-NP-TV-Z]{6})(\d)$#) {
+ my ($d,$c) = ($1,$2);
+ my @w = (1,3,1,7,3,9,1);
+ my $i = 0;
+ my %v = +map{($_,$i++)} (0..9,'A'..'Z');
+ return ((10-sum(map{$v{$_}*shift(@w)}split(//,$d))%10)%10)==$c?1:0;
+ } else {
+ return 0;
+ }
+}