aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Wilson <steven1170@zoho.eu>2021-10-21 20:50:47 +0100
committerSteven Wilson <steven1170@zoho.eu>2021-10-21 20:50:47 +0100
commitb86914d5288b0edf7b10e9c81542b70fc20c63a6 (patch)
tree1b5d7de08e76d7687e769a6b96060476c3dfffc3
parentf4164700e4e99fa47b8c0f3c7b25cd7504f2d70d (diff)
downloadperlweeklychallenge-club-b86914d5288b0edf7b10e9c81542b70fc20c63a6.tar.gz
perlweeklychallenge-club-b86914d5288b0edf7b10e9c81542b70fc20c63a6.tar.bz2
perlweeklychallenge-club-b86914d5288b0edf7b10e9c81542b70fc20c63a6.zip
add solution week 135 task 2 in perl
-rw-r--r--challenge-135/steven-wilson/perl/ch-2.pl43
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-135/steven-wilson/perl/ch-2.pl b/challenge-135/steven-wilson/perl/ch-2.pl
new file mode 100644
index 0000000000..b6e16c501c
--- /dev/null
+++ b/challenge-135/steven-wilson/perl/ch-2.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+# Week 135 Task 2
+# Validate SEDOL
+
+use strict;
+use warnings;
+use feature qw/ say /;
+use Test::More;
+use Test::Exception;
+
+ok( validate_sedol('2936921') == 1, "Valid SEDOL" );
+ok( validate_sedol('1234567') == 0, "Invalid SEDOL" );
+ok( validate_sedol('B0YBKL9') == 1, "Valid SEDOL" );
+dies_ok { validate_sedol('B0YBKLA'), } 'Die when check digit not a digit';
+dies_ok { validate_sedol('B0YBKLF9'), } "Die when SEDOL not correct length";
+dies_ok { validate_sedol('#0YDK29'), } "Die when invalid character";
+done_testing();
+
+sub validate_sedol {
+ my $input = shift;
+ length $input == 7 or die "Invalid SEDOL number, not correct length\n";
+ my $code = substr $input, 0, 6;
+ my $check = substr $input, 6, 1;
+ $check =~ /[0-9]/ or die "Invalid SEDOL number, invalid check digit\n";
+ $check == get_sedol_check_digit($code);
+}
+
+sub get_sedol_check_digit {
+ my $code = shift;
+ my @weight = qw/ 1 3 1 7 3 9 1 /;
+ $code =~ /[0-9A-Z]{6}/ or die "Invalid SEDOL number, invalid character\n";
+ my @code = split //, $code;
+ my $sum = 0;
+ for ( my $i = 0; $i < 6; $i++ ) {
+ if ( ord( $code[$i] ) < 58 ) {
+ $sum += ( $code[$i] * $weight[$i] );
+ }
+ else {
+ $sum += ( ( ord( $code[$i] ) - 64 + 9 ) * $weight[$i] );
+ }
+ }
+ return ( ( 10 - ( $sum % 10 ) ) % 10 );
+}