aboutsummaryrefslogtreecommitdiff
path: root/challenge-135
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-24 23:28:15 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-10-24 23:28:15 +0100
commit8965e32c8543a849129f89812cbc9a0b979f56c7 (patch)
treef1eaa0d75f1a5fda20386780d038c4cb7ed1c096 /challenge-135
parent8c6924d3d3dd0b024470812df4ab7f05bb0604c7 (diff)
downloadperlweeklychallenge-club-8965e32c8543a849129f89812cbc9a0b979f56c7.tar.gz
perlweeklychallenge-club-8965e32c8543a849129f89812cbc9a0b979f56c7.tar.bz2
perlweeklychallenge-club-8965e32c8543a849129f89812cbc9a0b979f56c7.zip
- Added solutions by Colin Crain.
Diffstat (limited to 'challenge-135')
-rwxr-xr-xchallenge-135/colin-crain/perl/ch-1.pl77
-rwxr-xr-xchallenge-135/colin-crain/perl/ch-2.pl82
-rwxr-xr-xchallenge-135/colin-crain/raku/ch-1.raku55
-rwxr-xr-xchallenge-135/colin-crain/raku/ch-2.raku50
4 files changed, 264 insertions, 0 deletions
diff --git a/challenge-135/colin-crain/perl/ch-1.pl b/challenge-135/colin-crain/perl/ch-1.pl
new file mode 100755
index 0000000000..b7c9f3dac5
--- /dev/null
+++ b/challenge-135/colin-crain/perl/ch-1.pl
@@ -0,0 +1,77 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# 135-1-middling-thruppence.pl
+#
+# Middle 3-digits
+# Submitted by: Mohammad S Anwar
+# 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
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+## main
+say middle_three( shift @ARGV ) if @ARGV;
+
+
+
+sub middle_three ( $num ) {
+
+ $num =~ s/^-//;
+ return "input does not appear to be a number"
+ if $num =~ /\D/;
+
+ my $len = length $num;
+ return "input too short"
+ if $len < 3
+ return "input has an even number of digits"
+ unless $len % 2
+
+ while ( length $num > 3 ) {
+ substr $num, -1, 1, '';
+ substr $num, 0, 1, '';
+ }
+
+ return $num;
+}
+
+
+
+use Test::More;
+
+is middle_three( 1234567 ), 345, 'ex-1';
+is middle_three( -123 ), 123, 'ex-2';
+is middle_three( 1 ), "input too short", 'ex-3';
+is middle_three( 1000 ), "input has an even number of digits", 'ex-4 variant';
+is middle_three( "9BAD0" ), "input does not appear to be a number", 'alphanumeric';
+
+
+done_testing();
diff --git a/challenge-135/colin-crain/perl/ch-2.pl b/challenge-135/colin-crain/perl/ch-2.pl
new file mode 100755
index 0000000000..088be8a56d
--- /dev/null
+++ b/challenge-135/colin-crain/perl/ch-2.pl
@@ -0,0 +1,82 @@
+#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl
+#
+# do-they-like-us-at-all.pl
+#
+# Validate SEDOL
+# Submitted by: Mohammad S Anwar
+# 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 the wikipedia page.
+#
+# Example 1
+# Input: $SEDOL = '2936921'
+# Output: 1
+#
+# Example 2
+# Input: $SEDOL = '1234567'
+# Output: 0
+#
+# Example 3
+# Input: $SEDOL = 'B0YBKL9'
+# Output: 1
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use warnings;
+use strict;
+use utf8;
+use feature ":5.26";
+use feature qw(signatures);
+no warnings 'experimental::signatures';
+
+
+## main
+say validate_sedol( shift @ARGV ) if @ARGV;
+
+
+
+sub validate_sedol ( $candidate ) {
+
+ return 0 if $candidate =~ m/ [AEIOU] | \W /ix or length $candidate != 7 ;
+
+ ## assign alphanumeric values
+ my $val = -1;
+ my %clookup = map { $_ => ++$val } (0..9, 'A'..'Z');
+
+ ## fixed SEDOL weight values
+ my $ws = 0;
+ my @weights = (1, 3, 1, 7, 3, 9, 1);
+
+ $ws += $weights[$_] * $clookup{ substr $candidate, $_, 1 } for (0..5);
+
+ my $cs_calculated = ( 10 - $ws % 10) % 10;
+ my $cs_digit = substr $candidate, 6, 1;
+
+ return $cs_digit == $cs_calculated
+ ? 1
+ : 0 ;
+}
+
+
+
+
+
+
+
+use Test::More;
+
+is validate_sedol( "0263494" ), 1, 'ex-0263494';
+is validate_sedol( "02634940" ), 0, 'too long';
+is validate_sedol( "026349" ), 0, 'too short';
+is validate_sedol( "0263495" ), 0, 'wrong checksum';
+is validate_sedol( "B2634Z9" ), 1, 'alpha, starts with B';
+
+done_testing();
+
+
diff --git a/challenge-135/colin-crain/raku/ch-1.raku b/challenge-135/colin-crain/raku/ch-1.raku
new file mode 100755
index 0000000000..7d7d4c02b6
--- /dev/null
+++ b/challenge-135/colin-crain/raku/ch-1.raku
@@ -0,0 +1,55 @@
+#!/usr/bin/env perl6
+#
+#
+# 135-1-middling-thruppence.raku
+#
+# Middle 3-digits
+# Submitted by: Mohammad S Anwar
+# 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
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use Test;
+
+is mid3( 1234567 ), 345, 'ex-1';
+is mid3( -123 ), 123, 'ex-2';
+is mid3( 1 ), "input too short", 'ex-3';
+is mid3( 1000 ), "even number of digits", 'ex-4';
+
+
+sub mid3 ( $num is copy ) {
+
+ $num .= abs;
+
+ given $num.chars {
+ when $_ < 3 { return "input too short" }
+ when $_ %% 2 { return "even number of digits" }
+ }
+
+ $num.substr: ($num.chars/2).floor-1, 3
+}
+
+
diff --git a/challenge-135/colin-crain/raku/ch-2.raku b/challenge-135/colin-crain/raku/ch-2.raku
new file mode 100755
index 0000000000..6c332a6167
--- /dev/null
+++ b/challenge-135/colin-crain/raku/ch-2.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl6
+#
+#
+# 135-2-do-they-like-us-at-all.raku
+#
+#
+#
+# © 2021 colin crain
+## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ##
+
+
+
+use Test;
+
+is validate_sedol( "0263494" ), 1, 'ex-0263494';
+is validate_sedol( "02634940" ), 0, 'too long';
+is validate_sedol( "026349" ), 0, 'too short';
+is validate_sedol( "0263495" ), 0, 'wrong checksum';
+is validate_sedol( "B2634Z9" ), 1, 'alpha, starts with B';
+
+
+
+
+
+
+
+sub validate_sedol ( $code ) {
+
+ return 0 if $code ~~ m:i/ <[AEIOU]> | \W / or $code.chars != 7;
+
+ ## assign alphanumeric values
+ my %clookup = (|('0'..'9'), |('A'..'Z')).kv.reverse;
+
+ ## fixed SEDOL weight values
+ my @weights = 1, 3, 1, 7, 3, 9, 1;
+
+ my $ws = (0..5).map({ @weights[$_] * %clookup{ $code.substr($_, 1) } })
+ .sum;
+
+ my $cs_calculated = (10 - $ws % 10) % 10;
+ my $cs_digit = $code.substr: 6, 1;
+
+ return $cs_digit == $cs_calculated
+ ?? 1
+ !! 0
+}
+
+
+
+