aboutsummaryrefslogtreecommitdiff
path: root/challenge-135
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-22 20:53:44 +0100
committerGitHub <noreply@github.com>2021-10-22 20:53:44 +0100
commitb335471bf72d45eb289a792ea500e6a6f63d7f1a (patch)
tree7c9c6b19ba11164f1589432974ea2b24cec69c12 /challenge-135
parente48f96ddd530c7e69976d78c74cde7e3366bafc0 (diff)
parent3d3d6b5d3c5e08b6edb10e471cc358ccbcc2200b (diff)
downloadperlweeklychallenge-club-b335471bf72d45eb289a792ea500e6a6f63d7f1a.tar.gz
perlweeklychallenge-club-b335471bf72d45eb289a792ea500e6a6f63d7f1a.tar.bz2
perlweeklychallenge-club-b335471bf72d45eb289a792ea500e6a6f63d7f1a.zip
Merge pull request #5078 from oWnOIzRi/week135
organise code into separate files
Diffstat (limited to 'challenge-135')
-rw-r--r--challenge-135/steven-wilson/perl/ch-2.pl41
-rw-r--r--challenge-135/steven-wilson/perl/lib/SEDOL.pm34
-rw-r--r--challenge-135/steven-wilson/perl/t/01_validate_sedol.t12
3 files changed, 52 insertions, 35 deletions
diff --git a/challenge-135/steven-wilson/perl/ch-2.pl b/challenge-135/steven-wilson/perl/ch-2.pl
index b6e16c501c..f8f05e27bd 100644
--- a/challenge-135/steven-wilson/perl/ch-2.pl
+++ b/challenge-135/steven-wilson/perl/ch-2.pl
@@ -1,43 +1,14 @@
#!/usr/bin/env perl
# Week 135 Task 2
# Validate SEDOL
+# usage: $ perl ch-2.pl <SEDOL number>
+# tests: $ prove -l t/01_validate_sedol.t
use strict;
use warnings;
use feature qw/ say /;
-use Test::More;
-use Test::Exception;
+use lib 'lib';
+use SEDOL;
-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 );
-}
+my $input = $ARGV[0];
+say validate_sedol($input);
diff --git a/challenge-135/steven-wilson/perl/lib/SEDOL.pm b/challenge-135/steven-wilson/perl/lib/SEDOL.pm
new file mode 100644
index 0000000000..6f224e9b49
--- /dev/null
+++ b/challenge-135/steven-wilson/perl/lib/SEDOL.pm
@@ -0,0 +1,34 @@
+package SEDOL 0.001;
+use strict;
+use warnings;
+use parent qw/ Exporter /;
+
+our @EXPORT = qw/ validate_sedol /;
+
+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) ? return 1 : return 0;
+}
+
+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 );
+}
+
+1;
diff --git a/challenge-135/steven-wilson/perl/t/01_validate_sedol.t b/challenge-135/steven-wilson/perl/t/01_validate_sedol.t
new file mode 100644
index 0000000000..8057eb48fe
--- /dev/null
+++ b/challenge-135/steven-wilson/perl/t/01_validate_sedol.t
@@ -0,0 +1,12 @@
+use strict;
+use warnings;
+use Test::More tests => 6;
+use Test::Exception;
+use SEDOL;
+
+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";