diff options
| -rw-r--r-- | challenge-080/walt-mankowski/perl/ch-2.pl | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-080/walt-mankowski/perl/ch-2.pl b/challenge-080/walt-mankowski/perl/ch-2.pl new file mode 100644 index 0000000000..d81887113b --- /dev/null +++ b/challenge-080/walt-mankowski/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(:5.32); +use experimental qw(signatures); + +# TASK #2 › Count Candies +# Submitted by: Mohammad S Anwar +# +# You are given rankings of @N candidates. +# +# Write a script to find out the total candies needed for all +# candidates. You are asked to follow the rules below: +# +# a) You must given at least one candy to each candidate. +# +# b) Candidate with higher ranking get more candies than their +# immediate neighbors on either side. + +my @n = @ARGV; +my $candy = @n; + +# right neighbors +for my $i (0..$#n-1) { + $candy++ if $n[$i] > $n[$i+1]; +} + +# left neighbors +for my $i (1..$#n) { + $candy++ if $n[$i] > $n[$i-1]; +} + +say $candy; |
