diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-18 13:47:11 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-04-18 13:47:11 +0100 |
| commit | 5caf667e0cdf85a295b96c60ccbd7801ce56a0a8 (patch) | |
| tree | c6d86f532e7385e5a75996edef71708bb8f6f8bd | |
| parent | d17bc69ff5642794e884f3918770ef17645315aa (diff) | |
| parent | 13927c8d833c5df5a9460ff71b4b6eaa9a9bbedb (diff) | |
| download | perlweeklychallenge-club-5caf667e0cdf85a295b96c60ccbd7801ce56a0a8.tar.gz perlweeklychallenge-club-5caf667e0cdf85a295b96c60ccbd7801ce56a0a8.tar.bz2 perlweeklychallenge-club-5caf667e0cdf85a295b96c60ccbd7801ce56a0a8.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -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"; + } +} |
