From 4c134411d883e1eb4010e0252854dc4b11a7d1a3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Mon, 18 Oct 2021 14:54:29 +0100 Subject: Add Perl and Python solutions to challenge 135 --- challenge-135/paulo-custodio/perl/ch-1.pl | 34 ++++++++++++++++++++ challenge-135/paulo-custodio/perl/ch-2.pl | 50 +++++++++++++++++++++++++++++ challenge-135/paulo-custodio/python/ch-1.py | 35 ++++++++++++++++++++ challenge-135/paulo-custodio/python/ch-2.py | 43 +++++++++++++++++++++++++ challenge-135/paulo-custodio/t/test-1.yaml | 25 +++++++++++++++ challenge-135/paulo-custodio/t/test-2.yaml | 15 +++++++++ challenge-135/paulo-custodio/test.pl | 4 +++ 7 files changed, 206 insertions(+) create mode 100644 challenge-135/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-135/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-135/paulo-custodio/python/ch-1.py create mode 100644 challenge-135/paulo-custodio/python/ch-2.py create mode 100644 challenge-135/paulo-custodio/t/test-1.yaml create mode 100644 challenge-135/paulo-custodio/t/test-2.yaml create mode 100644 challenge-135/paulo-custodio/test.pl diff --git a/challenge-135/paulo-custodio/perl/ch-1.pl b/challenge-135/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..ebb2200c2c --- /dev/null +++ b/challenge-135/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +# TASK #1 > 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 + +use Modern::Perl; + +my $n = abs(shift||0); +my $len = length($n); +if ($len%2==0) { + say "even number of digits"; +} +elsif ($len<3) { + say "too short"; +} +else { + say substr($n, ($len-3)/2, 3); +} diff --git a/challenge-135/paulo-custodio/perl/ch-2.pl b/challenge-135/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..aad313eac5 --- /dev/null +++ b/challenge-135/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +# TASK #2 > 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 + +use Modern::Perl; + +my $SEDOL = shift||""; +say check_sedol($SEDOL); + +sub check_sedol { + my($str) = @_; + return 0 unless $str =~ /^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$/; + my $input = substr($str, 0, 6); + my $check_digit = compute_check_digit($input); + if ($input.$check_digit eq $str) { + return 1; + } + else { + return 0; + } +} + +sub compute_check_digit { + my($input) = @_; + my @weight = (1, 3, 1, 7, 3, 9); + my @input = map {$_ ge 'A' ? ord($_)-ord('A')+10 : ord($_)-ord('0')} + split //, $input; + my $sum = 0; + for my $i (0..$#weight) { + $sum += $input[$i] * $weight[$i]; + } + return (10-$sum%10); +} diff --git a/challenge-135/paulo-custodio/python/ch-1.py b/challenge-135/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..389816b534 --- /dev/null +++ b/challenge-135/paulo-custodio/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# TASK #1 > 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 + +import sys + +def mid3digits(n): + l = len(str(n)) + if l%2==0: + return "even number of digits" + elif l<3: + return "too short" + else: + s = int((l-3)/2) + return str(n)[s:s+3]; + +n = abs(int(str(sys.argv[1]))) +print(mid3digits(n)) diff --git a/challenge-135/paulo-custodio/python/ch-2.py b/challenge-135/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9f1bb2594a --- /dev/null +++ b/challenge-135/paulo-custodio/python/ch-2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# TASK #2 > 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 + +import sys +import re + +def check_sedol(sedol): + def compute_check_digit(input_str): + weight = [1, 3, 1, 7, 3, 9] + input = [int(c, 36) for c in input_str] + sum = 0 + for i in range(0, 6): + sum += input[i] * weight[i] + return str(10-sum%10) + + if not re.match(r"^[0-9BCDFGHJKLMNPQRSTVWXYZ]{6}[0-9]$", sedol): + return 0 + input = sedol[0:6] + check_digit = compute_check_digit(input) + if input+check_digit==sedol: + return 1 + else: + return 0 + +print(check_sedol(sys.argv[1])) diff --git a/challenge-135/paulo-custodio/t/test-1.yaml b/challenge-135/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..eaf5d3a3a6 --- /dev/null +++ b/challenge-135/paulo-custodio/t/test-1.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 1234567 + input: + output: 345 +- setup: + cleanup: + args: -123 + input: + output: 123 +- setup: + cleanup: + args: 1 + input: + output: too short +- setup: + cleanup: + args: 10 + input: + output: even number of digits +- setup: + cleanup: + args: 1234 + input: + output: even number of digits diff --git a/challenge-135/paulo-custodio/t/test-2.yaml b/challenge-135/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..644605a018 --- /dev/null +++ b/challenge-135/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 2936921 + input: + output: 1 +- setup: + cleanup: + args: 1234567 + input: + output: 0 +- setup: + cleanup: + args: B0YBKL9 + input: + output: 1 diff --git a/challenge-135/paulo-custodio/test.pl b/challenge-135/paulo-custodio/test.pl new file mode 100644 index 0000000000..ba6c37260b --- /dev/null +++ b/challenge-135/paulo-custodio/test.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl +use Modern::Perl; +use Test::More; +require '../../challenge-001/paulo-custodio/test.pl'; -- cgit