Skip to content

Commit 2d4e155

Browse files
committed
[chore] replace py file with notebook
1 parent 65d0096 commit 2d4e155

File tree

2 files changed

+147
-82
lines changed

2 files changed

+147
-82
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.9.0-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3.9.0 64-bit",
19+
"metadata": {
20+
"interpreter": {
21+
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
22+
}
23+
}
24+
}
25+
},
26+
"nbformat": 4,
27+
"nbformat_minor": 2,
28+
"cells": [
29+
{
30+
"cell_type": "code",
31+
"execution_count": 1,
32+
"metadata": {},
33+
"outputs": [],
34+
"source": [
35+
"\"\"\"\n",
36+
"Write a function that takes in two strings and returns the minimal number of edit\n",
37+
"operations that need to be performed on the first string to get the second string.\n",
38+
"Edit Operations include: insertion, deletion and substitition.\n",
39+
"\n",
40+
"\n",
41+
"Sample input: str1: \"abc\", str2: \"yabd\"\n",
42+
"Sample output: 2\n",
43+
"Explanation: insert \"y\", substitute: \"c\" for \"d\"\n",
44+
"\n",
45+
"\"\"\"\n",
46+
"\n",
47+
"def levenshtein_distance(str1, str2):\n",
48+
" \"\"\"\n",
49+
" Complexity:\n",
50+
" Time: O(nm)\n",
51+
" Space: O(nm), We can use dynamic programming to improve on the space\n",
52+
" complexity.\n",
53+
" \"\"\"\n",
54+
" edits = [[col for col in range(len(str2) + 1)] for row in range(len(str1) + 1)]\n",
55+
" # [\n",
56+
" # [0, 1, 2, 3, 4]\n",
57+
" # [ ... ]\n",
58+
" # ]\n",
59+
"\n",
60+
"\n",
61+
" for row in range(1, len(str1) + 1):\n",
62+
" # make the first value of each row to be 1, 2, 3, 4, ...\n",
63+
" # [ [0, 1, 2, ..],\n",
64+
" # [1, ...]\n",
65+
" # [2, ...]\n",
66+
" # [3, ...] ]\n",
67+
"\n",
68+
" edits[row][0] = edits[row - 1][0] + 1\n",
69+
"\n",
70+
" # iterate through the 2 dimensional array\n",
71+
" for row in range(1, len(str1) + 1):\n",
72+
" for col in range(1, len(str2) + 1):\n",
73+
" if str1[row - 1] == str2[col - 1]:\n",
74+
" edits[row][col] = edits[row - 1][col - 1]\n",
75+
" else:\n",
76+
" edits[row][col] = 1 + min(\n",
77+
" edits[row - 1][col],\n",
78+
" edits[row][col - 1],\n",
79+
" edits[row - 1][col - 1]\n",
80+
" )\n",
81+
"\n",
82+
" # return the bottom right value of the two-dim array\n",
83+
" return edits[-1][-1]\n",
84+
"\n",
85+
"\n",
86+
"def levenshtein_distance_efficient(str1, str2):\n",
87+
" \"\"\"\n",
88+
" Complexity:\n",
89+
" Time: O(nm) where n = length of str1, and m = length of str2\n",
90+
" Space: O(min(m, n))\n",
91+
" \"\"\"\n",
92+
" small = str1 if len(str1) < len(str2) else str2\n",
93+
" big = str1 if len(str1) >= len(str2) else str2\n",
94+
"\n",
95+
" even_edits = [x for x in range(len(small) + 1)]\n",
96+
" odd_edits = [None for x in range(len(small) + 1)]\n",
97+
"\n",
98+
" for i in range(len(big) + 1):\n",
99+
" if i % 2 == 1:\n",
100+
" # current edits are odd edits\n",
101+
" current_edits, previous_edits = odd_edits, even_edits\n",
102+
" else:\n",
103+
" # curent_edits are even edits\n",
104+
" current_edits, previous_edits = even_edits, odd_edits\n",
105+
"\n",
106+
" # for the first element, make it each equal to i\n",
107+
" current_edits[0] = i\n",
108+
"\n",
109+
" for j in range(1, len(small) + 1):\n",
110+
" if big[i - 1] == small[j - 1]:\n",
111+
" current_edits[j] = previous_edits[j - 1]\n",
112+
" else:\n",
113+
" current_edits[j] = 1 + min(\n",
114+
" previous_edits[j - 1], previous_edits[j], current_edits[j - 1]\n",
115+
" )\n",
116+
" return even_edits[-1] if len(big) % 2 == 0 else odd_edits[-1]\n"
117+
]
118+
},
119+
{
120+
"cell_type": "code",
121+
"execution_count": 2,
122+
"metadata": {},
123+
"outputs": [
124+
{
125+
"output_type": "execute_result",
126+
"data": {
127+
"text/plain": [
128+
"2"
129+
]
130+
},
131+
"metadata": {},
132+
"execution_count": 2
133+
}
134+
],
135+
"source": [
136+
"levenshtein_distance(\"abc\", \"yabd\")"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {},
143+
"outputs": [],
144+
"source": []
145+
}
146+
]
147+
}

dynamic-programming/levenshtein_distance.py

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)