aboutsummaryrefslogtreecommitdiff
path: root/challenge-080
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-12-07 00:04:16 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-12-07 00:04:16 +0000
commit913c79f9179e9dc662d64dac73cc7c41e3b84ca7 (patch)
tree02f08e092ab0343a644fa2a13a4e800d8b281f69 /challenge-080
parent35e89da28fa5acf448dde4127c093031888e2fde (diff)
parent54620721cb174feca58d62f153c204d26b0297f7 (diff)
downloadperlweeklychallenge-club-913c79f9179e9dc662d64dac73cc7c41e3b84ca7.tar.gz
perlweeklychallenge-club-913c79f9179e9dc662d64dac73cc7c41e3b84ca7.tar.bz2
perlweeklychallenge-club-913c79f9179e9dc662d64dac73cc7c41e3b84ca7.zip
git isn't good at handling merges - worse than SVN by a long way
Diffstat (limited to 'challenge-080')
-rw-r--r--challenge-080/paulo-custodio/Makefile2
-rw-r--r--challenge-080/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-080/paulo-custodio/perl/ch-2.pl25
-rw-r--r--challenge-080/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-080/paulo-custodio/python/ch-2.py52
-rw-r--r--challenge-080/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-080/paulo-custodio/t/test-2.yaml10
-rw-r--r--challenge-080/paulo-custodio/test.pl23
8 files changed, 135 insertions, 31 deletions
diff --git a/challenge-080/paulo-custodio/Makefile b/challenge-080/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-080/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-080/paulo-custodio/perl/ch-1.pl b/challenge-080/paulo-custodio/perl/ch-1.pl
index d49f9ee1e0..088a0586e7 100644
--- a/challenge-080/paulo-custodio/perl/ch-1.pl
+++ b/challenge-080/paulo-custodio/perl/ch-1.pl
@@ -1,8 +1,8 @@
#!/usr/bin/perl
-# Challenge 081
+# Challenge 080
#
-# TASK #1 › Smallest Positive Number
+# TASK #1 > Smallest Positive Number
# Submitted by: Mohammad S Anwar
# You are given unsorted list of integers @N.
#
diff --git a/challenge-080/paulo-custodio/perl/ch-2.pl b/challenge-080/paulo-custodio/perl/ch-2.pl
index 4fea498307..5754b3ea8c 100644
--- a/challenge-080/paulo-custodio/perl/ch-2.pl
+++ b/challenge-080/paulo-custodio/perl/ch-2.pl
@@ -1,25 +1,38 @@
#!/usr/bin/perl
-# Challenge 081
+# Challenge 080
#
-# TASK #2 › Count Candies
+# 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:
+# 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.
+# 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.
+# 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.
+# 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
diff --git a/challenge-080/paulo-custodio/python/ch-1.py b/challenge-080/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..ab14f9f759
--- /dev/null
+++ b/challenge-080/paulo-custodio/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/python3
+
+# Challenge 080
+#
+# TASK #1 > Smallest Positive Number
+# Submitted by: Mohammad S Anwar
+# You are given unsorted list of integers @N.
+#
+# Write a script to find out the smallest positive number missing.
+#
+# Example 1:
+# Input: @N = (5, 2, -2, 0)
+# Output: 1
+# Example 2:
+# Input: @N = (1, 8, -1)
+# Output: 2
+# Example 3:
+# Input: @N = (2, 0, -1)
+# Output: 1
+
+import sys
+
+def missing(nums):
+ nums = sorted(filter(lambda x:x>0, nums))
+ for a,b in zip(nums, range(1, len(nums)+1)):
+ if a!=b:
+ return b
+ return len(nums)+1
+
+print(missing([int(x) for x in sys.argv[1:]]))
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:]])))
diff --git a/challenge-080/paulo-custodio/t/test-1.yaml b/challenge-080/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..dca395efd6
--- /dev/null
+++ b/challenge-080/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 5 2 -2 0
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 1 8 -1
+ input:
+ output: 2
+- setup:
+ cleanup:
+ args: 2 0 -1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 3 2 1 0 -1
+ input:
+ output: 4
diff --git a/challenge-080/paulo-custodio/t/test-2.yaml b/challenge-080/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..64927fb663
--- /dev/null
+++ b/challenge-080/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,10 @@
+- setup:
+ cleanup:
+ args: 1 2 2
+ input:
+ output: 4
+- setup:
+ cleanup:
+ args: 1 4 3 2
+ input:
+ output: 7
diff --git a/challenge-080/paulo-custodio/test.pl b/challenge-080/paulo-custodio/test.pl
deleted file mode 100644
index 116392bfa0..0000000000
--- a/challenge-080/paulo-custodio/test.pl
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl/ch-1.pl 5 2 -2 0"), "1\n";
-is capture("perl/ch-1.pl 1 8 -1"), "2\n";
-is capture("perl/ch-1.pl 2 0 -1"), "1\n";
-is capture("perl/ch-1.pl 3 2 1 0 -1"), "4\n";
-
-
-is capture("perl/ch-2.pl 1 2 2"), "4\n";
-is capture("perl/ch-2.pl 1 4 3 2"), "7\n";
-
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}