aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-02-18 10:44:06 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-02-18 10:48:34 -0800
commita65fdeb317c1eacf750954239f7d7b2ac678e825 (patch)
tree39c8d721af6acd26dc0a47006164ddc89aedcc1c
parentc07820d988ff3ea387cad72a2d46c3128e5abd88 (diff)
downloadperlweeklychallenge-club-a65fdeb317c1eacf750954239f7d7b2ac678e825.tar.gz
perlweeklychallenge-club-a65fdeb317c1eacf750954239f7d7b2ac678e825.tar.bz2
perlweeklychallenge-club-a65fdeb317c1eacf750954239f7d7b2ac678e825.zip
Ch100 (Python): Tasks 1 & 2
-rw-r--r--challenge-100/tyler-wardhaugh/python/README.md1
-rwxr-xr-xchallenge-100/tyler-wardhaugh/python/ch-1.py45
-rwxr-xr-xchallenge-100/tyler-wardhaugh/python/ch-2.py42
l---------challenge-100/tyler-wardhaugh/python/ch1.py1
l---------challenge-100/tyler-wardhaugh/python/ch2.py1
-rw-r--r--challenge-100/tyler-wardhaugh/python/requirements.txt0
-rwxr-xr-xchallenge-100/tyler-wardhaugh/python/test_ch1.py16
-rwxr-xr-xchallenge-100/tyler-wardhaugh/python/test_ch2.py15
8 files changed, 120 insertions, 1 deletions
diff --git a/challenge-100/tyler-wardhaugh/python/README.md b/challenge-100/tyler-wardhaugh/python/README.md
index 415b46eb8d..d540b43280 100644
--- a/challenge-100/tyler-wardhaugh/python/README.md
+++ b/challenge-100/tyler-wardhaugh/python/README.md
@@ -22,4 +22,3 @@ Run the project's tests (all the samples from the task descriptions plus some ot
## Requirements:
* [Python 3](https://www.python.org/)
-* [NumPy](https://numpy.org/)
diff --git a/challenge-100/tyler-wardhaugh/python/ch-1.py b/challenge-100/tyler-wardhaugh/python/ch-1.py
new file mode 100755
index 0000000000..1a7b6a8093
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python3
+"""Challenge 100, Task 1"""
+
+import re
+import sys
+from datetime import datetime
+
+
+DEFAULT_INPUT = '05:15pm'
+TIME_RE = re.compile(r"""
+ \A
+ \s*(?P<hhmm>\d{1,2}:\d{1,2})
+ \s*(?P<ampm>[ap]\.?m\.?)?
+ \s*
+ \Z
+ """, re.I | re.X)
+FMT = {
+ '12h': { 'parse': "%I:%M%p", 'out': '%H:%M' },
+ '24h': { 'parse': "%H:%M", 'out': "%I:%M%p" },
+ }
+
+
+def fun_time(timestr):
+ """Convert the given time from 12 hour format to 24 hour format and vice
+ versa."""
+
+ if (m := TIME_RE.match(timestr)):
+ mgroup = m.groupdict()
+ fmt = FMT['12h'] if mgroup['ampm'] else FMT['24h']
+ new_timestr = mgroup['hhmm'] + str(mgroup['ampm'] or '')
+ time = datetime.strptime(new_timestr, fmt['parse'])
+ return time.strftime(fmt['out'])
+
+
+def main(args=None):
+ """Run the task"""
+ if args is None:
+ args = sys.argv[1:]
+
+ timestr = args[0] if args else DEFAULT_INPUT
+ print(fun_time(timestr))
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/challenge-100/tyler-wardhaugh/python/ch-2.py b/challenge-100/tyler-wardhaugh/python/ch-2.py
new file mode 100755
index 0000000000..b143f7f4fe
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+"""Challenge 100, Task 2"""
+
+import sys
+from functools import reduce
+
+
+DEFAULT_INPUT = [ [1], [2,4], [6,4,9], [5,1,7,2] ]
+
+
+def min_row(row):
+ """For a row of length n, generate n-1 numbers representing the pair of
+ each element in row with its right neighbor."""
+ for x, y in zip(row, row[1:]):
+ yield min(x, y)
+
+
+def min_triangle_sum(triangle):
+ """Find the mininum path sum for the triangle from top to bottom."""
+ def f(acc, row):
+ return [x + y for x, y in zip(min_row(acc), row)]
+ ans = reduce(f, reversed(triangle))
+ return ans[0]
+
+
+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
+
+ print(min_triangle_sum(triangle))
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/challenge-100/tyler-wardhaugh/python/ch1.py b/challenge-100/tyler-wardhaugh/python/ch1.py
new file mode 120000
index 0000000000..7680b02e4f
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/ch1.py
@@ -0,0 +1 @@
+ch-1.py \ No newline at end of file
diff --git a/challenge-100/tyler-wardhaugh/python/ch2.py b/challenge-100/tyler-wardhaugh/python/ch2.py
new file mode 120000
index 0000000000..13a132b99f
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/ch2.py
@@ -0,0 +1 @@
+ch-2.py \ No newline at end of file
diff --git a/challenge-100/tyler-wardhaugh/python/requirements.txt b/challenge-100/tyler-wardhaugh/python/requirements.txt
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/requirements.txt
diff --git a/challenge-100/tyler-wardhaugh/python/test_ch1.py b/challenge-100/tyler-wardhaugh/python/test_ch1.py
new file mode 100755
index 0000000000..66669ba9f6
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/test_ch1.py
@@ -0,0 +1,16 @@
+#!/usr/bin/env python3
+
+import unittest
+from ch1 import fun_time
+
+class TestTask1(unittest.TestCase):
+
+
+ def test_example_cases(self):
+ self.assertEqual('17:15', fun_time('05:15 pm'))
+ self.assertEqual('17:15', fun_time('05:15pm'))
+ self.assertEqual('07:15PM', fun_time('19:15'))
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-100/tyler-wardhaugh/python/test_ch2.py b/challenge-100/tyler-wardhaugh/python/test_ch2.py
new file mode 100755
index 0000000000..4b3b13553c
--- /dev/null
+++ b/challenge-100/tyler-wardhaugh/python/test_ch2.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+
+import unittest
+from ch2 import min_triangle_sum
+
+class TestTask2(unittest.TestCase):
+
+
+ def test_example_cases(self):
+ self.assertEqual(8, min_triangle_sum([ [1], [2,4], [6,4,9], [5,1,7,2] ]))
+ self.assertEqual(7, min_triangle_sum([ [3], [3,1], [5,2,3], [4,3,1,3] ]))
+
+
+if __name__ == '__main__':
+ unittest.main()