def insert_all(word, letter):
"""
Computes all words that can be formed from word simply by inserting
letter into all positions in word
word -- string
return list
"""
result = ""
index = 0
x = index + 1
while 0index:
result += word[:index] + letter + word[x:]
index += 1
return result
assertEqual(set(insert_all("","a")), set(["a"]))
assertEqual(set(insert_all("a","b")), set(["ab","ba"]))
assertEqual(set(insert_all("ab","c")), set(["abc","acb","cab"]))
运行后它说Traceback (most recent call last):
File "C:\Python26\SAVES\lab6.py", line 198, in
assertEqual(set(insert_all("a","b")), set(["ab","ba"]))
File "C:\Python26\SAVES\lab6.py", line 186, in insert_all
result += word[:index], letter
TypeError: cannot concatenate 'str' and 'tuple' objects
求跟正
|