2015-10-27 28 views
0

我想從我的教科書中獲取僞代碼並將其轉換爲python函數。僞代碼用於創建用於Horspool字符串匹配的移位表。從僞代碼創建python字符串匹配的移位表

僞代碼如下:

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 

這顯然是行不通的,但我想了解如何採取什麼僞代碼正在執行並將其轉換爲工作代碼,因爲我並不完全確定僞代碼中的第一個循環是如何通過從表列表中獲取size-1(如果尚未創建的話)。任何幫助表示讚賞。

回答

0

我看到的一個問題是你說for j in m-2。 'm-2'是一個int,因爲'm'是一個int;寫for j in int沒有多大意義。我想你的意思是寫for j in range(0,m-2)