aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-13 16:28:04 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-13 16:28:04 +0000
commit8c1fdda84743187534ae4615a0dfcb41283293e4 (patch)
treece1caaa1016186bc4006fa7e26320bebc554a633
parent8d4cd3c68f9bf9ec693fedf996ac00bb499e22e4 (diff)
downloadperlweeklychallenge-club-8c1fdda84743187534ae4615a0dfcb41283293e4.tar.gz
perlweeklychallenge-club-8c1fdda84743187534ae4615a0dfcb41283293e4.tar.bz2
perlweeklychallenge-club-8c1fdda84743187534ae4615a0dfcb41283293e4.zip
Add Python solution to challenge 27
-rw-r--r--challenge-027/paulo-custodio/python/ch-1.py23
-rw-r--r--challenge-027/paulo-custodio/python/ch-2.py26
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-027/paulo-custodio/python/ch-1.py b/challenge-027/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..8f3e6c9731
--- /dev/null
+++ b/challenge-027/paulo-custodio/python/ch-1.py
@@ -0,0 +1,23 @@
+#!/usr/bin/python3
+
+# Challenge 027
+#
+# Task #1
+# Write a script to find the intersection of two straight lines. The
+# co-ordinates of the two lines should be provided as command line parameter.
+# For example:
+#
+# The two ends of Line 1 are represented as co-ordinates (a,b) and (c,d).
+#
+# The two ends of Line 2 are represented as co-ordinates (p,q) and (r,s).
+#
+# The script should print the co-ordinates of point of intersection of the
+# above two lines.
+
+import sys
+
+x1,y1,x2,y2,x3,y3,x4,y4 = [int(x) for x in sys.argv[1:9]]
+D = (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)
+x = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/D
+y = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/D
+print("{:.1f} {:.1f}".format(x, y))
diff --git a/challenge-027/paulo-custodio/python/ch-2.py b/challenge-027/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..99fd57554b
--- /dev/null
+++ b/challenge-027/paulo-custodio/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/python3
+
+# Challenge 027
+#
+# Task #2
+# Write a script that allows you to capture/display historical data. It could
+# be an object or a scalar. For example
+#
+# my $x = 10; $x = 20; $x -= 5;
+#
+# After the above operations, it should list $x historical value in order.
+
+class LoggingScalar:
+ def __init__(self, value):
+ self.values = [value]
+ def set(self, value):
+ self.values.append(value)
+ def get(self):
+ return self.values[-1]
+ def show_hist(self):
+ print(" ".join([str(x) for x in self.values]))
+
+x = LoggingScalar(10)
+x.set(20)
+x.set(x.get()-5)
+x.show_hist()