我正在尝试创建一个传递字符串的函数,该字符串将是一个数学表达式和一个括号位置,返回我作为参数传递的位置的左括号的位置。
示例:
myfunction('(7 * 3) +1', 4)
应返回0
,因为位置4的括号在位置0处打开。
我甚至试图这样做,但它只适用于一些表达式,具体取决于位置而且我通过了。
我试过
def pos_open_parent(exp, pos_closed):
temp_exp = exp[:pos_closed]
count_open = 0
count_closed = 1
i = len(temp_exp) -1
while count_open != count_closed:
if temp_exp[i] == '(':
count_open += 1
elif temp_exp[i] == ')':
count_closed += 1
i -= 1
return i + 1
答案 0 :(得分:2)
你的代码似乎工作得很好,实际上,你只需要考虑没有匹配的开括号的情况,否则会引发异常。
作为算法的略微变化,我建议向后扫描表达式,计算您仍然需要的左括号的数量,并在该数字达到零时立即返回索引。
def pos_open_parens(exp, pos):
count = 1 # one open parens to find
for i in range(pos, -1, -1):
if exp[i] == ')': count += 1 # now we need one more...
if exp[i] == '(': count -= 1 # found one!
if count == 0: return i # found all the parens we need
return None # no matching open parens -> None
对于'(7*(1+4)-3)+1'
和职位2
,4
,9
和11
,这会返回0
,3
, 0
和None
,即如果找不到左括号(或者不足以匹配右括号),它将返回None
。
请注意,此可能表示表达式中存在不平衡的括号,但它也可以完全正常,就像我在上一个示例中一样。要检查不平衡的括号,可以使用类似的算法,扫描整个字符串并检查计数是否平衡。
答案 1 :(得分:1)
此脚本假定您的expr字符串是括号平衡的。
说明:
代码:
def find_matching_openin_par(expr, par):
if expr[par] != ')':
return "the specified pos is not a closing paren."
else:
opens = []
for index, ch_ in enumerate(expr):
if ch_ == '(':
opens.append(index)
#add a new opening paren. to the list
elif ch_ == ')':
#get the last added position of a "(" parenthesis.
pos = opens.pop() #it may throw an exception if the string is unbalanced!
if index == par:
#we keep that position, since all previously matched parenthesis were popped.
return pos
return "not found. malformed string"
答案 2 :(得分:0)
假设表达式始终有效,您的问题很容易解决。
以下是以下内容的实现:
def yourfunction(first_arg, second_arg):
stack = []
for i, c in enumerate(first_arg):
if i == second_arg:
return -1 if len(stack) == 0 else stack.pop()
if c == '(':
stack.append(i)
elif c == ')':
if len(stack) > 0:
stack.pop()
else:
raise ValueError('Mathematical expression is invalid. Check your parenthesis')
print yourfunction('(7*3)+1', 4)
0
print yourfunction('((7)*3)+1', 6)
0
print yourfunction('((7)*(3)+1', 7)
5
答案 3 :(得分:0)
这需要仅解析您提供的位置。它不会尝试强制执行有效的表达式。你应该单独做。
您没有指定的是针对任何括号之外的位置。我实现了它以返回-1
中任何未包含在括号中的位置。
def myfunction(expression, position):
open_positions = list()
for i, c in enumerate(expression):
if c == '(':
# found a new open parens. everything after this points to here
# until a new one is opened or this one is closed
open_positions.append(i)
if i == position:
# this is the position we are looking for
# return the current open parentheses position (or -1 if no parens)
try:
return open_positions[-1]
except IndexError:
return -1
if c == ')':
# closed a set of parentheses so nothing else can be inside
# remove the current open parentheses
open_positions.pop() # finished parens before getting to position
expression = '(1*((2+3)*4))+5'
visual_confirmation = '012345678901234'
for i in range(len(expression)):
open_position = myfunction(expression, i)
print 'open parens for position {} at {}'.format(i, open_position)
open parens for position 0 at 0
open parens for position 1 at 0
open parens for position 2 at 0
open parens for position 3 at 3
open parens for position 4 at 4
open parens for position 5 at 4
open parens for position 6 at 4
open parens for position 7 at 4
open parens for position 8 at 4
open parens for position 9 at 3
open parens for position 10 at 3
open parens for position 11 at 3
open parens for position 12 at 0
open parens for position 13 at -1
open parens for position 14 at -1