From 3d3d6b5d3c5e08b6edb10e471cc358ccbcc2200b Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Fri, 22 Oct 2021 16:34:19 +0100 Subject: organise code into separate files --- challenge-135/steven-wilson/perl/ch-2.pl | 41 ++++------------------ challenge-135/steven-wilson/perl/lib/SEDOL.pm | 34 ++++++++++++++++++ .../steven-wilson/perl/t/01_validate_sedol.t | 12 +++++++ 3 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 challenge-135/steven-wilson/perl/lib/SEDOL.pm create mode 100644 challenge-135/steven-wilson/perl/t/01_validate_sedol.t 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 +# 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"; -- cgit