update folder structure
parent
b84dd0a187
commit
603f184c61
|
@ -1,6 +1,7 @@
|
||||||
# Python
|
# Python
|
||||||
|
|
||||||
## Arrays
|
## Arrays
|
||||||
|
|
||||||
- [Count Inversions](arrays/counting_inversions.py)
|
- [Count Inversions](arrays/counting_inversions.py)
|
||||||
- [Majority Element](arrays/majority_element.py)
|
- [Majority Element](arrays/majority_element.py)
|
||||||
- [Rotate Array](arrays/rotate_array.py)
|
- [Rotate Array](arrays/rotate_array.py)
|
||||||
|
@ -10,31 +11,36 @@
|
||||||
- [Max Sub Array Sum](arrays/max_sub_array_sum.py)
|
- [Max Sub Array Sum](arrays/max_sub_array_sum.py)
|
||||||
|
|
||||||
## Linked Lists
|
## Linked Lists
|
||||||
|
|
||||||
- [Doubly](linked_lists/doubly.py)
|
- [Doubly](linked_lists/doubly.py)
|
||||||
- [Singly](linked_lists/singly.py)
|
- [Singly](linked_lists/singly.py)
|
||||||
- [Reverse List](linked_lists/reverse-linkedlist.py)
|
- [Reverse List](linked_lists/reverse-linkedlist.py)
|
||||||
- [Middle Node](linked_lists/middle-node-linkedlist.py)
|
- [Middle Node](linked_lists/middle-node-linkedlist.py)
|
||||||
- [Cycle Detection and Removal](linked_lists/cycle-detection-and-removal-linkedlist.py)
|
- [Cycle Detection and Removal](linked_lists/cycle-detection-and-removal-linkedlist.py)
|
||||||
|
|
||||||
|
|
||||||
## Dictionaries
|
## Dictionaries
|
||||||
|
|
||||||
- [Two Sum](dictionaries/two-sum.py)
|
- [Two Sum](dictionaries/two-sum.py)
|
||||||
|
|
||||||
## Multiplication
|
## Multiplication
|
||||||
|
|
||||||
- [Karatsuba](multiplication/karatsuba_algorithm.py)
|
- [Karatsuba](multiplication/karatsuba_algorithm.py)
|
||||||
|
|
||||||
## Recursion
|
## Recursion
|
||||||
|
|
||||||
- [Factorial](recursion/factorial.py)
|
- [Factorial](recursion/factorial.py)
|
||||||
- [n-th Fibonacci number](recursion/nth_fibonacci_number.py)
|
- [n-th Fibonacci number](recursion/nth_fibonacci_number.py)
|
||||||
- [Recursive Insertion Sort](recursion/recursive_insertion_sort.py)
|
- [Recursive Insertion Sort](recursion/recursive_insertion_sort.py)
|
||||||
- [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py)
|
- [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py)
|
||||||
- [GCD by Euclid's Algorithm](recursion/gcd_using_recursion.py)
|
- [GCD by Euclid's Algorithm](recursion/gcd_using_recursion.py)
|
||||||
|
- [Levenshtein distance](recursion/levenshtein_distance.py)
|
||||||
|
|
||||||
## Scheduling
|
## Scheduling
|
||||||
|
|
||||||
- [Interval Scheduling](scheduling/interval_scheduling.py)
|
- [Interval Scheduling](scheduling/interval_scheduling.py)
|
||||||
|
|
||||||
## Searching
|
## Searching
|
||||||
|
|
||||||
- [Binary Search](searching/binary_search.py)
|
- [Binary Search](searching/binary_search.py)
|
||||||
- [Jump Search](searching/jump_search.py)
|
- [Jump Search](searching/jump_search.py)
|
||||||
- [Linear Search](searching/linear_search.py)
|
- [Linear Search](searching/linear_search.py)
|
||||||
|
@ -44,6 +50,7 @@
|
||||||
- [Breath First Search](searching/breadth-first-search.py)
|
- [Breath First Search](searching/breadth-first-search.py)
|
||||||
|
|
||||||
## Sorting
|
## Sorting
|
||||||
|
|
||||||
- [Bubble Sort](sorting/bubble_sort.py)
|
- [Bubble Sort](sorting/bubble_sort.py)
|
||||||
- [Comb Sort](sorting/comb_sort.py)
|
- [Comb Sort](sorting/comb_sort.py)
|
||||||
- [Count Sort](sorting/count-sort.py)
|
- [Count Sort](sorting/count-sort.py)
|
||||||
|
@ -56,6 +63,7 @@
|
||||||
- [Merge sort](sorting/merge_sort.py)
|
- [Merge sort](sorting/merge_sort.py)
|
||||||
|
|
||||||
## Strings
|
## Strings
|
||||||
|
|
||||||
- [Is Good Str](strings/is_good_str.py)
|
- [Is Good Str](strings/is_good_str.py)
|
||||||
- [Palindrome](strings/palindrome.py)
|
- [Palindrome](strings/palindrome.py)
|
||||||
- [Word Count](strings/word_count.py)
|
- [Word Count](strings/word_count.py)
|
||||||
|
@ -69,6 +77,7 @@
|
||||||
- [Roman to Int](strings/roman-to-int.py)
|
- [Roman to Int](strings/roman-to-int.py)
|
||||||
|
|
||||||
## Dynamic Programming
|
## Dynamic Programming
|
||||||
|
|
||||||
- [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
- [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
||||||
- [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py)
|
- [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py)
|
||||||
- [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
- [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
||||||
|
@ -77,16 +86,20 @@
|
||||||
- [Levenshtein distance](dynamic_programming/levenshtein_distance.py)
|
- [Levenshtein distance](dynamic_programming/levenshtein_distance.py)
|
||||||
|
|
||||||
## Graphs
|
## Graphs
|
||||||
|
|
||||||
- [Simple Graph](graphs/graph.py)
|
- [Simple Graph](graphs/graph.py)
|
||||||
- [BFS SEQUENCE](graphs/bfs-sequence.py)
|
- [BFS SEQUENCE](graphs/bfs-sequence.py)
|
||||||
- [Depth First Search](graphs/depth-first-search.py)
|
- [Depth First Search](graphs/depth-first-search.py)
|
||||||
|
|
||||||
## Trees
|
## Trees
|
||||||
|
|
||||||
- [Binary Tree](trees/binary_tree.py)
|
- [Binary Tree](trees/binary_tree.py)
|
||||||
- [Binary Search Tree](trees/binary_search_tree.py)
|
- [Binary Search Tree](trees/binary_search_tree.py)
|
||||||
|
|
||||||
## Queues
|
## Queues
|
||||||
|
|
||||||
- [First in First out Queue](queues/fifo-queue.py)
|
- [First in First out Queue](queues/fifo-queue.py)
|
||||||
|
|
||||||
## Number Theory
|
## Number Theory
|
||||||
|
|
||||||
- [Prime Number Checker](number_theory/prime_number.py)
|
- [Prime Number Checker](number_theory/prime_number.py)
|
||||||
|
|
|
@ -1,41 +1,27 @@
|
||||||
# The Levenshtein distance (Edit distance) Problem
|
|
||||||
|
|
||||||
# Informally, the Levenshtein distance between two words is
|
def edit_distance(w1, w2):
|
||||||
# the minimum number of single-character edits (insertions, deletions or substitutions)
|
"""
|
||||||
# required to change one word into the other.
|
Find edit distance (minimum number of changes) required to convert string w1 to string w2.
|
||||||
|
"""
|
||||||
|
|
||||||
# For example, the Levenshtein distance between kitten and sitting is 3.
|
m = [[0 if x != 0 else y if y != 0 else x for x in range(len(w2) + 1)] for y in range(len(w1) + 1)]
|
||||||
# The minimal edit script that transforms the former into the latter is:
|
|
||||||
|
|
||||||
# kitten —> sitten (substitution of s for k)
|
for i in range(1,len(w1) + 1):
|
||||||
# sitten —> sittin (substitution of i for e)
|
m[i][0] = i
|
||||||
# sittin —> sitting (insertion of g at the end)
|
for j in range(1, len(w2) + 1):
|
||||||
|
m[0][j] = j
|
||||||
|
|
||||||
def levenshtein_distance(word_1, chars_1, word_2, chars_2):
|
for i in range(1, len(w1) + 1):
|
||||||
# base case if the strings are empty
|
for j in range(1, len(w2) + 1):
|
||||||
if chars_1 == 0:
|
m[i][j] = min(
|
||||||
return chars_2
|
m[i-1][j-1] + int(w1[i-1] != w2[j-1]),
|
||||||
if chars_2 == 0:
|
m[i-1][j] + 1,
|
||||||
return chars_1
|
m[i][j-1] + 1
|
||||||
|
)
|
||||||
|
|
||||||
# if last characters of the string match, the cost of
|
return m
|
||||||
# operations is 0, i.e. no changes are made
|
|
||||||
if word_1[chars_1 - 1] == word_2[chars_2 - 1]:
|
|
||||||
cost = 0
|
|
||||||
else:
|
|
||||||
cost = 1
|
|
||||||
|
|
||||||
# calculating the numbers of operations recursively
|
|
||||||
deletion = levenshtein_distance(word_1, chars_1 - 1, word_2, chars_2) + 1
|
|
||||||
insertion = levenshtein_distance(word_1, chars_1, word_2, chars_2 - 1) + 1
|
|
||||||
substitution = levenshtein_distance(word_1, chars_1 - 1, word_2, chars_2 - 1) + cost
|
|
||||||
|
|
||||||
return min(deletion, insertion, substitution)
|
if __name__ == "__main__":
|
||||||
|
print(edit_distance("sitting", "kitten"))
|
||||||
|
|
||||||
# driving script
|
|
||||||
if __name__ == '__main__':
|
|
||||||
word_1 = 'plain'
|
|
||||||
word_2 = 'plane'
|
|
||||||
|
|
||||||
print('The Levenshtein distance is:')
|
|
||||||
print(levenshtein_distance(word_1, len(word_1), word_2, len(word_2)))
|
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
|
|
||||||
def edit_distance(w1, w2):
|
|
||||||
"""
|
|
||||||
Find edit distance (minimum number of changes) required to convert string w1 to string w2.
|
|
||||||
"""
|
|
||||||
|
|
||||||
m = [[0 if x != 0 else y if y != 0 else x for x in range(len(w2) + 1)] for y in range(len(w1) + 1)]
|
|
||||||
|
|
||||||
for i in range(1,len(w1) + 1):
|
|
||||||
m[i][0] = i
|
|
||||||
for j in range(1, len(w2) + 1):
|
|
||||||
m[0][j] = j
|
|
||||||
|
|
||||||
for i in range(1, len(w1) + 1):
|
|
||||||
for j in range(1, len(w2) + 1):
|
|
||||||
m[i][j] = min(
|
|
||||||
m[i-1][j-1] + int(w1[i-1] != w2[j-1]),
|
|
||||||
m[i-1][j] + 1,
|
|
||||||
m[i][j-1] + 1
|
|
||||||
)
|
|
||||||
|
|
||||||
return m
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print(edit_distance("sitting", "kitten"))
|
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
# The Levenshtein distance (Edit distance) Problem
|
||||||
|
|
||||||
|
# Informally, the Levenshtein distance between two words is
|
||||||
|
# the minimum number of single-character edits (insertions, deletions or substitutions)
|
||||||
|
# required to change one word into the other.
|
||||||
|
|
||||||
|
# For example, the Levenshtein distance between kitten and sitting is 3.
|
||||||
|
# The minimal edit script that transforms the former into the latter is:
|
||||||
|
|
||||||
|
# kitten —> sitten (substitution of s for k)
|
||||||
|
# sitten —> sittin (substitution of i for e)
|
||||||
|
# sittin —> sitting (insertion of g at the end)
|
||||||
|
|
||||||
|
def levenshtein_distance(word_1, chars_1, word_2, chars_2):
|
||||||
|
# base case if the strings are empty
|
||||||
|
if chars_1 == 0:
|
||||||
|
return chars_2
|
||||||
|
if chars_2 == 0:
|
||||||
|
return chars_1
|
||||||
|
|
||||||
|
# if last characters of the string match, the cost of
|
||||||
|
# operations is 0, i.e. no changes are made
|
||||||
|
if word_1[chars_1 - 1] == word_2[chars_2 - 1]:
|
||||||
|
cost = 0
|
||||||
|
else:
|
||||||
|
cost = 1
|
||||||
|
|
||||||
|
# calculating the numbers of operations recursively
|
||||||
|
deletion = levenshtein_distance(word_1, chars_1 - 1, word_2, chars_2) + 1
|
||||||
|
insertion = levenshtein_distance(word_1, chars_1, word_2, chars_2 - 1) + 1
|
||||||
|
substitution = levenshtein_distance(word_1, chars_1 - 1, word_2, chars_2 - 1) + cost
|
||||||
|
|
||||||
|
return min(deletion, insertion, substitution)
|
||||||
|
|
||||||
|
# driving script
|
||||||
|
if __name__ == '__main__':
|
||||||
|
word_1 = 'plain'
|
||||||
|
word_2 = 'plane'
|
||||||
|
|
||||||
|
print('The Levenshtein distance is:')
|
||||||
|
print(levenshtein_distance(word_1, len(word_1), word_2, len(word_2)))
|
Loading…
Reference in New Issue