diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-10-21 18:16:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-21 18:16:21 +0100 |
| commit | 092a57869b58ce12462bb7ab7836203855f4cf2b (patch) | |
| tree | e5e046c63790713d463192db0fe0e088e9021291 | |
| parent | ea15542fd1b191457a9c1c4fc5cec88dbf983c17 (diff) | |
| parent | 0678f591fdd7f0b464ad62884e33e8cb4f43753e (diff) | |
| download | perlweeklychallenge-club-092a57869b58ce12462bb7ab7836203855f4cf2b.tar.gz perlweeklychallenge-club-092a57869b58ce12462bb7ab7836203855f4cf2b.tar.bz2 perlweeklychallenge-club-092a57869b58ce12462bb7ab7836203855f4cf2b.zip | |
Merge pull request #5067 from andrezgz/challenge-0135
challenge-135 andrezgz solution
| -rw-r--r-- | challenge-135/andrezgz/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-135/andrezgz/perl/ch-2.pl | 56 |
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-135/andrezgz/perl/ch-1.pl b/challenge-135/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..fb49d14186 --- /dev/null +++ b/challenge-135/andrezgz/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-135/ +# 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 + +use strict; +use warnings; +use feature 'say'; + +my $n = shift; +die "not an integer\n" if !$n || $n !~ /^-?\d+$/; + +$n *= -1 if $n < 0; +my $l = length $n; + +die "even number of digits\n" if $l % 2 == 0; +die "too short\n" if $l < 3; + +my $m = int($l/2) - 1; +say substr $n, $m, 3; + +__END__ + +./ch-1.pl 1234567 +345 + +./ch-1.pl -123 +123 + +./ch-1.pl 1 +too short + +./ch-1.pl 10 +even number of digits diff --git a/challenge-135/andrezgz/perl/ch-2.pl b/challenge-135/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..fd72cb47e8 --- /dev/null +++ b/challenge-135/andrezgz/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-135/ +# 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 the wikipedia page (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 + +use strict; +use warnings; +use feature 'say'; + +my $sedol = shift; +say is_valid($sedol); +exit 0; + +sub is_valid { + my $sedol = shift; + + return 0 if !$sedol || $sedol !~ /^([0-9B-DF-HJ-NP-TV-Z]{6})([0-9])$/; + my $code = $1; + my $check = $2; + + my $sum = 0; + for (0 .. 5) { + my $v = substr $code, $_, 1; # value for numbers + $v = ord($v) - 55 if $v !~ /\d/; # value for letters (alphabet position) + $sum += $v * (1,3,1,7,3,9)[$_]; # weighted sum of values + } + + return $check == (10 - $sum % 10) % 10 ? 1 : 0; +} + +__END__ + +./ch-2.pl 2936921 +1 + +./ch-2.pl 1234567 +0 + +./ch-2.pl B0YBKL9 +1 |
