給定一個字符串,例如'helloyellowellow',解析給定字符串中的所有有效字符串。 (例如:[[hell,hello,yellow],[low,low] ........]使用Python進行字符串解析?
我正在尋找最優化的編寫代碼的方式。知道這是最好的辦法
全面披露 - 這是一個面試問題
master = []
# Dictionary for us to look up words
def is_word(inputstr):
#returns True/False
def processstring(fstr,secstr,li):
if is_word(fstr):
li.append(fstr)
if len(secstr) == 0:
if len(li) != 0:
master.append(li)
return
processstring(fstr+secstr[0], secstr[1:len(secstr)],li)
def wrapperprocess(inpstr):
li = []
if len(inpstr) == 0:
return
processstring('',inpstr,li)
wrapperprocess(inpstr[1:len(inpstr)])
wrapperprocess('helloyellowellow')
print master
在您的解決方案,看起來像你忘了'返回李'。一個更好的方法是「匹配」匹配的單詞,而不是維護一個列表,追加到它並返回它。 – shx2