From f1f0e2555ff76d6dc26ab4336ccf9ae79ab25a9f Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 18 Oct 2021 11:18:11 +0100 Subject: 135 --- challenge-135/james-smith/perl/ch-1.pl | 31 ++++++++++++++++++++++++++++ challenge-135/james-smith/perl/ch-2.pl | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 challenge-135/james-smith/perl/ch-1.pl create mode 100644 challenge-135/james-smith/perl/ch-2.pl diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..a91fc49d88 --- /dev/null +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 1234567, 345 ], + [ -123, 123 ], + [ 1, 'Too short' ], + [ 'dred', 'Not a number' ], + [ 1000, 'Even digits' ], +); + +is( middle3($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub middle3 { + my $n = shift; + return 'Not a number' unless $n =~ m{^-?\d+$}; + return 'Too short' unless $n =~ m{\d{3}}; + return 'Even digits' if $n =~ m{^-?(?:\d\d)+$}; + $n =~ s{^-}{}; + return substr $n, (-3 + length $n ) / 2, 3; +} + diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..2585b4f41d --- /dev/null +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ '2936921', 1 ], + [ '1234567', 0 ], + [ 'B0YBKL9', 1 ], + [ '0263494', 1 ], + [ '0540528', 1 ], + [ 'BG03Y86', 1 ], +); + +is( is_sedol($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub is_sedol { +## Check correct format... + return 0 unless $_[0] =~ m{^[0-9B-HJ-NP-TW-Z]{6}\d$}; + +## Total and weights foreach digit + my( $t, @wts ) = qw(0 1 3 1 7 3 9 1); + +## Calculate SEDOL sum... note YODA sum -55 + ord $_ to avoid precedence issue + $t += shift @wts * ( $_ =~/[A-Z]/ ? -55 + ord $_ : $_ ) foreach split m//, $_[0]; + +## Return true if total modulo 10 is 0 + return $t % 10 ? 0 : 1; +} + -- cgit From 2ce0d1e77d6261b0a2ba07369482c78c7d237ab4 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 18 Oct 2021 11:21:50 +0100 Subject: fixed code --- challenge-135/james-smith/perl/ch-1.pl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl index a91fc49d88..62022269aa 100644 --- a/challenge-135/james-smith/perl/ch-1.pl +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -23,9 +23,9 @@ done_testing(); sub middle3 { my $n = shift; return 'Not a number' unless $n =~ m{^-?\d+$}; - return 'Too short' unless $n =~ m{\d{3}}; - return 'Even digits' if $n =~ m{^-?(?:\d\d)+$}; - $n =~ s{^-}{}; - return substr $n, (-3 + length $n ) / 2, 3; + $n = abs $n; + return length $n < 3 ? 'Too short' + : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3 + : 'Even digits'; } -- cgit From 624df489701537fdbe85a3d72447975258d61161 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 18 Oct 2021 14:51:27 +0100 Subject: tidyingup --- challenge-135/james-smith/perl/ch-1.pl | 14 ++++++++++++-- challenge-135/james-smith/perl/ch-2.pl | 13 +++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl index 62022269aa..780b93d005 100644 --- a/challenge-135/james-smith/perl/ch-1.pl +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -16,7 +16,8 @@ my @TESTS = ( [ 1000, 'Even digits' ], ); -is( middle3($_->[0]), $_->[1] ) foreach @TESTS; +is( middle3( $_->[0]), $_->[1] ) foreach @TESTS; +is( middle3no($_->[0]), $_->[1] ) foreach @TESTS[0..2,4]; done_testing(); @@ -26,6 +27,15 @@ sub middle3 { $n = abs $n; return length $n < 3 ? 'Too short' : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3 - : 'Even digits'; + : 'Even digits' + ; +} + +sub middle3no { + my $n = abs shift; + return length $n < 3 ? 'Too short' + : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3 + : 'Even digits' + ; } diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl index 2585b4f41d..1e0097ff0d 100644 --- a/challenge-135/james-smith/perl/ch-2.pl +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -14,24 +14,25 @@ my @TESTS = ( [ 'B0YBKL9', 1 ], [ '0263494', 1 ], [ '0540528', 1 ], + [ '1A34O67', 0 ], [ 'BG03Y86', 1 ], ); -is( is_sedol($_->[0]), $_->[1] ) foreach @TESTS; +is( is_sedol($_->[0]), $_->[1] ) for @TESTS; done_testing(); sub is_sedol { -## Check correct format... +## Check correct format... numbers and consonants only return 0 unless $_[0] =~ m{^[0-9B-HJ-NP-TW-Z]{6}\d$}; -## Total and weights foreach digit - my( $t, @wts ) = qw(0 1 3 1 7 3 9 1); +## Accumulator and weights for each charachter + my( $t, @w ) = qw(0 1 3 1 7 3 9 1); ## Calculate SEDOL sum... note YODA sum -55 + ord $_ to avoid precedence issue - $t += shift @wts * ( $_ =~/[A-Z]/ ? -55 + ord $_ : $_ ) foreach split m//, $_[0]; + $t += ( /\d/ ? $_ : -55 + ord $_ ) * shift @w for split //, $_[0]; -## Return true if total modulo 10 is 0 +## Return true (1) if total modulo 10 is 0, and false (0) otherwise return $t % 10 ? 0 : 1; } -- cgit From 763ef4dce78fef05d7b450ae9dd71c9cba94b4af Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 19 Oct 2021 16:31:20 +0100 Subject: added some golfed solutions --- challenge-135/james-smith/perl/ch-2.pl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl index 1e0097ff0d..9c09365af4 100644 --- a/challenge-135/james-smith/perl/ch-2.pl +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -19,6 +19,7 @@ my @TESTS = ( ); is( is_sedol($_->[0]), $_->[1] ) for @TESTS; +is( is_sedol_compact($_->[0]), $_->[1] ) for @TESTS; done_testing(); @@ -36,3 +37,10 @@ sub is_sedol { return $t % 10 ? 0 : 1; } +sub is_sedol_compact { + return 0 unless$_[0]=~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + my($t,@w)=qw(0 1 3 1 7 3 9 1); + $t+=(/\d/?$_:-55+ord$_)*shift@w for split//,$_[0]; + $t%10?0:1; +} + -- cgit From 8699e810931218e4437ae9c8926772616f249e06 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 19 Oct 2021 16:31:26 +0100 Subject: added some golfed solutions --- challenge-135/james-smith/perl/ch-1.pl | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl index 780b93d005..5f9b117675 100644 --- a/challenge-135/james-smith/perl/ch-1.pl +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -9,6 +9,8 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my @TESTS = ( + [ 123456789, 456 ], + [ -1234567, 345 ], [ 1234567, 345 ], [ -123, 123 ], [ 1, 'Too short' ], @@ -32,10 +34,7 @@ sub middle3 { } sub middle3no { - my $n = abs shift; - return length $n < 3 ? 'Too short' - : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3 - : 'Even digits' - ; + my$l=length(my$n=abs shift); + return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; } -- cgit From 0f5a34b1abdb36741fe2840f2d787d3efe587bd9 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 16:59:28 +0100 Subject: Update ch-1.pl --- challenge-135/james-smith/perl/ch-1.pl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl index 5f9b117675..dbbf9a33b7 100644 --- a/challenge-135/james-smith/perl/ch-1.pl +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -18,23 +18,23 @@ my @TESTS = ( [ 1000, 'Even digits' ], ); -is( middle3( $_->[0]), $_->[1] ) foreach @TESTS; -is( middle3no($_->[0]), $_->[1] ) foreach @TESTS[0..2,4]; +is( middle3( $_->[0]), $_->[1] ) foreach @TESTS; +is( middle3compact( $_->[0]), $_->[1] ) foreach @TESTS[0..4,6]; done_testing(); sub middle3 { my $n = shift; return 'Not a number' unless $n =~ m{^-?\d+$}; - $n = abs $n; - return length $n < 3 ? 'Too short' - : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3 - : 'Even digits' + my $l = length( $n = abs $n ); + return $l < 3 ? 'Too short' + : $l % 2 ? substr $n, ( $l - 3 ) / 2, 3 + : 'Even digits' ; } -sub middle3no { - my$l=length(my$n=abs shift); +sub middle3compact { + my$l=length(my$n=abs$_[0]); return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; } -- cgit From bb11efe8163a07c07c4c53edd0d82c6096bfeb90 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 17:10:32 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 95 ++++++++++++++++++++++++------------- 1 file changed, 62 insertions(+), 33 deletions(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index 1d17e5726f..dbb2c8cdca 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -1,4 +1,4 @@ -# Perl Weekly Challenge #134 +# Perl Weekly Challenge #135 You can find more information about this weeks, and previous weeks challenges at: @@ -10,57 +10,86 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-134/james-smith/perl +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-135/james-smith/perl -# Task 1 - Pandigital Numbers +# Task 1 - Middle three digits -***Write a script to generate first 5 Pandigital Numbers in base 10.*** +***You are given an integer. Write a script find out the middle 3-digits of the given integer, if possible otherwise throw sensible error.*** ## The solution -Pandigital numbers (base 10) are numbers who contain all the digits between 0-9 (but don't start with 0) -The lowest pandigital numbers all are permutations of the digits 0..9 with 0 not being the first digit. +Obviously using `substr $n, ??, 3` will give a 3-digit chunk of a number - so what are the errors... -To walk the pandigital numbers we can write a script which generates the next perumtation in "lexical" order. You don't need to do this recursively or with tightly nested loops a simple algorithm finds them... + * Not an integer - doesn't match `/^-?\d+$/` + * Less than 3 digits - length less than 3 + * Does not have a unique "central 3-digits" *i.e.* has even length - * Find the largest value of `$i` such that `$s[$i]` `<` `$s[$i+1]` - * Find the largest value of `$j` such that `$s[$i]` `<` `$s[$j]` - * Flip these to entries - * Flip all entries from $i+1 to end of the list... +The value of the `??` is the expression above - `( length number - 3 ) / 2` -We note though that we don't have to start at 0123456789 (the lowest permutation) as all numbers starting with 0 are skipped. We can then pre-empty this loop by noting the largest permutation 0987654321 which isn't a pan-digital number, so when we find the next iteration we have our first pandigital number.... +All that gives us the simple function.... ```perl -my @s = reverse 1..9, 0; - -sub next_perm { - my( $i, $j ); - ( $s[$_] < $s[$_+1] ) && ( $i = $_ ) foreach 0 .. @s-2; - return unless defined $i; - ( $s[$i] < $s[$_] ) && ( $j = $_ ) foreach $i+1 .. @s-1; - @s[ $i, $j ] = @s[ $j, $i ]; - @s[ $i+1 .. @s-1 ] = @s[ reverse $i+1 .. @s-1 ]; - return 1; +sub middle3 { + my $n = shift; + return 'Not a number' unless $n =~ m{^-?\d+$}; + my $l = length( $n = abs $n ); + return $l < 3 ? 'Too short' + : $l % 2 ? substr $n, ( $l - 3 ) / 2, 3 + : 'Even digits' + ; } +``` + +It is possible to compact this slightly - buy 1 - assuming `$n` is an integer, and then rewriting `($l-3)/2` as `$l/2-1` - which is good enough for the `substr` to work. -say @s while next_perm && $count--; +```perl +sub middle3compact { + my$l=length(my$n=abs$_[0]); + return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; +} ``` -# Task 2 - Distinct Terms Count +# Task 2 - Validate SEDOL -***You are given 2 positive numbers, `$m` and `$n`. Write a script to generate multiplcation table and display count of distinct terms.*** +***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.*** ## Solution -Number 2 again is the easier code this week... -We just loop through the two indicies and make a note of each product as a keys of a hash. And return scalar +You find about SEDOL numbers on Wikipedia at https://en.wikipedia.org/wiki/SEDOL. + +The consist of 6 digits/consonants + a checksum digit. The weighted sum of the 6 digits + the checksum is a multiple of 10. +The weights are 1, 3, 1, 7, 3 and 9 for the six digits and the checksum. + +We have to: + * validate the number is a SEDOL number + * compute the weighted sum + * check to see if it is a multiple of 10 ```perl - my($m,$n,%x) = @_; - for my $i (1..$m) { - $x{$i*$_}++ for 1..$n; - } - return scalar keys %x; +sub is_sedol { +## Check correct format... numbers and consonants only + return 0 unless $_[0] =~ m{^[0-9B-HJ-NP-TW-Z]{6}\d$}; + +## Accumulator and weights for each charachter + my( $t, @w ) = qw(0 1 3 1 7 3 9 1); + +## Calculate SEDOL sum... note YODA sum -55 + ord $_ to avoid precedence issue + $t += ( /\d/ ? $_ : -55 + ord $_ ) * shift @w for split //, $_[0]; + +## Return true (1) if total modulo 10 is 0, and false (0) otherwise + return $t % 10 ? 0 : 1; +} ``` +Again we can compact the code - by removing spaces and a couple of rewrites: -If `$m` & `$n` are large (and similar) there may be gain in separating the rectangle into a square and a rectangle - and only compute products for one half of the triangle... + * replace `unless $x=~//` with `if $x!~//`; + * replace `$x?0:1` with `1^!$x`. + +```perl +sub is_sedol_compact { + return 0 if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + my($t,@w)=qw(0 1 3 1 7 3 9 1); + $t+=(/\d/?$_:-55+ord$_)*shift@w for split//,$_[0]; + 1^!$t%10; +} +``` -- cgit From f0c53411527854718c62824b270aac3d0d2f041b Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 17:17:19 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index dbb2c8cdca..79fea8463a 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -83,13 +83,13 @@ sub is_sedol { Again we can compact the code - by removing spaces and a couple of rewrites: * replace `unless $x=~//` with `if $x!~//`; - * replace `$x?0:1` with `1^!$x`. + * flip `@w` and use `pop`. ```perl sub is_sedol_compact { return 0 if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; - my($t,@w)=qw(0 1 3 1 7 3 9 1); - $t+=(/\d/?$_:-55+ord$_)*shift@w for split//,$_[0]; - 1^!$t%10; + my($t,@w)=qw(0 1 9 3 7 1 3 1); + $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; + $t%10?0:1; } ``` -- cgit From 1819858c52b80c4c0ad6f5aaec86145f8133319c Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 17:18:30 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index 79fea8463a..1681821363 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -87,7 +87,7 @@ Again we can compact the code - by removing spaces and a couple of rewrites: ```perl sub is_sedol_compact { - return 0 if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + return 0if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; my($t,@w)=qw(0 1 9 3 7 1 3 1); $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; $t%10?0:1; -- cgit From a9ca60217253cfa36cd97314e99741214db1704c Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 17:19:58 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index 1681821363..c9d91e5434 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -45,7 +45,7 @@ It is possible to compact this slightly - buy 1 - assuming `$n` is an integer, a ```perl sub middle3compact { my$l=length(my$n=abs$_[0]); - return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; + $l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; } ``` -- cgit From a4bcff6a73947487e3e38ea21b9e2e6acade2ebf Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 19 Oct 2021 19:46:31 +0100 Subject: shorter --- challenge-135/james-smith/perl/ch-1.pl | 2 +- challenge-135/james-smith/perl/ch-2.pl | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl index dbbf9a33b7..cf1329eb75 100644 --- a/challenge-135/james-smith/perl/ch-1.pl +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -35,6 +35,6 @@ sub middle3 { sub middle3compact { my$l=length(my$n=abs$_[0]); - return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; + return$l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits' } diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl index 9c09365af4..4af6ee9a24 100644 --- a/challenge-135/james-smith/perl/ch-2.pl +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -18,7 +18,7 @@ my @TESTS = ( [ 'BG03Y86', 1 ], ); -is( is_sedol($_->[0]), $_->[1] ) for @TESTS; +is( is_sedol( $_->[0]), $_->[1] ) for @TESTS; is( is_sedol_compact($_->[0]), $_->[1] ) for @TESTS; done_testing(); @@ -38,9 +38,9 @@ sub is_sedol { } sub is_sedol_compact { - return 0 unless$_[0]=~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; - my($t,@w)=qw(0 1 3 1 7 3 9 1); - $t+=(/\d/?$_:-55+ord$_)*shift@w for split//,$_[0]; - $t%10?0:1; + return 0if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + my($t,@w)=qw(0 1 9 3 7 1 3 1); + $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; + $t%10?0:1 } -- cgit From 7f2b5ae1775bc18ecf742e6932087846d507607c Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 19:55:51 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index c9d91e5434..262d090ea3 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -45,7 +45,7 @@ It is possible to compact this slightly - buy 1 - assuming `$n` is an integer, a ```perl sub middle3compact { my$l=length(my$n=abs$_[0]); - $l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits'; + $l<3?'Too short':$l%2?substr$n,$l/2-1,3:'Even digits' } ``` @@ -55,14 +55,14 @@ sub middle3compact { ## Solution -You find about SEDOL numbers on Wikipedia at https://en.wikipedia.org/wiki/SEDOL. +You find about SEDOL (Stock Exchange Daily Official List) numbers on Wikipedia at https://en.wikipedia.org/wiki/SEDOL. The consist of 6 digits/consonants + a checksum digit. The weighted sum of the 6 digits + the checksum is a multiple of 10. -The weights are 1, 3, 1, 7, 3 and 9 for the six digits and the checksum. +The weights are 1, 3, 1, 7, 3 and 9 for the six digits and 1 for the checksum. We have to: - * validate the number is a SEDOL number - * compute the weighted sum + * validate the number is of valid format for a SEDOL number - 6 numbers or consonants & a single number. + * compute the weighted sum (using ord to convert the B-Z characters to their numeric equivalends) * check to see if it is a multiple of 10 ```perl @@ -70,7 +70,7 @@ sub is_sedol { ## Check correct format... numbers and consonants only return 0 unless $_[0] =~ m{^[0-9B-HJ-NP-TW-Z]{6}\d$}; -## Accumulator and weights for each charachter +## Accumulator and weights for each character my( $t, @w ) = qw(0 1 3 1 7 3 9 1); ## Calculate SEDOL sum... note YODA sum -55 + ord $_ to avoid precedence issue @@ -80,16 +80,18 @@ sub is_sedol { return $t % 10 ? 0 : 1; } ``` + Again we can compact the code - by removing spaces and a couple of rewrites: * replace `unless $x=~//` with `if $x!~//`; * flip `@w` and use `pop`. + * Note `0if` expands as `0 if`. ```perl sub is_sedol_compact { return 0if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; my($t,@w)=qw(0 1 9 3 7 1 3 1); $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; - $t%10?0:1; + $t%10?0:1 } ``` -- cgit From 7a410af7fbfab9fb4436a9d310f052285cc229b4 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 19:56:15 +0100 Subject: Create blog.txt --- challenge-135/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-135/james-smith/blog.txt diff --git a/challenge-135/james-smith/blog.txt b/challenge-135/james-smith/blog.txt new file mode 100644 index 0000000000..c0f74c980b --- /dev/null +++ b/challenge-135/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/blob/master/challenge-135/james-smith/ -- cgit From a7aeb6dfe5a9f7e1e5fe5641fc859711ca5b0e42 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 19 Oct 2021 19:58:13 +0100 Subject: 1 char shorter --- challenge-135/james-smith/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl index 4af6ee9a24..e0911fe6bd 100644 --- a/challenge-135/james-smith/perl/ch-2.pl +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -38,7 +38,7 @@ sub is_sedol { } sub is_sedol_compact { - return 0if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + return 0if$_[0]!~/^[\dB-HJ-NP-TW-Z]{6}\d$/; my($t,@w)=qw(0 1 9 3 7 1 3 1); $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; $t%10?0:1 -- cgit From d61c0fba73a6b0c05f042c0d8f294fba221cc8e5 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 19 Oct 2021 19:58:32 +0100 Subject: Update README.md --- challenge-135/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-135/james-smith/README.md b/challenge-135/james-smith/README.md index 262d090ea3..abbce3740c 100644 --- a/challenge-135/james-smith/README.md +++ b/challenge-135/james-smith/README.md @@ -89,7 +89,7 @@ Again we can compact the code - by removing spaces and a couple of rewrites: ```perl sub is_sedol_compact { - return 0if$_[0]!~/^[0-9B-HJ-NP-TW-Z]{6}\d$/; + return 0if$_[0]!~/^[\dB-HJ-NP-TW-Z]{6}\d$/; my($t,@w)=qw(0 1 9 3 7 1 3 1); $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; $t%10?0:1 -- cgit From 656374745c58a605f0cf4ee19882f68f17a14820 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Tue, 19 Oct 2021 22:51:10 +0100 Subject: thanks Eliza - forget ord was one of the functions for which you don't need to include $_ as it works on that automatically --- challenge-135/james-smith/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl index e0911fe6bd..2442b6abd6 100644 --- a/challenge-135/james-smith/perl/ch-2.pl +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -40,7 +40,7 @@ sub is_sedol { sub is_sedol_compact { return 0if$_[0]!~/^[\dB-HJ-NP-TW-Z]{6}\d$/; my($t,@w)=qw(0 1 9 3 7 1 3 1); - $t+=(/\d/?$_:-55+ord$_)*pop@w for split//,$_[0]; + $t+=(/\d/?$_:-55+ord)*pop@w for split//,$_[0]; $t%10?0:1 } -- cgit