diff options
| author | Cris-HD <crisn7@hotmail.com> | 2021-10-24 20:15:00 +0200 |
|---|---|---|
| committer | Cris-HD <crisn7@hotmail.com> | 2021-10-24 20:15:00 +0200 |
| commit | 9c21dfe6a35e271f6f57858b9aeba13add1dbde6 (patch) | |
| tree | 14a1909dc41af3ce5640a6ce208c65046da0d29a | |
| parent | 484c41107a3c73d28fb2df5c23d2ba8d8a16a5fc (diff) | |
| download | perlweeklychallenge-club-9c21dfe6a35e271f6f57858b9aeba13add1dbde6.tar.gz perlweeklychallenge-club-9c21dfe6a35e271f6f57858b9aeba13add1dbde6.tar.bz2 perlweeklychallenge-club-9c21dfe6a35e271f6f57858b9aeba13add1dbde6.zip | |
Added challenge 135 solution
| -rwxr-xr-x | challenge-135/cristian-heredia/perl/ch_1.pl | 79 |
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();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
