Python 字符串的递归解压缩
我正在尝试使用递归解压字符串。例如,输入: 3[b3[a] 应输出: baaabaaa 但我得到: BAAABAAABAAABBAABAAABAAAAAAAA 我有以下代码,但它显然是关闭的。第一个find_end函数按预期工作。我对使用递归和任何帮助理解/跟踪额外字母的来源或任何帮助我理解这种非常酷的方法的一般提示都是非常感谢的Python 字符串的递归解压缩,python,Python,我正在尝试使用递归解压字符串。例如,输入: 3[b3[a] 应输出: baaabaaa 但我得到: BAAABAAABAAABBAABAAABAAAAAAAA 我有以下代码,但它显然是关闭的。第一个find_end函数按预期工作。我对使用递归和任何帮助理解/跟踪额外字母的来源或任何帮助我理解这种非常酷的方法的一般提示都是非常感谢的 def find_end(original, start, level): if original[start] != "[":
def find_end(original, start, level):
if original[start] != "[":
message = "ERROR in find_error, must start with [:", original[start:]
raise ValueError(message)
indent = level * " "
index = start + 1
count = 1
while count != 0 and index < len(original):
if original[index] == "[":
count += 1
elif original[index] == "]":
count -= 1
index += 1
if count != 0:
message = "ERROR in find_error, mismatched brackets:", original[start:]
raise ValueError(message)
return index - 1
def decompress(original, level):
# set the result to an empty string
result = ""
# for any character in the string we have not looked at yet
for i in range(len(original)):
# if the character at the current index is a digit
if original[i].isnumeric():
# the character of the current index is the number of repetitions needed
repititions = int(original[i])
# start = the next index containing the '[' character
x = 0
while x < (len(original)):
if original[x].isnumeric():
start = x + 1
x = len(original)
else:
x += 1
# last = the index of the matching ']'
last = find_end(original, start, level)
# calculate a substring using `original[start + 1:last]
sub_original = original[start + 1 : last]
# RECURSIVELY call decompress with the substring
# sub = decompress(original, level + 1)
# concatenate the result of the recursive call times the number of repetitions needed to the result
result += decompress(sub_original, level + 1) * repititions
# set the current index to the index of the matching ']'
i = last
# else
else:
# concatenate the letter at the current index to the result
if original[i] != "[" and original[i] != "]":
result += original[i]
# return the result
return result
def main():
passed = True
ORIGINAL = 0
EXPECTED = 1
# The test cases
provided = [
("3[b]", "bbb"),
("3[b3[a]]", "baaabaaabaaa"),
("3[b2[ca]]", "bcacabcacabcaca"),
("5[a3[b]1[ab]]", "abbbababbbababbbababbbababbbab"),
]
# Run the provided tests cases
for t in provided:
actual = decompress(t[ORIGINAL], 0)
if actual != t[EXPECTED]:
print("Error decompressing:", t[ORIGINAL])
print(" Expected:", t[EXPECTED])
print(" Actual: ", actual)
print()
passed = False
# print that all the tests passed
if passed:
print("All tests passed")
if __name__ == '__main__':
main()
def find_end(原始、开始、级别):
如果原始[开始]!="[":
message=“查找错误中的错误,必须以[:”开头,原始[开始:]
提升值错误(消息)
缩进=级别*“”
索引=开始+1
计数=1
当计数!=0且索引
根据我从您的代码中收集到的信息,它可能给出了错误的结果,因为您在给定级别上查找最后一个匹配的右括号的方法(我不是100%确定,代码太多)。但是,我可以建议使用堆栈的更干净的方法(几乎类似于DFS,没有复杂性):
def反编译:
堆栈=[]
对于s中的i:
如果i.isalnum():
stack.append(i)
elif i==“]”:
temp=stack.pop()
count=stack.pop()
如果count.isnumeric():
stack.append(int(count)*temp)
其他:
stack.append(计数+临时)
对于范围内的i(透镜(堆栈)-2,-1,-1):
如果堆栈[i].isnumeric():
堆栈[i]=int(堆栈[i])*堆栈[i+1]
其他:
堆栈[i]+=堆栈[i+1]
返回堆栈[0]
打印(反编译(“3[b]”)#bbb
打印(反编译(“3[b3[a]”)#baaabaaa
打印(反编译(“3[b2[ca]]”)
打印(反编译(“5[a3[b]1[ab]]”)
这只适用于一个简单的观察:与其在读取[
后评估子字符串,不如在遇到]
后评估子字符串。这将允许您在对各个部分进行单独评估之后构建结果。(这类似于使用编程进行前缀/后缀评估)。(如果愿意,您也可以在此过程中添加错误检查。在一个过程中检查字符串的语义是否正确,并在另一个过程中对其求值,比一次完成两个过程更容易)