aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-02 11:13:16 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-02 11:13:16 +0000
commit40245eebd22668a2ff92aee1c7e0f62fe750536f (patch)
treebc69d382e2bf8c241fd1f6e9724d0736ed442c79 /challenge-080/paulo-custodio/python/ch-2.py
parent9b5f294fd7feb46c546f0fa2b51ff4d12eb8ffe5 (diff)
downloadperlweeklychallenge-club-40245eebd22668a2ff92aee1c7e0f62fe750536f.tar.gz
perlweeklychallenge-club-40245eebd22668a2ff92aee1c7e0f62fe750536f.tar.bz2
perlweeklychallenge-club-40245eebd22668a2ff92aee1c7e0f62fe750536f.zip
Add Python solution to challenge 80
Diffstat (limited to 'challenge-080/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-080/paulo-custodio/python/ch-2.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-080/paulo-custodio/python/ch-2.py b/challenge-080/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..f877f5aa47
--- /dev/null
+++ b/challenge-080/paulo-custodio/python/ch-2.py
@@ -0,0 +1,52 @@
+#!/usr/bin/python3
+
+# Challenge 080
+#
+# 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
+
+import sys
+
+def candies(n):
+ candy = [1 for x in n]
+ for i in range(len(n)):
+ if i>0:
+ if n[i]>n[i-1]:
+ candy[i] += 1
+ if i<len(n)-1:
+ if n[i]>n[i+1]:
+ candy[i] += 1
+ return candy
+
+print(sum(candies([int(x) for x in sys.argv[1:]])))