aboutsummaryrefslogtreecommitdiff
path: root/challenge-096/tyler-wardhaugh/python/ch2.py
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-01-21 01:54:35 +0000
committerGitHub <noreply@github.com>2021-01-21 01:54:35 +0000
commit878d7ba2b2e95df8d0d59caa63019c1ac8cd198f (patch)
tree8335b89b59173681dc8ee0fe75c74fd79824861c /challenge-096/tyler-wardhaugh/python/ch2.py
parent56877338c2addcd41e2a8685b205f505963827b2 (diff)
parentd803b8295cb052657601c0f90392afac7b5fde47 (diff)
downloadperlweeklychallenge-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/ch2.py')
-rwxr-xr-xchallenge-096/tyler-wardhaugh/python/ch2.py43
1 files changed, 43 insertions, 0 deletions
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())