mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2024-11-15 17:30:52 +00:00
add subsequence, substring
This commit is contained in:
parent
0d991c54b9
commit
ab51583741
54
chapter11/subsequence.py
Normal file
54
chapter11/subsequence.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.DEBUG,
|
||||||
|
format="%(asctime)s %(levelname)s\n\r%(message)s",
|
||||||
|
datefmt="%H:%len_x:%S",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def longest_common_subsequence(X, Y, len_x, len_y):
|
||||||
|
# create table and zero fill it
|
||||||
|
table = [[0 for _ in range(len_x + 1)] for _ in range(len_y + 1)]
|
||||||
|
|
||||||
|
for i in range(1, len_y + 1):
|
||||||
|
for j in range(1, len_x + 1):
|
||||||
|
if X[j - 1] == Y[i - 1]:
|
||||||
|
table[i][j] = table[i - 1][j - 1] + 1
|
||||||
|
else:
|
||||||
|
table[i][j] = max(table[i - 1][j], table[i][j - 1])
|
||||||
|
|
||||||
|
format_and_print(X, Y, table)
|
||||||
|
|
||||||
|
return table[-1][-1]
|
||||||
|
|
||||||
|
|
||||||
|
def format_and_print(X, Y, table):
|
||||||
|
# enumerate first row (headings) + label each row in column0
|
||||||
|
for i, row in enumerate(table):
|
||||||
|
if i == 0:
|
||||||
|
continue
|
||||||
|
row[0] = Y[i - 1]
|
||||||
|
table[0] = [None, *X]
|
||||||
|
|
||||||
|
# print tabularised 2D array
|
||||||
|
logger.info(tabulate(table, tablefmt="pretty"))
|
||||||
|
|
||||||
|
|
||||||
|
X = "fosh"
|
||||||
|
Y = "fort"
|
||||||
|
|
||||||
|
logger.info(f"words: {X} {Y}")
|
||||||
|
longest_substring = longest_common_subsequence(X, Y, len(X), len(Y))
|
||||||
|
print(f"Longest common subsequence: {longest_substring}")
|
||||||
|
|
||||||
|
|
||||||
|
X = "fosh"
|
||||||
|
Y = "fish"
|
||||||
|
|
||||||
|
logger.info(f"words: {X} {Y}")
|
||||||
|
longest_substring = longest_common_subsequence(X, Y, len(X), len(Y))
|
||||||
|
print(f"Longest common subsequence: {longest_substring}")
|
57
chapter11/substring.py
Normal file
57
chapter11/substring.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from tabulate import tabulate
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.DEBUG,
|
||||||
|
format="%(asctime)s %(levelname)s\n\r%(message)s",
|
||||||
|
datefmt="%H:%M:%S",
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def longest_common_substring(X, Y, len_x, len_y):
|
||||||
|
# create table and zero fill it
|
||||||
|
table = [[0 for _ in range(len_x + 1)] for _ in range(len_y + 1)]
|
||||||
|
|
||||||
|
longest = 0
|
||||||
|
|
||||||
|
for i in range(1, len_y + 1):
|
||||||
|
for j in range(1, len_x + 1):
|
||||||
|
if X[j - 1] == Y[i - 1]:
|
||||||
|
table[i][j] = table[i - 1][j - 1] + 1
|
||||||
|
longest = max(longest, table[i][j])
|
||||||
|
else:
|
||||||
|
table[i][j] = 0
|
||||||
|
|
||||||
|
format_and_print(X, Y, table)
|
||||||
|
|
||||||
|
return longest
|
||||||
|
|
||||||
|
|
||||||
|
def format_and_print(X, Y, table):
|
||||||
|
# enumerate first row (headings) + label each row in column0
|
||||||
|
for i, row in enumerate(table):
|
||||||
|
if i == 0:
|
||||||
|
continue
|
||||||
|
row[0] = Y[i - 1]
|
||||||
|
table[0] = [None, *X]
|
||||||
|
|
||||||
|
# print tabularised 2D array
|
||||||
|
logger.info(tabulate(table, tablefmt="pretty"))
|
||||||
|
|
||||||
|
|
||||||
|
X = "fish"
|
||||||
|
Y = "vish"
|
||||||
|
|
||||||
|
logger.info(f"words: {X} {Y}")
|
||||||
|
longest_substring = longest_common_substring(X, Y, len(X), len(Y))
|
||||||
|
print(f"Longest common substring: {longest_substring}")
|
||||||
|
|
||||||
|
|
||||||
|
X = "vista"
|
||||||
|
Y = "hish"
|
||||||
|
|
||||||
|
logger.info(f"words: {X} {Y}")
|
||||||
|
longest_substring = longest_common_substring(X, Y, len(X), len(Y))
|
||||||
|
print(f"Longest common substring: {longest_substring}")
|
Loading…
Reference in New Issue
Block a user