diff options
Diffstat (limited to 'challenge-099/tyler-wardhaugh/python')
| -rw-r--r-- | challenge-099/tyler-wardhaugh/python/README.md | 6 | ||||
| -rwxr-xr-x | challenge-099/tyler-wardhaugh/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-099/tyler-wardhaugh/python/ch-2.py | 53 | ||||
| l--------- | challenge-099/tyler-wardhaugh/python/ch1.py | 1 | ||||
| l--------- | challenge-099/tyler-wardhaugh/python/ch2.py | 1 | ||||
| -rw-r--r-- | challenge-099/tyler-wardhaugh/python/requirements.txt | 1 | ||||
| -rwxr-xr-x | challenge-099/tyler-wardhaugh/python/test_ch1.py | 17 | ||||
| -rwxr-xr-x | challenge-099/tyler-wardhaugh/python/test_ch2.py | 15 |
8 files changed, 120 insertions, 3 deletions
diff --git a/challenge-099/tyler-wardhaugh/python/README.md b/challenge-099/tyler-wardhaugh/python/README.md index 976f770899..da9be25946 100644 --- a/challenge-099/tyler-wardhaugh/python/README.md +++ b/challenge-099/tyler-wardhaugh/python/README.md @@ -1,7 +1,7 @@ # The Weekly Challenge -The Weekly Challenge - #096 - Tyler Wardhaugh +The Weekly Challenge - #099 - Tyler Wardhaugh ## Usage @@ -10,11 +10,11 @@ Ensure requirements are satified (ideally in venv): Run Task 1: - $ ./ch1.py S + $ ./ch1.py S P Run Task 2: - $ ./ch2.py S1 S2 + $ ./ch2.py S T Run the project's tests (all the samples from the task descriptions plus some others): diff --git a/challenge-099/tyler-wardhaugh/python/ch-1.py b/challenge-099/tyler-wardhaugh/python/ch-1.py new file mode 100755 index 0000000000..99cab7c794 --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +"""Challenge 99, Task 1""" + +import sys +from fnmatch import fnmatch + + +DEFAULT_INPUT = ('abcde', 'a*e') + + +def pattern_match(string, pattern): + """Return true if pattern p matches string s, according to glob rules.""" + return fnmatch(string, pattern) + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + s, p = args[0:1] if args else DEFAULT_INPUT + if pattern_match(s, p): + print(1) + else: + print(0) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-099/tyler-wardhaugh/python/ch-2.py b/challenge-099/tyler-wardhaugh/python/ch-2.py new file mode 100755 index 0000000000..5bceca3c51 --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +"""Challenge 99, Task 2""" + +import sys +import numpy as np + + +DEFAULT_INPUT = ('littleit', 'lit') + + +def unique_subsequences(s, t): + """Use a dynamic programming algorithm to solve unique subsequences. + Source: https://www.geeksforgeeks.org/count-distinct-occurrences-as-a-subsequence/""" + + m = len(t) + n = len(s) + + if m > n: + return 0 + + # NumPy is overkill for this use case, especially since we're not taking + # advantage of any high-performance array manipulations, but I wanted to + # show its use. + dp = np.zeros((m + 1, n + 1), dtype=int) + dp[0, :] = np.ones(n + 1) + + with np.nditer(dp, flags=['multi_index'], order='C') as it: + while not it.finished: + i, j = it.multi_index + it.iternext() + + if i == 0 or j == 0: + continue + + if t[i - 1] != s[j - 1]: + dp[i][j] = dp[i][j - 1] + else: + dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1] + + return dp[m][n] + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + s, t = args[0:1] if args else DEFAULT_INPUT + print(unique_subsequences(s, t)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-099/tyler-wardhaugh/python/ch1.py b/challenge-099/tyler-wardhaugh/python/ch1.py new file mode 120000 index 0000000000..7680b02e4f --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/ch1.py @@ -0,0 +1 @@ +ch-1.py
\ No newline at end of file diff --git a/challenge-099/tyler-wardhaugh/python/ch2.py b/challenge-099/tyler-wardhaugh/python/ch2.py new file mode 120000 index 0000000000..13a132b99f --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/ch2.py @@ -0,0 +1 @@ +ch-2.py
\ No newline at end of file diff --git a/challenge-099/tyler-wardhaugh/python/requirements.txt b/challenge-099/tyler-wardhaugh/python/requirements.txt new file mode 100644 index 0000000000..a39ae54275 --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/requirements.txt @@ -0,0 +1 @@ +numpy==1.20.1 diff --git a/challenge-099/tyler-wardhaugh/python/test_ch1.py b/challenge-099/tyler-wardhaugh/python/test_ch1.py new file mode 100755 index 0000000000..94e18be7b3 --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/test_ch1.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import unittest +from ch1 import pattern_match + +class TestTask1(unittest.TestCase): + + + def test_example_cases(self): + self.assertTrue(pattern_match("abcde", "a*e")) + self.assertFalse(pattern_match("abcde", "a*d")) + self.assertFalse(pattern_match("abcde", "?b*d")) + self.assertTrue(pattern_match("abcde", "a*c?e")) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-099/tyler-wardhaugh/python/test_ch2.py b/challenge-099/tyler-wardhaugh/python/test_ch2.py new file mode 100755 index 0000000000..968a4fe8e4 --- /dev/null +++ b/challenge-099/tyler-wardhaugh/python/test_ch2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import unittest +from ch2 import unique_subsequences + +class TestTask2(unittest.TestCase): + + + def test_example_cases(self): + self.assertEqual(5, unique_subsequences('littleit', 'lit')) + self.assertEqual(3, unique_subsequences('london', 'lon')) + + +if __name__ == '__main__': + unittest.main() |
