从伪代码为python中的字符串匹配创建移位表
我试图从我的教科书中获取伪代码,并将其转换为python函数。伪代码用于创建一个移位表,以便与Horspool字符串匹配一起使用 伪代码如下所示:从伪代码为python中的字符串匹配创建移位表,python,shift,Python,Shift,我试图从我的教科书中获取伪代码,并将其转换为python函数。伪代码用于创建一个移位表,以便与Horspool字符串匹配一起使用 伪代码如下所示: ShiftTable(P[0..m - 1]) //Takes input pattern P[0..m-1] and alphabet of possible characters //Produces output Table[0..size-1] indexed by alphabet's characters and filled with
ShiftTable(P[0..m - 1])
//Takes input pattern P[0..m-1] and alphabet of possible characters
//Produces output Table[0..size-1] indexed by alphabet's characters and filled with shift sizes computed by formula
for i = 0 to size - 1 do Table[i] = m
for j = 0 to m - 2 do Table[P[j]] = m - 1 - j
return Table
def BuildShiftTable(pattern):
m = len(pattern)
alphabet = [i for i in string.ascii_lowercase]
print(alphabet)
for i in alphabet:
table[i] = alphabet[i]
for j in m - 2:
table[pattern[j]] = m-1-j
return table
我的代码如下:
ShiftTable(P[0..m - 1])
//Takes input pattern P[0..m-1] and alphabet of possible characters
//Produces output Table[0..size-1] indexed by alphabet's characters and filled with shift sizes computed by formula
for i = 0 to size - 1 do Table[i] = m
for j = 0 to m - 2 do Table[P[j]] = m - 1 - j
return Table
def BuildShiftTable(pattern):
m = len(pattern)
alphabet = [i for i in string.ascii_lowercase]
print(alphabet)
for i in alphabet:
table[i] = alphabet[i]
for j in m - 2:
table[pattern[j]] = m-1-j
return table
它显然不起作用,但我试图理解如何将伪代码的作用转化为工作代码,因为我不确定伪代码中的第一个for循环是什么意思,如果还没有创建,那么从表列表中获取size-1。感谢您的帮助 我看到的一个问题是,你在m-2中对j说
m-2'是整数,因为'm'是整数;在int中为j编写没有多大意义。我想你的意思是为范围(0,m-2)内的j编写