aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-10-03 19:00:56 +0100
committerGitHub <noreply@github.com>2020-10-03 19:00:56 +0100
commit5a369e347499a262bc4c79ce6d5ce383472f96e2 (patch)
tree75c79b641836748f749b41c9d9508ac7d99e380f
parentbf8d529f63a5bf6201db9f1cfc2a1d94059c2e82 (diff)
parent5b181c706d7d2c50ffb716adb3df8d6e78ae0925 (diff)
downloadperlweeklychallenge-club-5a369e347499a262bc4c79ce6d5ce383472f96e2.tar.gz
perlweeklychallenge-club-5a369e347499a262bc4c79ce6d5ce383472f96e2.tar.bz2
perlweeklychallenge-club-5a369e347499a262bc4c79ce6d5ce383472f96e2.zip
Merge pull request #2437 from oWnOIzRi/week80
add solution for week 80 task 2
-rw-r--r--challenge-080/steven-wilson/perl/ch-2.pl89
1 files changed, 89 insertions, 0 deletions
diff --git a/challenge-080/steven-wilson/perl/ch-2.pl b/challenge-080/steven-wilson/perl/ch-2.pl
new file mode 100644
index 0000000000..f20356fe2f
--- /dev/null
+++ b/challenge-080/steven-wilson/perl/ch-2.pl
@@ -0,0 +1,89 @@
+#!/usr/bin/env perl
+# Solution by Steven Wilson 3rd Oct 2020.
+
+# 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
+# mmediate neighbors on either side.
+# Example 1:
+#
+# Input: @N = (1, 2, 2)
+#
+# Explanation:
+#
+# Applying rule #a, each candidate will get one candy. So total
+# candies needed so far 3. Now applying rule #b, the first candidate
+# do not get any more candy as its rank is lower than it's
+# neighbours. The second candidate gets one more candy as it's
+# ranking is higher than it's neighbour. Finally the third candidate
+# do not get any extra candy as it's ranking is not higher than
+# neighbour. Therefore total candies required is 4.
+#
+# Output: 4
+#
+# Example 2:
+#
+# Input: @N = (1, 4, 3, 2)
+#
+# Explanation:
+#
+# Applying rule #a, each candidate will get one candy. So total
+# candies needed so far 4. Now applying rule #b, the first candidate
+# do not get any more candy as its rank is lower than it's
+# neighbours. The second candidate gets two more candies as it's
+# ranking is higher than it's both neighbour. The third candidate
+# gets one more candy as it's ranking is higher than it's neighbour.
+# Finally the fourth candidate do not get any extra candy as it's
+# ranking is not higher than neighbour. Therefore total candies
+# required is 7.
+#
+# Output: 7
+#
+
+use strict;
+use warnings;
+use feature qw/ say /;
+use Test::More;
+use List::Util qw/ sum /;
+
+my @N1_t = ( 1, 2, 2 );
+my @N2_t = ( 1, 4, 3, 2 );
+my @N3_t = ( 1, 1, 2, 3 );
+my @N4_t = ( 3, 1, 2, 1 );
+ok( get_total_candies( \@N1_t ) == 4, "example 1" );
+ok( get_total_candies( \@N2_t ) == 7, "example 2" );
+ok( get_total_candies( \@N3_t ) == 7, "last ranking higher" );
+ok( get_total_candies( \@N4_t ) == 6, "first ranking higher" );
+done_testing();
+
+sub get_total_candies {
+ my $rankings_ref = shift;
+ my @rankings = @{$rankings_ref};
+ my @candies = map { 1 } 1 .. scalar @rankings;
+ my $candies_changed = 1;
+ while ($candies_changed) {
+ $candies_changed = 0;
+ for my $i ( 0 .. ( scalar @rankings - 2 ) ) {
+ if ( $rankings[$i] > $rankings[ $i + 1 ]
+ && !( $candies[$i] > $candies[ $i + 1 ] ) )
+ {
+ $candies[$i]++;
+ $candies_changed = 1;
+ }
+ elsif ( $rankings[$i] < $rankings[ $i + 1 ]
+ && !( $candies[$i] < $candies[ $i + 1 ] ) )
+ {
+ $candies[ $i + 1 ]++;
+ $candies_changed = 1;
+ }
+ }
+ }
+ return sum @candies;
+}
+