aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-27 20:06:59 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-27 20:06:59 +0000
commit541a825c91802e0a5bb3bff382b410731aa534f4 (patch)
treeb346c5c73594d48c10d2fea3a8b6ba6e359f7657 /challenge-101/paulo-custodio/python
parent2a8d81c5c54d8df1ecdac0e0e069ff8a1ad35a42 (diff)
downloadperlweeklychallenge-club-541a825c91802e0a5bb3bff382b410731aa534f4.tar.gz
perlweeklychallenge-club-541a825c91802e0a5bb3bff382b410731aa534f4.tar.bz2
perlweeklychallenge-club-541a825c91802e0a5bb3bff382b410731aa534f4.zip
Add Python solution to challenge 101
Diffstat (limited to 'challenge-101/paulo-custodio/python')
-rw-r--r--challenge-101/paulo-custodio/python/ch-1.py93
-rw-r--r--challenge-101/paulo-custodio/python/ch-2.py32
2 files changed, 125 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/python/ch-1.py b/challenge-101/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..ff71f11953
--- /dev/null
+++ b/challenge-101/paulo-custodio/python/ch-1.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+
+# You are given an array @A of items (integers say, but they can be anything).
+#
+# Your task is to pack that array into an MxN matrix spirally counterclockwise,
+# as tightly as possible.
+#
+# 'Tightly' means the absolute value |M-N| of the difference has to be as small
+# as possible.
+
+import sys
+import math
+
+def spiral(numbers):
+
+ # find max width of elements, convert to int
+ def max_width(numbers):
+ num_width = 1
+ for i in range(0, len(numbers)):
+ if len(numbers[i]) < num_width:
+ num_width = len(numbers[i])
+ numbers[i] = int(numbers[i])
+ return num_width
+
+ # find out m,n
+ def smallest_rect(n):
+ l, h = 1, n
+ for i in range(1, int(math.sqrt(n)+1)):
+ if ((n % i) == 0):
+ l, h = i, int(n / i)
+ return l, h
+
+ # build empty rectangle
+ rect = []
+ def build_rect(numbers):
+ m, n = smallest_rect(len(numbers))
+ for r in range (0, m+1):
+ rect.append(["" for c in range(0, n+1)])
+ return m, n
+
+ # build spiral rectangle
+ num_width = max_width(numbers)
+ m, n = build_rect(numbers)
+
+ r, c = m, 1
+ i = 0
+ while (i < len(numbers)):
+ # go East
+ while (c <= n):
+ if (rect[r][c] != ""):
+ break
+ rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i])
+ i += 1
+ c += 1
+ c -= 1
+ r -= 1
+ # go North
+ while (r >= 1):
+ if (rect[r][c] != ""):
+ break
+ rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i])
+ i += 1
+ r -= 1
+ r += 1
+ c -= 1
+ # go West
+ while (c >= 1):
+ if (rect[r][c] != ""):
+ break
+ rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i])
+ i += 1
+ c -= 1
+ c += 1
+ r += 1
+ # go South
+ while (r <= m):
+ if (rect[r][c] != ""):
+ break
+ rect[r][c] = ("{:"+str(num_width+1)+"d}").format(numbers[i])
+ i += 1
+ r += 1
+ r -= 1
+ c += 1
+
+ for r in range (1, m+1):
+ output = ""
+ for c in range (1, n+1):
+ output += rect[r][c]
+ print(output)
+
+# main
+numbers = sys.argv[1:]
+spiral(numbers)
diff --git a/challenge-101/paulo-custodio/python/ch-2.py b/challenge-101/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..1ea5f422f7
--- /dev/null
+++ b/challenge-101/paulo-custodio/python/ch-2.py
@@ -0,0 +1,32 @@
+#! /usr/bin/env python
+
+# TASK #2 > Origin-containing Triangle
+# Submitted by: Stuart Little
+# You are given three points in the plane, as a list of six co-ordinates:
+# A=(x1,y1), B=(x2,y2) and C=(x3,y3).
+#
+# Write a script to find out if the triangle formed by the given three
+# co-ordinates contain origin (0,0).
+#
+# Print 1 if found otherwise 0.
+
+import sys
+
+def point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3):
+ def sign(x1,y1,x2,y2,x3,y3):
+ return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)
+
+ d1 = sign(xp,yp, x1,y1, x2,y2)
+ d2 = sign(xp,yp, x2,y2, x3,y3)
+ d3 = sign(xp,yp, x3,y3, x1,y1)
+
+ has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
+ has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
+
+ if (not (has_neg and has_pos)):
+ return 1
+ else:
+ return 0
+
+x1,y1,x2,y2,x3,y3 = (float(sys.argv[i]) for i in range(1, 7))
+print(point_in_triangle(0.0,0.0, x1,y1,x2,y2,x3,y3))