diff options
Diffstat (limited to 'challenge-101')
| -rw-r--r-- | challenge-101/tyler-wardhaugh/python/README.md | 9 | ||||
| -rwxr-xr-x | challenge-101/tyler-wardhaugh/python/ch-1.py | 74 | ||||
| -rwxr-xr-x | challenge-101/tyler-wardhaugh/python/ch-2.py | 41 | ||||
| l--------- | challenge-101/tyler-wardhaugh/python/ch1.py | 1 | ||||
| l--------- | challenge-101/tyler-wardhaugh/python/ch2.py | 1 | ||||
| -rw-r--r-- | challenge-101/tyler-wardhaugh/python/requirements.txt | 0 | ||||
| -rwxr-xr-x | challenge-101/tyler-wardhaugh/python/test_ch1.py | 22 | ||||
| -rwxr-xr-x | challenge-101/tyler-wardhaugh/python/test_ch2.py | 19 |
8 files changed, 165 insertions, 2 deletions
diff --git a/challenge-101/tyler-wardhaugh/python/README.md b/challenge-101/tyler-wardhaugh/python/README.md index d540b43280..3b9306245e 100644 --- a/challenge-101/tyler-wardhaugh/python/README.md +++ b/challenge-101/tyler-wardhaugh/python/README.md @@ -10,11 +10,16 @@ Ensure requirements are satified (ideally in venv): Run Task 1: - $ ./ch1.py T + $ ./ch1.py COLL + # e.g.: + $ ./ch-1.py "[1, 2, 3, 4, 5, 6]" + Run Task 2: - $ ./ch2.py T + $ ./ch2.py POINTS + # e.g.: + $ ./ch2.py "[[0, 1], [2,0], [-6, 0]]" Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-101/tyler-wardhaugh/python/ch-1.py b/challenge-101/tyler-wardhaugh/python/ch-1.py new file mode 100755 index 0000000000..963f7ef319 --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/ch-1.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +"""Challenge 101, Task 1""" + +import sys + + +DEFAULT_INPUT = [1, 2, 3, 4] + + +def print_matrix(mat, colsep=' '): + """Pretty print a matrix (list of lists)""" + strs = [[str(x) for x in row] for row in mat] + lens = [max(len(x) for x in col) for col in zip(*strs)] + fmt = colsep.join(f'{{:{x}}}' for x in lens) + tbl = (fmt.format(*row) for row in strs) + + for row in tbl: + print(row) + + +def get_min_size(coll_len): + """Determine the minimum shape necessary for a matrix to hold len items.""" + f = lambda x: abs(x - coll_len // x) + candidates = {x: f(x) for x in range(1, 1+coll_len) if coll_len % x == 0} + + rows = min(candidates, key=candidates.get) + cols = coll_len // rows + return rows, cols + + +def rotate_matrix(mat): + """Rotate the matrix counterclockwise""" + rotated_mat = [list(x) for x in zip(*mat)] + rotated_mat.reverse() + return rotated_mat + + + +def _pack(coll, m, n): + """Recursively pack an incoming coll into an m x n spiral matrix. This is a + helper function for pack_spiral, not meant to be called directly.""" + if m == 1: + yield [coll] + elif n == 1: + yield from ([x] for x in reversed(coll)) + else: + result = rotate_matrix(list(_pack(coll[n:], n, m-1))) + [coll[:n]] + yield from result + + +def pack_spiral(coll): + """Return coll as a packed spiral matrix.""" + m, n = get_min_size(len(coll)) + spiral = list(_pack(coll, m, n)) + return spiral + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + coll = None + if args: + import json + coll = json.loads(args[0]) + else: + coll = DEFAULT_INPUT + + print_matrix(pack_spiral(coll)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-101/tyler-wardhaugh/python/ch-2.py b/challenge-101/tyler-wardhaugh/python/ch-2.py new file mode 100755 index 0000000000..2f38f93038 --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/ch-2.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +"""Challenge 101, Task 2""" + +import sys + + +DEFAULT_INPUT = [[0, 1], [1, 0], [2, 2]] + + +def contains_origin(triangle): + """Determine if the triangle formed by the given three points cover the (0,0) + origin on a 2D plane, using the Barycentric Coordinate Sytem method from + https://totologic.blogspot.com/2014/01/accurate-point-in-triangle-test.html""" + (x1, y1), (x2, y2), (x3, y3) = triangle + + denominator = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3) + A = ((y2 - y3) * (0 - x3) + (x3 - x2) * (0 - y3)) / denominator + B = ((y3 - y1)*(0 - x3) + (x1 - x3)*(0 - y3)) / denominator + C = 1 - A - B + + return all(0 <= x <= 1 for x in [A, B, C]) + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + triangle = None + if args: + import json + triangle = json.loads(args[0]) + else: + triangle = DEFAULT_INPUT + + result = 1 if contains_origin(triangle) else 0 + print(result) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-101/tyler-wardhaugh/python/ch1.py b/challenge-101/tyler-wardhaugh/python/ch1.py new file mode 120000 index 0000000000..7680b02e4f --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/ch1.py @@ -0,0 +1 @@ +ch-1.py
\ No newline at end of file diff --git a/challenge-101/tyler-wardhaugh/python/ch2.py b/challenge-101/tyler-wardhaugh/python/ch2.py new file mode 120000 index 0000000000..13a132b99f --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/ch2.py @@ -0,0 +1 @@ +ch-2.py
\ No newline at end of file diff --git a/challenge-101/tyler-wardhaugh/python/requirements.txt b/challenge-101/tyler-wardhaugh/python/requirements.txt new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/requirements.txt diff --git a/challenge-101/tyler-wardhaugh/python/test_ch1.py b/challenge-101/tyler-wardhaugh/python/test_ch1.py new file mode 100755 index 0000000000..9355f68556 --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/test_ch1.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +"""Test Task 1""" + +import unittest +from ch1 import pack_spiral + + +class TestTask1(unittest.TestCase): + """Test Task 1""" + + def test_example_cases(self): + """Test Task 1""" + self.assertEqual([[4, 3], [1, 2]], + pack_spiral(list(range(1, 5)))) + self.assertEqual([[6, 5, 4], [1, 2, 3]], + pack_spiral(list(range(1, 7)))) + self.assertEqual([[9, 8, 7, 6], [10, 11, 12, 5], [1, 2, 3, 4]], + pack_spiral(list(range(1, 13)))) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-101/tyler-wardhaugh/python/test_ch2.py b/challenge-101/tyler-wardhaugh/python/test_ch2.py new file mode 100755 index 0000000000..9cbce4b371 --- /dev/null +++ b/challenge-101/tyler-wardhaugh/python/test_ch2.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +"""Test Task 2""" + +import unittest +from ch2 import contains_origin + + +class TestTask2(unittest.TestCase): + """Test Task 2""" + + def test_example_cases(self): + """Test Task 2""" + self.assertFalse(contains_origin([[0, 1], [1, 0], [2, 2]])) + self.assertTrue(contains_origin([[1, 1], [-1, 1], [0, -3]])) + self.assertTrue(contains_origin([[0, 1], [2, 0], [-6, 0]])) + + +if __name__ == '__main__': + unittest.main() |
