forked from gitgik/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] replace py file with notebook
- Loading branch information
Showing
2 changed files
with
147 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
{ | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.0-final" | ||
}, | ||
"orig_nbformat": 2, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.9.0 64-bit", | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | ||
} | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\"\"\"\n", | ||
"Write a function that takes in two strings and returns the minimal number of edit\n", | ||
"operations that need to be performed on the first string to get the second string.\n", | ||
"Edit Operations include: insertion, deletion and substitition.\n", | ||
"\n", | ||
"\n", | ||
"Sample input: str1: \"abc\", str2: \"yabd\"\n", | ||
"Sample output: 2\n", | ||
"Explanation: insert \"y\", substitute: \"c\" for \"d\"\n", | ||
"\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"def levenshtein_distance(str1, str2):\n", | ||
" \"\"\"\n", | ||
" Complexity:\n", | ||
" Time: O(nm)\n", | ||
" Space: O(nm), We can use dynamic programming to improve on the space\n", | ||
" complexity.\n", | ||
" \"\"\"\n", | ||
" edits = [[col for col in range(len(str2) + 1)] for row in range(len(str1) + 1)]\n", | ||
" # [\n", | ||
" # [0, 1, 2, 3, 4]\n", | ||
" # [ ... ]\n", | ||
" # ]\n", | ||
"\n", | ||
"\n", | ||
" for row in range(1, len(str1) + 1):\n", | ||
" # make the first value of each row to be 1, 2, 3, 4, ...\n", | ||
" # [ [0, 1, 2, ..],\n", | ||
" # [1, ...]\n", | ||
" # [2, ...]\n", | ||
" # [3, ...] ]\n", | ||
"\n", | ||
" edits[row][0] = edits[row - 1][0] + 1\n", | ||
"\n", | ||
" # iterate through the 2 dimensional array\n", | ||
" for row in range(1, len(str1) + 1):\n", | ||
" for col in range(1, len(str2) + 1):\n", | ||
" if str1[row - 1] == str2[col - 1]:\n", | ||
" edits[row][col] = edits[row - 1][col - 1]\n", | ||
" else:\n", | ||
" edits[row][col] = 1 + min(\n", | ||
" edits[row - 1][col],\n", | ||
" edits[row][col - 1],\n", | ||
" edits[row - 1][col - 1]\n", | ||
" )\n", | ||
"\n", | ||
" # return the bottom right value of the two-dim array\n", | ||
" return edits[-1][-1]\n", | ||
"\n", | ||
"\n", | ||
"def levenshtein_distance_efficient(str1, str2):\n", | ||
" \"\"\"\n", | ||
" Complexity:\n", | ||
" Time: O(nm) where n = length of str1, and m = length of str2\n", | ||
" Space: O(min(m, n))\n", | ||
" \"\"\"\n", | ||
" small = str1 if len(str1) < len(str2) else str2\n", | ||
" big = str1 if len(str1) >= len(str2) else str2\n", | ||
"\n", | ||
" even_edits = [x for x in range(len(small) + 1)]\n", | ||
" odd_edits = [None for x in range(len(small) + 1)]\n", | ||
"\n", | ||
" for i in range(len(big) + 1):\n", | ||
" if i % 2 == 1:\n", | ||
" # current edits are odd edits\n", | ||
" current_edits, previous_edits = odd_edits, even_edits\n", | ||
" else:\n", | ||
" # curent_edits are even edits\n", | ||
" current_edits, previous_edits = even_edits, odd_edits\n", | ||
"\n", | ||
" # for the first element, make it each equal to i\n", | ||
" current_edits[0] = i\n", | ||
"\n", | ||
" for j in range(1, len(small) + 1):\n", | ||
" if big[i - 1] == small[j - 1]:\n", | ||
" current_edits[j] = previous_edits[j - 1]\n", | ||
" else:\n", | ||
" current_edits[j] = 1 + min(\n", | ||
" previous_edits[j - 1], previous_edits[j], current_edits[j - 1]\n", | ||
" )\n", | ||
" return even_edits[-1] if len(big) % 2 == 0 else odd_edits[-1]\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"2" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 2 | ||
} | ||
], | ||
"source": [ | ||
"levenshtein_distance(\"abc\", \"yabd\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.