aboutsummaryrefslogtreecommitdiff
path: root/challenge-096/tyler-wardhaugh/python
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-01-20 10:05:39 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2021-01-20 13:21:03 -0800
commite788f447e46760cc68c99a605e1d0b0820184304 (patch)
treed6da51e83c615812aace0d9333474cce382eb649 /challenge-096/tyler-wardhaugh/python
parentcfb51189b52bda76f0536d7674e7247b15b9f09b (diff)
downloadperlweeklychallenge-club-e788f447e46760cc68c99a605e1d0b0820184304.tar.gz
perlweeklychallenge-club-e788f447e46760cc68c99a605e1d0b0820184304.tar.bz2
perlweeklychallenge-club-e788f447e46760cc68c99a605e1d0b0820184304.zip
Ch96 (Python): Tasks 1 & 2
Diffstat (limited to 'challenge-096/tyler-wardhaugh/python')
-rw-r--r--challenge-096/tyler-wardhaugh/python/.gitignore24
-rw-r--r--challenge-096/tyler-wardhaugh/python/Makefile13
-rw-r--r--challenge-096/tyler-wardhaugh/python/README.md25
-rwxr-xr-xchallenge-096/tyler-wardhaugh/python/ch1.py25
-rwxr-xr-xchallenge-096/tyler-wardhaugh/python/ch2.py43
-rw-r--r--challenge-096/tyler-wardhaugh/python/requirements.txt1
-rwxr-xr-xchallenge-096/tyler-wardhaugh/python/test_ch1.py18
-rwxr-xr-xchallenge-096/tyler-wardhaugh/python/test_ch2.py15
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()