diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-24 22:51:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-24 22:51:07 +0100 |
| commit | c3ffbc68fb0bdba2ee1583de84c9358c4df886d8 (patch) | |
| tree | 5c04c8aae969c33a2fa77f2f2fcd76a58b8dfcb7 | |
| parent | 52c4afe386b89945b680a4598538e2e575590411 (diff) | |
| parent | cc43f3c59a69186579f49930c34f9f8864289773 (diff) | |
| download | perlweeklychallenge-club-c3ffbc68fb0bdba2ee1583de84c9358c4df886d8.tar.gz perlweeklychallenge-club-c3ffbc68fb0bdba2ee1583de84c9358c4df886d8.tar.bz2 perlweeklychallenge-club-c3ffbc68fb0bdba2ee1583de84c9358c4df886d8.zip | |
Merge pull request #5092 from dcw803/master
imported my solutions to this week's tasks..
| -rw-r--r-- | challenge-135/duncan-c-white/README | 72 | ||||
| -rwxr-xr-x | challenge-135/duncan-c-white/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-135/duncan-c-white/perl/ch-2.pl | 92 |
3 files changed, 184 insertions, 34 deletions
diff --git a/challenge-135/duncan-c-white/README b/challenge-135/duncan-c-white/README index 83854546f8..9e73de07f7 100644 --- a/challenge-135/duncan-c-white/README +++ b/challenge-135/duncan-c-white/README @@ -1,52 +1,56 @@ -TASK #1 - Pandigital Numbers +TASK #1 - Middle 3-digits -Write a script to generate first 5 Pandigital Numbers in base 10. +You are given an integer. -As per the wikipedia, it says: +Write a script find out the middle 3-digits of the given integer, if +possible otherwise throw sensible error. -A pandigital number is an integer that in a given base has among its -significant digits each digit used in the base at least once. -The smallest base 10 pandigital number is 1023456789, and subsequent -ones are lexicographic permutations, ie. 1023456798, 1023456879... +Example 1 -MY NOTES: Basically just needs a standard lexicographic permutation algorithm, -with the initial value to permute being 1023456789. + Input: $n = 1234567 + Output: 345 -TASK #2 - Distinct Terms Count +Example 2 -You are given 2 positive numbers, $m and $n. + Input: $n = -123 + Output: 123 -Write a script to generate multiplication table and display count of distinct terms. +Example 3 -Example 1 + Input: $n = 1 + Output: too short + +Example 4 + + Input: $n = 10 + Output: even number of digits + +MY NOTES: Pretty easy, although it's not clear how to always treat negative numbers. +Assume abs() them. -Input: $m = 3, $n = 3 -Output: - x | 1 2 3 - --+------ - 1 | 1 2 3 - 2 | 2 4 6 - 3 | 3 6 9 +TASK #2 - Validate SEDOL + +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. + +For more information about SEDOL, please checkout https://en.wikipedia.org/wiki/SEDOL + +Example 1 -Distinct Terms: 1, 2, 3, 4, 6, 9 -Count: 6 + Input: $SEDOL = '2936921' + Output: 1 Example 2 -Input: $m = 3, $n = 5 -Output: + Input: $SEDOL = '1234567' + Output: 0 - x | 1 2 3 4 5 - --+-------------- - 1 | 1 2 3 4 5 - 2 | 2 4 6 8 10 - 3 | 3 6 9 12 15 +Example 3 -Distinct Terms: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 -Count: 11 + Input: $SEDOL = 'B0YBKL9' + Output: 1 +MY NOTES: Pretty easy -MY NOTES: Pretty easy, the distinct terms just need a set (hash) as usual. -The tricky bit is the pretty layout of the multiplication table, especially -getting the correct column widths.. diff --git a/challenge-135/duncan-c-white/perl/ch-1.pl b/challenge-135/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..44c5630e13 --- /dev/null +++ b/challenge-135/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +# +# TASK #1 - Middle 3-digits +# +# You are given an integer. +# +# Write a script find out the middle 3-digits of the given integer, if +# possible otherwise throw sensible error. +# +# Example 1 +# +# Input: $n = 1234567 +# Output: 345 +# +# Example 2 +# +# Input: $n = -123 +# Output: 123 +# +# Example 3 +# +# Input: $n = 1 +# Output: too short +# +# Example 4 +# +# Input: $n = 10 +# Output: even number of digits +# +# MY NOTES: Pretty easy, although it's not clear how to always treat negative numbers. +# Assume abs() them. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +#use Data::Dumper; + + +my $debug=0; +die "Usage: middle-3-digits N\n" unless + GetOptions( "debug"=>\$debug ) && @ARGV==1; +my $n = shift @ARGV; +$n = abs($n); + +my $l = length($n); +die "$n too short\n" if $l<3; + +die "even number of digits\n" if ($l&1)==0; + +my $midpos = int($l/2); + +say substr($n,$midpos-1,3); diff --git a/challenge-135/duncan-c-white/perl/ch-2.pl b/challenge-135/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..8d911c173a --- /dev/null +++ b/challenge-135/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl +# +# TASK #2 - Validate SEDOL +# +# 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. +# +# For more information about SEDOL, please checkout https://en.wikipedia.org/wiki/SEDOL +# +# Example 1 +# +# Input: $SEDOL = '2936921' +# Output: 1 +# +# Example 2 +# +# Input: $SEDOL = '1234567' +# Output: 0 +# +# Example 3 +# +# Input: $SEDOL = 'B0YBKL9' +# Output: 1 +# +# MY NOTES: Pretty easy translation of the Javascript version in the SEDOL web page. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; + +die "Usage: validate-sedol [-d|--debug] CODE\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV==1; +my $code = shift @ARGV; +my $l = length($code); + +die "$code length $l, not 7\n" unless $l == 7; + + +my @weight = (1, 3, 1, 7, 3, 9, 1); + + +# +# my( $checkdigit, $errormessage ) = sedol_check_digit( $code ); +# Take a 6-char SEDOL code, and calculate and return the +# 7th digit (the check digit), or (undef, $errormessage) to +# indicate an error. +# +fun sedol_check_digit( $code ) +{ + + unless( $code =~ /^[0-9BCDFGHJ-NP-TV-Z]{6}$/ ) + { + return (undef, "Invalid SEDOL number '$code'"); + } + my $sum = 0; + my $l = length($code); + $code = lc($code); + for( my $i = 0; $i < $l; $i++) + { + my $char = substr($code,$i,1); + my $base36 = ($char =~ /^\d/) ? ord($char)-ord('0') : ord($char)+10-ord('a'); + say "debug: code=$code, char=$char, base36=$base36, i=$i, w[i]==$weight[$i]" if $debug; + $sum += $weight[$i] * $base36; + } + my $check = (10 - $sum%10) % 10; + return ($check, undef); +} + + +# +# my $validsedol = checkSedol( $text ); +# Return 1 iff $text is a valid Sedol code; 0 otherwise. +# +fun checkSedol( $text ) +{ + my $input = substr($text,0,6); + my( $checkdigit, $error ) = sedol_check_digit($input); + return 0 if defined $error; + say "debug: checkSedol($text): checkdigit = $checkdigit" if $debug; + return $text eq $input . $checkdigit; +} + + +my $validsedol = checkSedol( $code ); +say $validsedol ? 1 : 0; |
