diff options
| author | Walt Mankowski <waltman@pobox.com> | 2020-09-28 19:53:11 -0400 |
|---|---|---|
| committer | Walt Mankowski <waltman@pobox.com> | 2020-09-28 19:53:11 -0400 |
| commit | e7323444df339dabb8d0be4dfeaa493c826e4bac (patch) | |
| tree | 7a7cb3de4be4ff5368073903b77b75aee3c5502b | |
| parent | 78c4b3edb3294a781653d9ff67126b20add144a9 (diff) | |
| download | perlweeklychallenge-club-e7323444df339dabb8d0be4dfeaa493c826e4bac.tar.gz perlweeklychallenge-club-e7323444df339dabb8d0be4dfeaa493c826e4bac.tar.bz2 perlweeklychallenge-club-e7323444df339dabb8d0be4dfeaa493c826e4bac.zip | |
perl code for challenge 80 task 2
| -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; |
