diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-01-21 01:54:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-21 01:54:35 +0000 |
| commit | 878d7ba2b2e95df8d0d59caa63019c1ac8cd198f (patch) | |
| tree | 8335b89b59173681dc8ee0fe75c74fd79824861c /challenge-096/tyler-wardhaugh/python | |
| parent | 56877338c2addcd41e2a8685b205f505963827b2 (diff) | |
| parent | d803b8295cb052657601c0f90392afac7b5fde47 (diff) | |
| download | perlweeklychallenge-club-878d7ba2b2e95df8d0d59caa63019c1ac8cd198f.tar.gz perlweeklychallenge-club-878d7ba2b2e95df8d0d59caa63019c1ac8cd198f.tar.bz2 perlweeklychallenge-club-878d7ba2b2e95df8d0d59caa63019c1ac8cd198f.zip | |
Merge pull request #3328 from tylerw/tw/challenge-096
Challenge 096
Diffstat (limited to 'challenge-096/tyler-wardhaugh/python')
| -rw-r--r-- | challenge-096/tyler-wardhaugh/python/.gitignore | 24 | ||||
| -rw-r--r-- | challenge-096/tyler-wardhaugh/python/Makefile | 13 | ||||
| -rw-r--r-- | challenge-096/tyler-wardhaugh/python/README.md | 25 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/python/ch1.py | 25 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/python/ch2.py | 43 | ||||
| -rw-r--r-- | challenge-096/tyler-wardhaugh/python/requirements.txt | 1 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/python/test_ch1.py | 18 | ||||
| -rwxr-xr-x | challenge-096/tyler-wardhaugh/python/test_ch2.py | 15 |
8 files changed, 164 insertions, 0 deletions
diff --git a/challenge-096/tyler-wardhaugh/python/.gitignore b/challenge-096/tyler-wardhaugh/python/.gitignore new file mode 100644 index 0000000000..b1d9e2517e --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/.gitignore @@ -0,0 +1,24 @@ +### Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# IPython +profile_default/ +ipython_config.py + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +pythonenv* diff --git a/challenge-096/tyler-wardhaugh/python/Makefile b/challenge-096/tyler-wardhaugh/python/Makefile new file mode 100644 index 0000000000..bf7573b58a --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/Makefile @@ -0,0 +1,13 @@ +.PHONEY: help +help: + @grep -E '^[0-9a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \ + sort | \ + awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + +.PHONEY: test +test: ## run the test suite + @python -m unittest + +.PHONEY: satisfy-reqs +satisfy-reqs: ## ensure the requirements are installed + @python -m pip install -r requirements.txt diff --git a/challenge-096/tyler-wardhaugh/python/README.md b/challenge-096/tyler-wardhaugh/python/README.md new file mode 100644 index 0000000000..976f770899 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/README.md @@ -0,0 +1,25 @@ + +# The Weekly Challenge + +The Weekly Challenge - #096 - Tyler Wardhaugh + +## Usage + +Ensure requirements are satified (ideally in venv): + $ make satisfy-reqs + +Run Task 1: + + $ ./ch1.py S + +Run Task 2: + + $ ./ch2.py S1 S2 + +Run the project's tests (all the samples from the task descriptions plus some others): + + $ make test + +## Requirements: +* [Python 3](https://www.python.org/) +* [NumPy](https://numpy.org/) diff --git a/challenge-096/tyler-wardhaugh/python/ch1.py b/challenge-096/tyler-wardhaugh/python/ch1.py new file mode 100755 index 0000000000..37272382cb --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/ch1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +"""Challenge 96, Task 1""" + +import sys +import re + +DEFAULT_INPUT = "The Weekly Challenge" + +def reverse_words(s): + """Split a string s into words and reverse their order.""" + words = re.split(r'\s+', s.strip()) + return ' '.join(words[::-1]) + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + s = args[0] if args else DEFAULT_INPUT + print(reverse_words(s)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-096/tyler-wardhaugh/python/ch2.py b/challenge-096/tyler-wardhaugh/python/ch2.py new file mode 100755 index 0000000000..e55363e938 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/ch2.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""Challenge 96, Task 2""" + +import sys +import numpy as np + + +DEFAULT_INPUT = ["kitten", "sitting"] + + +def edit_distance(s1, s2): + """Determine the edit distance between two strings.""" + dp = np.zeros((len(s1), len(s2)), dtype=np.int) + for i, j in np.ndindex(dp.shape): + if i == 0: + dp[i][j] = j + elif j == 0: + dp[i][j] = i + elif s1[i - 1] == s2[j - 1]: + dp[i][j] = dp[i - 1][j - 1] + else: + dp[i][j] = 1 + min(dp[i][j - 1], + dp[i - 1][j], + dp[i - 1][j - 1]) + + return dp[-1][-1] + + +def main(args=None): + """Run the task""" + if args is None: + args = sys.argv[1:] + + if args: + s1, s2 = args[0:2] + else: + s1, s2 = DEFAULT_INPUT + + print(edit_distance(s1, s2)) + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/challenge-096/tyler-wardhaugh/python/requirements.txt b/challenge-096/tyler-wardhaugh/python/requirements.txt new file mode 100644 index 0000000000..1e99151eb7 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/requirements.txt @@ -0,0 +1 @@ +numpy==1.19.5 diff --git a/challenge-096/tyler-wardhaugh/python/test_ch1.py b/challenge-096/tyler-wardhaugh/python/test_ch1.py new file mode 100755 index 0000000000..5f27496224 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/test_ch1.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +import unittest +from ch1 import reverse_words + +class TestTask1(unittest.TestCase): + + + def test_example_cases(self): + self.assertEqual( + "Challenge Weekly The", reverse_words("The Weekly Challenge")) + self.assertEqual( + "family same the of part are Raku and Perl", + reverse_words(" Perl and Raku are part of the same family ")) + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-096/tyler-wardhaugh/python/test_ch2.py b/challenge-096/tyler-wardhaugh/python/test_ch2.py new file mode 100755 index 0000000000..4341cbded4 --- /dev/null +++ b/challenge-096/tyler-wardhaugh/python/test_ch2.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import unittest +from ch2 import edit_distance + +class TestTask2(unittest.TestCase): + + + def test_example_cases(self): + self.assertEqual(3, edit_distance("kitten", "sitting")) + self.assertEqual(2, edit_distance("sunday", "monday")) + + +if __name__ == '__main__': + unittest.main() |
