aboutsummaryrefslogtreecommitdiff
path: root/challenge-078
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-09 11:22:11 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-09 11:22:11 +0000
commit4174c1724112a2200f3c5d001e9419fdffddafc8 (patch)
treee33df748c800f4b366deb58e10e08137db8df111 /challenge-078
parent519d44c0a7356bc03840549e421446656f7f2813 (diff)
downloadperlweeklychallenge-club-4174c1724112a2200f3c5d001e9419fdffddafc8.tar.gz
perlweeklychallenge-club-4174c1724112a2200f3c5d001e9419fdffddafc8.tar.bz2
perlweeklychallenge-club-4174c1724112a2200f3c5d001e9419fdffddafc8.zip
Add Python solution to challenge 78
Diffstat (limited to 'challenge-078')
-rw-r--r--challenge-078/paulo-custodio/Makefile2
-rw-r--r--challenge-078/paulo-custodio/perl/ch-1.pl8
-rw-r--r--challenge-078/paulo-custodio/perl/ch-2.pl26
-rw-r--r--challenge-078/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-078/paulo-custodio/python/ch-2.py60
-rw-r--r--challenge-078/paulo-custodio/t/test-1.yaml20
-rw-r--r--challenge-078/paulo-custodio/t/test-2.yaml25
-rw-r--r--challenge-078/paulo-custodio/test.pl37
8 files changed, 165 insertions, 49 deletions
diff --git a/challenge-078/paulo-custodio/Makefile b/challenge-078/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-078/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-078/paulo-custodio/perl/ch-1.pl b/challenge-078/paulo-custodio/perl/ch-1.pl
index 673378b489..847a64de60 100644
--- a/challenge-078/paulo-custodio/perl/ch-1.pl
+++ b/challenge-078/paulo-custodio/perl/ch-1.pl
@@ -2,13 +2,15 @@
# Challenge 078
#
-# TASK #1 › Leader Element
+# TASK #1 > Leader Element
# Submitted by: Mohammad S Anwar
# You are given an array @A containing distinct integers.
#
-# Write a script to find all leader elements in the array @A. Print (0) if none found.
+# Write a script to find all leader elements in the array @A. Print (0) if
+# none found.
#
-# An element is leader if it is greater than all the elements to its right side.
+# An element is leader if it is greater than all the elements to its right
+# side.
#
# Example 1:
# Input: @A = (9, 10, 7, 5, 6, 1)
diff --git a/challenge-078/paulo-custodio/perl/ch-2.pl b/challenge-078/paulo-custodio/perl/ch-2.pl
index 5762c9a6c1..468c70dd20 100644
--- a/challenge-078/paulo-custodio/perl/ch-2.pl
+++ b/challenge-078/paulo-custodio/perl/ch-2.pl
@@ -2,22 +2,27 @@
# Challenge 078
#
-# TASK #2 › Left Rotation
+# TASK #2 > Left Rotation
# Submitted by: Mohammad S Anwar
-# You are given array @A containing positive numbers and @B containing one or more indices from the array @A.
+# You are given array @A containing positive numbers and @B containing one or
+# more indices from the array @A.
#
-# Write a script to left rotate @A so that the number at the first index of @B becomes the first element in the array.
-# Similarly, left rotate @A again so that the number at the second index of @B becomes the first element in the array.
+# Write a script to left rotate @A so that the number at the first index of @B
+# becomes the first element in the array.
+# Similarly, left rotate @A again so that the number at the second index of @B
+# becomes the first element in the array.
#
# Example 1:
# Input:
# @A = (10 20 30 40 50)
# @B = (3 4)
# Explanation:
-# a) We left rotate the 3rd index element (40) in the @A to make it 0th index member in the array.
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th index
+# member in the array.
# [40 50 10 20 30]
#
-# b) We left rotate the 4th index element (50) in the @A to make it 0th index member in the array.
+# b) We left rotate the 4th index element (50) in the @A to make it 0th index
+# member in the array.
# [50 10 20 30 40]
#
# Output:
@@ -28,13 +33,16 @@
# @A = (7 4 2 6 3)
# @B = (1 3 4)
# Explanation:
-# a) We left rotate the 1st index element (4) in the @A to make it 0th index member in the array.
+# a) We left rotate the 1st index element (4) in the @A to make it 0th index
+# member in the array.
# [4 2 6 3 7]
#
-# b) We left rotate the 3rd index element (6) in the @A to make it 0th index member in the array.
+# b) We left rotate the 3rd index element (6) in the @A to make it 0th index
+# member in the array.
# [6 3 7 4 2]
#
-# c) We left rotate the 4th index element (3) in the @A to make it 0th index member in the array.
+# c) We left rotate the 4th index element (3) in the @A to make it 0th index
+# member in the array.
# [3 7 4 2 6]
#
# Output:
diff --git a/challenge-078/paulo-custodio/python/ch-1.py b/challenge-078/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..5d29728911
--- /dev/null
+++ b/challenge-078/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/python3
+
+# Challenge 078
+#
+# TASK #1 > Leader Element
+# Submitted by: Mohammad S Anwar
+# You are given an array @A containing distinct integers.
+#
+# Write a script to find all leader elements in the array @A. Print (0) if
+# none found.
+#
+# An element is leader if it is greater than all the elements to its right
+# side.
+#
+# Example 1:
+# Input: @A = (9, 10, 7, 5, 6, 1)
+# Output: (10, 7, 6, 1)
+# Example 2:
+# Input: @A = (3, 4, 5)
+# Output: (5)
+
+import sys
+
+def find_leaders(a):
+ leaders = []
+ cur_max = 0
+ for i in range(len(a))[::-1]:
+ if a[i] > cur_max:
+ leaders.insert(0, a[i])
+ cur_max = a[i]
+ if len(leaders)==0:
+ leaders = [0]
+ return leaders
+
+leaders = find_leaders([int(x) for x in sys.argv[1:]])
+print("("+", ".join([str(x) for x in leaders])+")")
diff --git a/challenge-078/paulo-custodio/python/ch-2.py b/challenge-078/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..c980b09aeb
--- /dev/null
+++ b/challenge-078/paulo-custodio/python/ch-2.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+
+# Challenge 078
+#
+# TASK #2 > Left Rotation
+# Submitted by: Mohammad S Anwar
+# You are given array @A containing positive numbers and @B containing one or
+# more indices from the array @A.
+#
+# Write a script to left rotate @A so that the number at the first index of @B
+# becomes the first element in the array.
+# Similarly, left rotate @A again so that the number at the second index of @B
+# becomes the first element in the array.
+#
+# Example 1:
+# Input:
+# @A = (10 20 30 40 50)
+# @B = (3 4)
+# Explanation:
+# a) We left rotate the 3rd index element (40) in the @A to make it 0th index
+# member in the array.
+# [40 50 10 20 30]
+#
+# b) We left rotate the 4th index element (50) in the @A to make it 0th index
+# member in the array.
+# [50 10 20 30 40]
+#
+# Output:
+# [40 50 10 20 30]
+# [50 10 20 30 40]
+# Example 2:
+# Input:
+# @A = (7 4 2 6 3)
+# @B = (1 3 4)
+# Explanation:
+# a) We left rotate the 1st index element (4) in the @A to make it 0th index
+# member in the array.
+# [4 2 6 3 7]
+#
+# b) We left rotate the 3rd index element (6) in the @A to make it 0th index
+# member in the array.
+# [6 3 7 4 2]
+#
+# c) We left rotate the 4th index element (3) in the @A to make it 0th index
+# member in the array.
+# [3 7 4 2 6]
+#
+# Output:
+# [4 2 6 3 7]
+# [6 3 7 4 2]
+# [3 7 4 2 6]
+
+import sys
+
+A = [int(x) for x in sys.argv[1].split()]
+B = [int(x) for x in sys.argv[2].split()]
+
+for i in B:
+ a = [*A[i:], *A[:i]]
+ print("["+" ".join([str(x) for x in a])+"]")
diff --git a/challenge-078/paulo-custodio/t/test-1.yaml b/challenge-078/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..9e3bfb0499
--- /dev/null
+++ b/challenge-078/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,20 @@
+- setup:
+ cleanup:
+ args: 9 10 7 5 6 1
+ input:
+ output: (10, 7, 6, 1)
+- setup:
+ cleanup:
+ args: 3 4 5
+ input:
+ output: (5)
+- setup:
+ cleanup:
+ args:
+ input:
+ output: (0)
+- setup:
+ cleanup:
+ args: 0 0 0 0 0
+ input:
+ output: (0)
diff --git a/challenge-078/paulo-custodio/t/test-2.yaml b/challenge-078/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..6e2ca00c51
--- /dev/null
+++ b/challenge-078/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: "'10 20 30 40 50' '3 4'"
+ input:
+ output: |
+ [40 50 10 20 30]
+ [50 10 20 30 40]
+- setup:
+ cleanup:
+ args: "'7 4 2 6 3' '1 3 4'"
+ input:
+ output: |
+ [4 2 6 3 7]
+ [6 3 7 4 2]
+ [3 7 4 2 6]
+- setup:
+ cleanup:
+ args: "'7 4 2 6 3' '0 1 2 3 4'"
+ input:
+ output: |
+ [7 4 2 6 3]
+ [4 2 6 3 7]
+ [2 6 3 7 4]
+ [6 3 7 4 2]
+ [3 7 4 2 6]
diff --git a/challenge-078/paulo-custodio/test.pl b/challenge-078/paulo-custodio/test.pl
deleted file mode 100644
index 6b2dda4547..0000000000
--- a/challenge-078/paulo-custodio/test.pl
+++ /dev/null
@@ -1,37 +0,0 @@
-#!/usr/bin/perl
-
-use Modern::Perl;
-use Test::More;
-
-is capture("perl perl/ch-1.pl 9 10 7 5 6 1"), "(10, 7, 6, 1)\n";
-is capture("perl perl/ch-1.pl 3 4 5"), "(5)\n";
-is capture("perl perl/ch-1.pl "), "(0)\n";
-is capture("perl perl/ch-1.pl 0 0 0 0 0"), "(0)\n";
-
-is capture("perl perl/ch-2.pl '10 20 30 40 50' '3 4'"), <<END;
-[40 50 10 20 30]
-[50 10 20 30 40]
-END
-
-is capture("perl perl/ch-2.pl '7 4 2 6 3' '1 3 4'"), <<END;
-[4 2 6 3 7]
-[6 3 7 4 2]
-[3 7 4 2 6]
-END
-
-is capture("perl perl/ch-2.pl '7 4 2 6 3' '0 1 2 3 4'"), <<END;
-[7 4 2 6 3]
-[4 2 6 3 7]
-[2 6 3 7 4]
-[6 3 7 4 2]
-[3 7 4 2 6]
-END
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \t\v\f\r]*\n/\n/g;
- return $out;
-}