aboutsummaryrefslogtreecommitdiff
path: root/challenge-135
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-20 08:07:03 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-20 08:07:03 +0100
commit407abc71fee53f5e5477aa6cb19abb51741172ac (patch)
tree69c73bfb6b0859aafaf13a852f21bbe75f9fd7ed /challenge-135
parent703fcf238c31f42d6ad6c39204e3c6d2bf7f5bfb (diff)
downloadperlweeklychallenge-club-407abc71fee53f5e5477aa6cb19abb51741172ac.tar.gz
perlweeklychallenge-club-407abc71fee53f5e5477aa6cb19abb51741172ac.tar.bz2
perlweeklychallenge-club-407abc71fee53f5e5477aa6cb19abb51741172ac.zip
- Added solutions by Peter Campbell Smith.
Diffstat (limited to 'challenge-135')
-rwxr-xr-xchallenge-135/peter-campbell-smith/perl/ch-1.pl38
-rwxr-xr-xchallenge-135/peter-campbell-smith/perl/ch-2.pl67
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-135/peter-campbell-smith/perl/ch-1.pl b/challenge-135/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..ee7221110b
--- /dev/null
+++ b/challenge-135/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-10-18
+# PWC 135 task 1
+
+use v5.20;
+use warnings;
+use strict;
+
+# You are given an integer.
+
+# Write a script find out the middle 3-digits of the given integer, if possible
+# otherwise throw sensible error.
+
+my ($n, $length);
+
+# input
+print 'n: ';
+$n = <>;
+chomp($n);
+say qq[\nInput: $n];
+
+# output
+print 'Output: ';
+$length = length($n);
+
+if ($n !~ m|^\d+$|) {
+ say 'not an integer';
+
+} elsif ($length < 3) {
+ say 'too short';
+
+} elsif (($length & 1) == 0) {
+ say 'even number of digits';
+
+} else { # valid number
+ say substr($n, ($length - 3) / 2, 3);
+}
diff --git a/challenge-135/peter-campbell-smith/perl/ch-2.pl b/challenge-135/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..1be7631f5d
--- /dev/null
+++ b/challenge-135/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2021-10-18
+# PWC 135 task 2
+
+use v5.20;
+use warnings;
+use strict;
+
+# 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.
+
+# A valid SEDOL:
+# is 7 characters long
+# has the first 6 chars matching [B-DF-HJ-NP-TV-Z0-9]
+# has a digit 0-9 as the last char
+# has a weighted sum of digits (see @weights) which is a multiple of 10
+
+my (@weights, $sedol, @tests);
+
+@weights = (1, 3, 1, 7, 3, 9, 1); # weight of each char in the checksum (from Wikipedia)
+
+# check some known valid SEDOLs (taken from London Stock Exchange listings)
+say "\n--- VALID ---";
+@tests = qw[0263494 B7S9G98 B1VZ0M2 B12WC93 B6T5S47 BDZWB75 BLGZ986 0798059];
+for $sedol (@tests) {
+ check_sedol($sedol);
+}
+
+# check some known invalid SEDOLs
+say "\n--- INVALID ---";
+@tests = qw[BBBBBBB CCCCCC ZZZZAZ0 BCDFGH* BDZWB76 1798059];
+for $sedol (@tests) {
+ check_sedol($sedol);
+}
+
+sub check_sedol {
+
+ my ($SEDOL, $check_sum, $j, $char, $char_value);
+
+ $SEDOL = $_[0];
+ say qq[\nInput: \$SEDOL = $SEDOL];
+
+ # check length and valid chars
+ unless ($SEDOL =~ m|^[B-DF-HJ-NP-TV-Z0-9]{6}\d$|) {
+ say qq[Output: 0 (wrong length or invalid chars)];
+ return;
+ }
+
+ # calculate checksum
+ @weights = (1, 3, 1, 7, 3, 9, 1);
+ $check_sum = 0;
+ for $j (0..6) {
+ $char = substr($SEDOL, $j, 1);
+ $char_value = ord($char) - ($char =~ m|\d| ? ord('0') : ord('A') - 10);
+ $check_sum += $weights[$j] * $char_value;
+ }
+
+ # is it a multiple of 10?
+ if ($check_sum % 10 == 0) {
+ say 'Output: 1';
+ } else {
+ say qq[Output: 0 (checksum is $check_sum - not a multiple of 10)];
+ }
+}