aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-24 22:33:16 +0100
committerGitHub <noreply@github.com>2021-10-24 22:33:16 +0100
commit3216bc8024c39552dfbee7771a162dfd95f9b0e1 (patch)
tree47897685e56cda4dce02c359c9b1c570c05ea3b4
parentf76399be01c5067e241b4ea177a06d96d0854798 (diff)
parent9c21dfe6a35e271f6f57858b9aeba13add1dbde6 (diff)
downloadperlweeklychallenge-club-3216bc8024c39552dfbee7771a162dfd95f9b0e1.tar.gz
perlweeklychallenge-club-3216bc8024c39552dfbee7771a162dfd95f9b0e1.tar.bz2
perlweeklychallenge-club-3216bc8024c39552dfbee7771a162dfd95f9b0e1.zip
Merge pull request #5090 from Cris-HD/branch-for-challenge-135_1
Added challenge 135 solution
-rwxr-xr-xchallenge-135/cristian-heredia/perl/ch_1.pl79
1 files changed, 79 insertions, 0 deletions
diff --git a/challenge-135/cristian-heredia/perl/ch_1.pl b/challenge-135/cristian-heredia/perl/ch_1.pl
new file mode 100755
index 0000000000..85999186eb
--- /dev/null
+++ b/challenge-135/cristian-heredia/perl/ch_1.pl
@@ -0,0 +1,79 @@
+=begin
+
+ 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
+
+=end
+=cut
+
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $input = '1234567';
+
+# Convert input into array
+my @array = split(//, abs($input));
+
+checkInputs();
+
+sub checkInputs {
+ if (@array == 3){
+ my $result = join( '', @array );
+ print("$result\n");
+ }
+ elsif(@array % 2 == 0){
+ print("Even number of digits\n");
+ }
+ elsif (@array > 3){
+ obtainDigits();
+ }
+ elsif (@array < 3){
+ print("too short\n");
+ }
+}
+
+sub obtainDigits{
+ # remove one element from the last of the array.
+ pop(@array);
+
+ # remove one element from the beginning of the array.
+ shift(@array);
+
+ checkInputs();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+