diff options
| author | Walt Mankowski <waltman@pobox.com> | 2020-08-14 20:13:19 -0400 |
|---|---|---|
| committer | Walt Mankowski <waltman@pobox.com> | 2020-08-14 20:13:19 -0400 |
| commit | fda1e0eca95c095c00dcfc5a25ab411d0dffa9ee (patch) | |
| tree | 9d161da4531c6ed651414eae332ed68d65fbe3dd /challenge-073 | |
| parent | 7ccb48f1b9b3bc07f9a63f335a57dc1756d7539c (diff) | |
| download | perlweeklychallenge-club-fda1e0eca95c095c00dcfc5a25ab411d0dffa9ee.tar.gz perlweeklychallenge-club-fda1e0eca95c095c00dcfc5a25ab411d0dffa9ee.tar.bz2 perlweeklychallenge-club-fda1e0eca95c095c00dcfc5a25ab411d0dffa9ee.zip | |
perl solution for challenge 73 task 2
First draft
Diffstat (limited to 'challenge-073')
| -rw-r--r-- | challenge-073/walt-mankowski/perl/ch-2.pl | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-073/walt-mankowski/perl/ch-2.pl b/challenge-073/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..44c7235dec --- /dev/null +++ b/challenge-073/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); +use List::Util qw(min); + +# 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. + +my @A = @ARGV; +my @output; +for my $i (0..$#A) { + if ($i == 0) { + push @output, 0; + } else { + my $min = min @A[0..$i-1]; + push @output, $min < $A[$i] ? $min : 0; + } +} + +say "@output"; |
