2024-04-18 10:52:11 发布
网友
出于交互式解析的目的,给定一个输入字符串,我需要从索引0开始提取可能最长的子字符串,并且只包含匹配的括号。你知道吗
示例(类似LISP的s表达式)
输入字符串:(print "hello") (assign a (+ c d)) (assign e (+ f g)
(print "hello") (assign a (+ c d)) (assign e (+ f g)
输出子串:(print "hello") (assign a (+ c d))
(print "hello") (assign a (+ c d))
我想做一个简单的Python函数来实现这一点。你知道吗
在计算圆括号时循环字符串,最后将字符串切片到圆括号计数器为0的最后一个索引:
def max_parseable_substring(text): parentheses = 0 end = 0 for i, char in enumerate(text): if char == "(": parentheses += 1 elif char == ")": parentheses -= 1 if parentheses == 0: end = i return text[:end]
在计算圆括号时循环字符串,最后将字符串切片到圆括号计数器为0的最后一个索引:
相关问题 更多 >
编程相关推荐