aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 16:25:33 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 16:25:33 +0100
commit4b61c9ed07a7c3faa5a5587c35c7b9164956d111 (patch)
treeea1a5a3a24712446426e6dde8d4c0b1139f273fe
parent802cc9f0ba0366c549bb649abaffe5c742553f85 (diff)
downloadperlweeklychallenge-club-4b61c9ed07a7c3faa5a5587c35c7b9164956d111.tar.gz
perlweeklychallenge-club-4b61c9ed07a7c3faa5a5587c35c7b9164956d111.tar.bz2
perlweeklychallenge-club-4b61c9ed07a7c3faa5a5587c35c7b9164956d111.zip
Add Python solution to challenge 152
-rw-r--r--challenge-152/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-152/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-152/paulo-custodio/python/ch-1.py50
-rw-r--r--challenge-152/paulo-custodio/python/ch-2.py44
4 files changed, 96 insertions, 2 deletions
diff --git a/challenge-152/paulo-custodio/perl/ch-1.pl b/challenge-152/paulo-custodio/perl/ch-1.pl
index 91d309f516..604c851859 100644
--- a/challenge-152/paulo-custodio/perl/ch-1.pl
+++ b/challenge-152/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 152
#
-# TASK #1 › Triangle Sum Path
+# TASK #1 > Triangle Sum Path
# Submitted by: Mohammad S Anwar
# You are given a triangle array.
#
diff --git a/challenge-152/paulo-custodio/perl/ch-2.pl b/challenge-152/paulo-custodio/perl/ch-2.pl
index 6a5b8bc0e8..9d2258ead8 100644
--- a/challenge-152/paulo-custodio/perl/ch-2.pl
+++ b/challenge-152/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 152
#
-# TASK #2 › Rectangle Area
+# TASK #2 > Rectangle Area
# Submitted by: Mohammad S Anwar
# You are given coordinates bottom-left and top-right corner of two rectangles
# in a 2D plane.
diff --git a/challenge-152/paulo-custodio/python/ch-1.py b/challenge-152/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..c454f06f43
--- /dev/null
+++ b/challenge-152/paulo-custodio/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+# Challenge 152
+#
+# TASK #1 > Triangle Sum Path
+# Submitted by: Mohammad S Anwar
+# You are given a triangle array.
+#
+# Write a script to find the minimum sum path from top to bottom.
+#
+# Example 1:
+# Input: $triangle = [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ]
+#
+# 1
+# 5 3
+# 2 3 4
+# 7 1 0 2
+# 6 4 5 2 8
+#
+# Output: 8
+#
+# Minimum Sum Path = 1 + 3 + 2 + 0 + 2 => 8
+# Example 2:
+# Input: $triangle = [ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ]
+#
+# 5
+# 2 3
+# 4 1 5
+# 0 1 2 3
+# 7 2 4 1 9
+#
+# Output: 9
+#
+# Minimum Sum Path = 5 + 2 + 1 + 0 + 1 => 9
+
+import fileinput
+
+def read_input():
+ lines = []
+ for line in fileinput.input():
+ cols = [int(x) for x in line.split()]
+ lines.append(cols)
+ return lines
+
+def min_sum_path(lines):
+ path = [min(x) for x in lines]
+ return sum(path)
+
+lines = read_input()
+print(min_sum_path(lines))
diff --git a/challenge-152/paulo-custodio/python/ch-2.py b/challenge-152/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..df732c6506
--- /dev/null
+++ b/challenge-152/paulo-custodio/python/ch-2.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+
+# Challenge 152
+#
+# TASK #2 > Rectangle Area
+# Submitted by: Mohammad S Anwar
+# You are given coordinates bottom-left and top-right corner of two rectangles
+# in a 2D plane.
+#
+# Write a script to find the total area covered by the two rectangles.
+#
+# Example 1:
+# Input: Rectangle 1 => (-1,0), (2,2)
+# Rectangle 2 => (0,-1), (4,4)
+#
+# Output: 22
+# Example 2:
+# Input: Rectangle 1 => (-3,-1), (1,3)
+# Rectangle 2 => (-1,-3), (2,2)
+#
+# Output: 25
+
+import sys
+
+x1, y1, x2, y2, x3, y3, x4, y4 = map(int, sys.argv[1:])
+
+# area of rectangles
+area12 = abs(x2-x1)*abs(y2-y1)
+area34 = abs(x4-x3)*abs(y4-y3)
+
+# intersection of rectangles
+x5 = max(x1, x3)
+y5 = max(y1, y3)
+x6 = min(x2, x4)
+y6 = min(y2, y4)
+
+area56 = abs(x6-x5)*abs(y6-y5)
+
+if x5 > x6 or y5 > y6: # no intersection
+ area56 = 0
+
+area = area12 + area34 - area56
+
+print(area)