diff options
| author | Cris-HD <59669732+Cris-HD@users.noreply.github.com> | 2020-04-17 19:19:31 +0200 |
|---|---|---|
| committer | Cris-HD <59669732+Cris-HD@users.noreply.github.com> | 2020-04-17 19:19:31 +0200 |
| commit | e6b676791a5c544df384ebf3f84a166f1535749a (patch) | |
| tree | c70eb571186e306ef1161455ddcc84767b156db7 | |
| parent | f1e41069c40c9b9821ffc212b465d096da9e0326 (diff) | |
| download | perlweeklychallenge-club-e6b676791a5c544df384ebf3f84a166f1535749a.tar.gz perlweeklychallenge-club-e6b676791a5c544df384ebf3f84a166f1535749a.tar.bz2 perlweeklychallenge-club-e6b676791a5c544df384ebf3f84a166f1535749a.zip | |
Create ch-1.pl
| -rw-r--r-- | challenge-056/cristian-heredia/perl/ch-1.pl | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/challenge-056/cristian-heredia/perl/ch-1.pl b/challenge-056/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..45efba040a --- /dev/null +++ b/challenge-056/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,123 @@ +use strict; +use warnings; +use Data::Dumper; + + +#Diff-K +#You are given an array @N of positive integers (sorted) and another non negative integer k. +#Write a script to find if there exists 2 indices i and j such that A[i] - A[j] = k and i != j. +#It should print the pairs of indices, if any such pairs exist. +# +#Example: +# @N = (2, 7, 9) +# $k = 2 +#Output : 2,1# + +my @array; +my $k; + +my @sortedArray; +my $length; +my $subtraction; +my $results = ''; +my $number; +my $continue = 'N'; + + +print "Please, provide a number for the array:\n"; +obtainNumber(); +requestArray(); + +sub requestArray { + + if ($number =~ /^\d+$/ ) { + if ($continue ne 'Y') { + push @array, $number; + } + print "Please provide another number\n"; + obtainNumber(); + + if ($number =~ /^\d+$/) { + push @array, $number; + print "Do you want to continue introducing more numbers? Yes (Y) or No (N)\n"; + $continue = <>; + $continue =~ s/^\s+|\s+$//g; + + if ($continue eq 'Y') { + requestArray(); + } + else { + requestK(); + } + } + else { + print "That's no correct, please provide another number.\n"; + obtainNumber(); + requestArray(); + } + } + else { + print "That's no correct, please provide another number.\n"; + obtainNumber(); + requestArray(); + } +} + +sub obtainNumber { + + $number = <>; + $number =~ s/^\s+|\s+$//g; +} + +sub requestK { + + print "Please provide a number for the comparison.\n"; + obtainNumber(); + if ($number =~ /^\d+$/) { + $k = $number; + sortArray(); + } + else { + print "That's no correct, please provide another number.\n"; + requestK(); + } +} + +sub sortArray { + + @sortedArray = sort { $a <=> $b } @array; + checkConditions(); +} + + +sub checkConditions { + + $length = @sortedArray; + foreach (my $i = 0; $i < $length; $i++) { + + foreach (my $j = 0; $j < $length; $j++) { + + if ($i != $j) { + + $subtraction = $sortedArray[$i] - $sortedArray[$j]; + + if ($subtraction == $k) { + + $results = $results."$i, $j\n"; + } + } + } + } + printMessage(); +} + + +sub printMessage { + + if ($results ne '') { + print "The pairs of indices that meet that conditions are:\n$results"; + } + else { + print "There is not any indices that meet that conditions.\n"; + } +} |
