aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorandrezgz <andrezgz@gmail.com>2021-10-20 22:57:11 -0300
committerandrezgz <andrezgz@gmail.com>2021-10-20 22:57:11 -0300
commit0678f591fdd7f0b464ad62884e33e8cb4f43753e (patch)
tree3739644f750c04f6ee1e37bd0a872878a15cc541
parent20c6e9bc78a2bab9f8da2f0bda2fa204a6ffdb3c (diff)
downloadperlweeklychallenge-club-0678f591fdd7f0b464ad62884e33e8cb4f43753e.tar.gz
perlweeklychallenge-club-0678f591fdd7f0b464ad62884e33e8cb4f43753e.tar.bz2
perlweeklychallenge-club-0678f591fdd7f0b464ad62884e33e8cb4f43753e.zip
challenge-135 andrezgz solution
-rw-r--r--challenge-135/andrezgz/perl/ch-1.pl51
-rw-r--r--challenge-135/andrezgz/perl/ch-2.pl56
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