diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-16 17:25:31 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-16 17:25:31 +0100 |
| commit | cc053ff8fa0d8b64f8ab260139dbb78a8b664fbf (patch) | |
| tree | 493572ef1b79769b5374127fb76888a083366f31 /challenge-073 | |
| parent | 76a978eeffd22189ee71238295637ab2cca69380 (diff) | |
| download | perlweeklychallenge-club-cc053ff8fa0d8b64f8ab260139dbb78a8b664fbf.tar.gz perlweeklychallenge-club-cc053ff8fa0d8b64f8ab260139dbb78a8b664fbf.tar.bz2 perlweeklychallenge-club-cc053ff8fa0d8b64f8ab260139dbb78a8b664fbf.zip | |
- Added solutions by Cristina Heredia.
Diffstat (limited to 'challenge-073')
| -rw-r--r-- | challenge-073/cristian-heredia/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-073/cristian-heredia/perl/ch-2.pl | 68 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-073/cristian-heredia/perl/ch-1.pl b/challenge-073/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..e2df4d45b3 --- /dev/null +++ b/challenge-073/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,62 @@ +#TASK #1 › Min Sliding Window +#Submitted by: Mohammad S Anwar +#You are given an array of integers @A and sliding window size $S. +# +#Write a script to create an array of min from each sliding window. +# +#Example +#Input: @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8) and $S = 3 +#Output: (0, 0, 0, 2, 3, 3, 4, 4) +# +#[(1 5 0) 2 9 3 7 6 4 8] = Min (0) +#[1 (5 0 2) 9 3 7 6 4 8] = Min (0) +#[1 5 (0 2 9) 3 7 6 4 8] = Min (0) +#[1 5 0 (2 9 3) 7 6 4 8] = Min (2) +#[1 5 0 2 (9 3 7) 6 4 8] = Min (3) +#[1 5 0 2 9 (3 7 6) 4 8] = Min (3) +#[1 5 0 2 9 3 (7 6 4) 8] = Min (4) +#[1 5 0 2 9 3 7 (6 4 8)] = Min (4) + +use strict; +use warnings; +use Data::Dumper; +use List::Util qw(min); + +#Input +my @A = (1, 5, 0, 2, 9, 3, 7, 6, 4, 8); +my $S = 3; #SlidingWindow + +#variables +my @window; +my @outout; +my $counter = 0; +my $next = 0; +my $lenght = @A; +my $max = $lenght - $S + 1; + +slidingWindow(); + +sub slidingWindow { + + if ($next != $max) { + + if ($counter == $S ) { + $counter = 0; + $next++; + $S++; + @window = (); + slidingWindow(); + } + else { + foreach (my $i = $next; $i<$S; $i++) { + push @window, $A[$i]; + } + $counter = $S; + push @outout, min(@window); + slidingWindow(); + } + } + else { + print Dumper \@outout; + } +} diff --git a/challenge-073/cristian-heredia/perl/ch-2.pl b/challenge-073/cristian-heredia/perl/ch-2.pl new file mode 100644 index 0000000000..18bae5e6ff --- /dev/null +++ b/challenge-073/cristian-heredia/perl/ch-2.pl @@ -0,0 +1,68 @@ +#TASK #2 › Smallest Neighbour +#Submitted by: Mohammad S Anwar +#You are given an array of integers @A. +# +#Write a script to create an array that represents the smallest element to the left of each corresponding index. If none found then use 0. +# +#Example 1 +#Input: @A = (7, 8, 3, 12, 10) +#Output: (0, 7, 0, 3, 3) +# +#For index 0, the smallest number to the left of $A[0] i.e. 7 is none, so we put 0. +#For index 1, the smallest number to the left of $A[1] as compare to 8, in (7) is 7 so we put 7. +#For index 2, the smallest number to the left of $A[2] as compare to 3, in (7, 8) is none, so we put 0. +#For index 3, the smallest number to the left of $A[3] as compare to 12, in (7, 8, 3) is 3, so we put 3. +#For index 4, the smallest number to the left of $A[4] as compare to 10, in (7, 8, 3, 12) is 3, so we put 3 again. +# +#Example 2 +#Input: @A = (4, 6, 5) +#Output: (0, 4, 4) +# +#For index 0, the smallest number to the left of $A[0] is none, so we put 0. +#For index 1, the smallest number to the left of $A[1] as compare to 6, in (4) is 4, so we put 4. +#For index 2, the smallest number to the left of $A[2] as compare to 5, in (4, 6) is 4, so we put 4 again. + + +use strict; +use warnings; +use Data::Dumper; +use List::Util qw(min); + +#Input +my @A = (7, 8, 3, 12, 10); + +#variables +my $lenght = @A; +my @result; +my @checkMin; +my $minNumber; + + +checkArray(); + +sub checkArray { + + foreach (my $i=0; $i<$lenght;$i++) { + + if ($i == 0) { + + push @result, 0; + } + else { + @checkMin = (); + + foreach (my $j=0;$j<$i;$j++){ + push @checkMin, $A[$j]; + } + $minNumber = min(@checkMin); + if ($minNumber < $A[$i]) { + push @result, $minNumber; + } + else { + push @result, 0; + } + } + + } + print Dumper \@result; +} |
