mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
psf/black code formatting (#1277)
This commit is contained in:
committed by
Christian Clauss
parent
07f04a2e55
commit
9eac17a408
@@ -20,12 +20,9 @@ Time Complexity : O(n/m)
|
||||
|
||||
|
||||
class BoyerMooreSearch:
|
||||
|
||||
|
||||
def __init__(self, text, pattern):
|
||||
self.text, self.pattern = text, pattern
|
||||
self.textLen, self.patLen = len(text), len(pattern)
|
||||
|
||||
|
||||
def match_in_pattern(self, char):
|
||||
""" finds the index of char in pattern in reverse order
|
||||
@@ -36,14 +33,13 @@ class BoyerMooreSearch:
|
||||
Returns :
|
||||
i (int): index of char from last in pattern
|
||||
-1 (int): if char is not found in pattern
|
||||
"""
|
||||
"""
|
||||
|
||||
for i in range(self.patLen-1, -1, -1):
|
||||
for i in range(self.patLen - 1, -1, -1):
|
||||
if char == self.pattern[i]:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
def mismatch_in_text(self, currentPos):
|
||||
""" finds the index of mis-matched character in text when compared with pattern from last
|
||||
|
||||
@@ -55,14 +51,13 @@ class BoyerMooreSearch:
|
||||
-1 (int): if there is no mis-match between pattern and text block
|
||||
"""
|
||||
|
||||
for i in range(self.patLen-1, -1, -1):
|
||||
for i in range(self.patLen - 1, -1, -1):
|
||||
if self.pattern[i] != self.text[currentPos + i]:
|
||||
return currentPos + i
|
||||
return -1
|
||||
|
||||
|
||||
def bad_character_heuristic(self):
|
||||
# searches pattern in text and returns index positions
|
||||
# searches pattern in text and returns index positions
|
||||
positions = []
|
||||
for i in range(self.textLen - self.patLen + 1):
|
||||
mismatch_index = self.mismatch_in_text(i)
|
||||
@@ -70,12 +65,14 @@ class BoyerMooreSearch:
|
||||
positions.append(i)
|
||||
else:
|
||||
match_index = self.match_in_pattern(self.text[mismatch_index])
|
||||
i = mismatch_index - match_index #shifting index lgtm [py/multiple-definition]
|
||||
i = (
|
||||
mismatch_index - match_index
|
||||
) # shifting index lgtm [py/multiple-definition]
|
||||
return positions
|
||||
|
||||
|
||||
|
||||
text = "ABAABA"
|
||||
pattern = "AB"
|
||||
pattern = "AB"
|
||||
bms = BoyerMooreSearch(text, pattern)
|
||||
positions = bms.bad_character_heuristic()
|
||||
|
||||
@@ -84,5 +81,3 @@ if len(positions) == 0:
|
||||
else:
|
||||
print("Pattern found in following positions: ")
|
||||
print(positions)
|
||||
|
||||
|
||||
|
||||
@@ -46,14 +46,14 @@ def get_failure_array(pattern):
|
||||
if pattern[i] == pattern[j]:
|
||||
i += 1
|
||||
elif i > 0:
|
||||
i = failure[i-1]
|
||||
i = failure[i - 1]
|
||||
continue
|
||||
j += 1
|
||||
failure.append(i)
|
||||
return failure
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
# Test 1)
|
||||
pattern = "abc1abc12"
|
||||
text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc"
|
||||
|
||||
@@ -64,10 +64,13 @@ def levenshtein_distance(first_word, second_word):
|
||||
return previous_row[-1]
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
first_word = input('Enter the first word:\n').strip()
|
||||
second_word = input('Enter the second word:\n').strip()
|
||||
if __name__ == "__main__":
|
||||
first_word = input("Enter the first word:\n").strip()
|
||||
second_word = input("Enter the second word:\n").strip()
|
||||
|
||||
result = levenshtein_distance(first_word, second_word)
|
||||
print('Levenshtein distance between {} and {} is {}'.format(
|
||||
first_word, second_word, result))
|
||||
print(
|
||||
"Levenshtein distance between {} and {} is {}".format(
|
||||
first_word, second_word, result
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
# calculate palindromic length from center with incrementing difference
|
||||
def palindromic_length( center, diff, string):
|
||||
if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] :
|
||||
def palindromic_length(center, diff, string):
|
||||
if (
|
||||
center - diff == -1
|
||||
or center + diff == len(string)
|
||||
or string[center - diff] != string[center + diff]
|
||||
):
|
||||
return 0
|
||||
return 1 + palindromic_length(center, diff+1, string)
|
||||
return 1 + palindromic_length(center, diff + 1, string)
|
||||
|
||||
def palindromic_string( input_string ):
|
||||
|
||||
def palindromic_string(input_string):
|
||||
"""
|
||||
Manacher’s algorithm which finds Longest Palindromic Substring in linear time.
|
||||
|
||||
@@ -16,37 +21,36 @@ def palindromic_string( input_string ):
|
||||
3. return output_string from center - max_length to center + max_length and remove all "|"
|
||||
"""
|
||||
max_length = 0
|
||||
|
||||
|
||||
# if input_string is "aba" than new_input_string become "a|b|a"
|
||||
new_input_string = ""
|
||||
output_string = ""
|
||||
|
||||
# append each character + "|" in new_string for range(0, length-1)
|
||||
for i in input_string[:len(input_string)-1] :
|
||||
for i in input_string[: len(input_string) - 1]:
|
||||
new_input_string += i + "|"
|
||||
#append last character
|
||||
# append last character
|
||||
new_input_string += input_string[-1]
|
||||
|
||||
|
||||
# for each character in new_string find corresponding palindromic string
|
||||
for i in range(len(new_input_string)) :
|
||||
for i in range(len(new_input_string)):
|
||||
|
||||
# get palindromic length from ith position
|
||||
length = palindromic_length(i, 1, new_input_string)
|
||||
|
||||
# update max_length and start position
|
||||
if max_length < length :
|
||||
if max_length < length:
|
||||
max_length = length
|
||||
start = i
|
||||
|
||||
#create that string
|
||||
for i in new_input_string[start-max_length:start+max_length+1] :
|
||||
|
||||
# create that string
|
||||
for i in new_input_string[start - max_length : start + max_length + 1]:
|
||||
if i != "|":
|
||||
output_string += i
|
||||
|
||||
|
||||
return output_string
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
n = input()
|
||||
print(palindromic_string(n))
|
||||
|
||||
@@ -1,114 +1,118 @@
|
||||
'''
|
||||
"""
|
||||
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
|
||||
The only allowed operations are
|
||||
---Copy character with cost cC
|
||||
---Replace character with cost cR
|
||||
---Delete character with cost cD
|
||||
---Insert character with cost cI
|
||||
'''
|
||||
"""
|
||||
|
||||
|
||||
def compute_transform_tables(X, Y, cC, cR, cD, cI):
|
||||
X = list(X)
|
||||
Y = list(Y)
|
||||
m = len(X)
|
||||
n = len(Y)
|
||||
X = list(X)
|
||||
Y = list(Y)
|
||||
m = len(X)
|
||||
n = len(Y)
|
||||
|
||||
costs = [[0 for _ in range(n+1)] for _ in range(m+1)]
|
||||
ops = [[0 for _ in range(n+1)] for _ in range(m+1)]
|
||||
costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||
ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
||||
|
||||
for i in range(1, m+1):
|
||||
costs[i][0] = i*cD
|
||||
ops[i][0] = 'D%c' % X[i-1]
|
||||
for i in range(1, m + 1):
|
||||
costs[i][0] = i * cD
|
||||
ops[i][0] = "D%c" % X[i - 1]
|
||||
|
||||
for i in range(1, n+1):
|
||||
costs[0][i] = i*cI
|
||||
ops[0][i] = 'I%c' % Y[i-1]
|
||||
for i in range(1, n + 1):
|
||||
costs[0][i] = i * cI
|
||||
ops[0][i] = "I%c" % Y[i - 1]
|
||||
|
||||
for i in range(1, m+1):
|
||||
for j in range(1, n+1):
|
||||
if X[i-1] == Y[j-1]:
|
||||
costs[i][j] = costs[i-1][j-1] + cC
|
||||
ops[i][j] = 'C%c' % X[i-1]
|
||||
else:
|
||||
costs[i][j] = costs[i-1][j-1] + cR
|
||||
ops[i][j] = 'R%c' % X[i-1] + str(Y[j-1])
|
||||
for i in range(1, m + 1):
|
||||
for j in range(1, n + 1):
|
||||
if X[i - 1] == Y[j - 1]:
|
||||
costs[i][j] = costs[i - 1][j - 1] + cC
|
||||
ops[i][j] = "C%c" % X[i - 1]
|
||||
else:
|
||||
costs[i][j] = costs[i - 1][j - 1] + cR
|
||||
ops[i][j] = "R%c" % X[i - 1] + str(Y[j - 1])
|
||||
|
||||
if costs[i-1][j] + cD < costs[i][j]:
|
||||
costs[i][j] = costs[i-1][j] + cD
|
||||
ops[i][j] = 'D%c' % X[i-1]
|
||||
if costs[i - 1][j] + cD < costs[i][j]:
|
||||
costs[i][j] = costs[i - 1][j] + cD
|
||||
ops[i][j] = "D%c" % X[i - 1]
|
||||
|
||||
if costs[i][j-1] + cI < costs[i][j]:
|
||||
costs[i][j] = costs[i][j-1] + cI
|
||||
ops[i][j] = 'I%c' % Y[j-1]
|
||||
if costs[i][j - 1] + cI < costs[i][j]:
|
||||
costs[i][j] = costs[i][j - 1] + cI
|
||||
ops[i][j] = "I%c" % Y[j - 1]
|
||||
|
||||
return costs, ops
|
||||
|
||||
return costs, ops
|
||||
|
||||
def assemble_transformation(ops, i, j):
|
||||
if i == 0 and j == 0:
|
||||
seq = []
|
||||
return seq
|
||||
else:
|
||||
if ops[i][j][0] == 'C' or ops[i][j][0] == 'R':
|
||||
seq = assemble_transformation(ops, i-1, j-1)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
elif ops[i][j][0] == 'D':
|
||||
seq = assemble_transformation(ops, i-1, j)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
else:
|
||||
seq = assemble_transformation(ops, i, j-1)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
if i == 0 and j == 0:
|
||||
seq = []
|
||||
return seq
|
||||
else:
|
||||
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
||||
seq = assemble_transformation(ops, i - 1, j - 1)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
elif ops[i][j][0] == "D":
|
||||
seq = assemble_transformation(ops, i - 1, j)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
else:
|
||||
seq = assemble_transformation(ops, i, j - 1)
|
||||
seq.append(ops[i][j])
|
||||
return seq
|
||||
|
||||
if __name__ == '__main__':
|
||||
_, operations = compute_transform_tables('Python', 'Algorithms', -1, 1, 2, 2)
|
||||
|
||||
m = len(operations)
|
||||
n = len(operations[0])
|
||||
sequence = assemble_transformation(operations, m-1, n-1)
|
||||
if __name__ == "__main__":
|
||||
_, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2)
|
||||
|
||||
string = list('Python')
|
||||
i = 0
|
||||
cost = 0
|
||||
m = len(operations)
|
||||
n = len(operations[0])
|
||||
sequence = assemble_transformation(operations, m - 1, n - 1)
|
||||
|
||||
with open('min_cost.txt', 'w') as file:
|
||||
for op in sequence:
|
||||
print(''.join(string))
|
||||
string = list("Python")
|
||||
i = 0
|
||||
cost = 0
|
||||
|
||||
if op[0] == 'C':
|
||||
file.write('%-16s' % 'Copy %c' % op[1])
|
||||
file.write('\t\t\t' + ''.join(string))
|
||||
file.write('\r\n')
|
||||
with open("min_cost.txt", "w") as file:
|
||||
for op in sequence:
|
||||
print("".join(string))
|
||||
|
||||
cost -= 1
|
||||
elif op[0] == 'R':
|
||||
string[i] = op[2]
|
||||
if op[0] == "C":
|
||||
file.write("%-16s" % "Copy %c" % op[1])
|
||||
file.write("\t\t\t" + "".join(string))
|
||||
file.write("\r\n")
|
||||
|
||||
file.write('%-16s' % ('Replace %c' % op[1] + ' with ' + str(op[2])))
|
||||
file.write('\t\t' + ''.join(string))
|
||||
file.write('\r\n')
|
||||
cost -= 1
|
||||
elif op[0] == "R":
|
||||
string[i] = op[2]
|
||||
|
||||
cost += 1
|
||||
elif op[0] == 'D':
|
||||
string.pop(i)
|
||||
file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2])))
|
||||
file.write("\t\t" + "".join(string))
|
||||
file.write("\r\n")
|
||||
|
||||
file.write('%-16s' % 'Delete %c' % op[1])
|
||||
file.write('\t\t\t' + ''.join(string))
|
||||
file.write('\r\n')
|
||||
cost += 1
|
||||
elif op[0] == "D":
|
||||
string.pop(i)
|
||||
|
||||
cost += 2
|
||||
else:
|
||||
string.insert(i, op[1])
|
||||
file.write("%-16s" % "Delete %c" % op[1])
|
||||
file.write("\t\t\t" + "".join(string))
|
||||
file.write("\r\n")
|
||||
|
||||
file.write('%-16s' % 'Insert %c' % op[1])
|
||||
file.write('\t\t\t' + ''.join(string))
|
||||
file.write('\r\n')
|
||||
cost += 2
|
||||
else:
|
||||
string.insert(i, op[1])
|
||||
|
||||
cost += 2
|
||||
file.write("%-16s" % "Insert %c" % op[1])
|
||||
file.write("\t\t\t" + "".join(string))
|
||||
file.write("\r\n")
|
||||
|
||||
i += 1
|
||||
cost += 2
|
||||
|
||||
print(''.join(string))
|
||||
print('Cost: ', cost)
|
||||
i += 1
|
||||
|
||||
file.write('\r\nMinimum cost: ' + str(cost))
|
||||
print("".join(string))
|
||||
print("Cost: ", cost)
|
||||
|
||||
file.write("\r\nMinimum cost: " + str(cost))
|
||||
|
||||
@@ -7,23 +7,26 @@ Complexity : O(n*m)
|
||||
n=length of main string
|
||||
m=length of pattern string
|
||||
"""
|
||||
def naivePatternSearch(mainString,pattern):
|
||||
patLen=len(pattern)
|
||||
strLen=len(mainString)
|
||||
position=[]
|
||||
for i in range(strLen-patLen+1):
|
||||
match_found=True
|
||||
|
||||
|
||||
def naivePatternSearch(mainString, pattern):
|
||||
patLen = len(pattern)
|
||||
strLen = len(mainString)
|
||||
position = []
|
||||
for i in range(strLen - patLen + 1):
|
||||
match_found = True
|
||||
for j in range(patLen):
|
||||
if mainString[i+j]!=pattern[j]:
|
||||
match_found=False
|
||||
if mainString[i + j] != pattern[j]:
|
||||
match_found = False
|
||||
break
|
||||
if match_found:
|
||||
position.append(i)
|
||||
return position
|
||||
|
||||
mainString="ABAAABCDBBABCDDEBCABC"
|
||||
pattern="ABC"
|
||||
position=naivePatternSearch(mainString,pattern)
|
||||
|
||||
mainString = "ABAAABCDBBABCDDEBCABC"
|
||||
pattern = "ABC"
|
||||
position = naivePatternSearch(mainString, pattern)
|
||||
print("Pattern found in position ")
|
||||
for x in position:
|
||||
print(x)
|
||||
print(x)
|
||||
|
||||
Reference in New Issue
Block a user