Python中的Kattis波兰表示法挑战

问题描述

我正在尝试做polish notation challenge on kattis.com。事情是,我觉得我已经完成了他们所要求的一切,并且我尝试了解决所有可能想到的问题。我什至查看了其他人的解决方案,尽管他们的解决方案更干净,但我想在学习时继续使用我的解决方案。

为什么this person's code起作用但我的却不起作用?

这是我当前的代码

import sys
case = 1
valid_ints = set([str(i) for i in range(-10,11)])


def simplify(index,myLine,processed):
  while index+1 > 0:
      if (myLine[index] == "+" or myLine[index] == "-" or myLine[index] == "*") and index < len(myLine)-2:
        if myLine[index+1] in valid_ints and myLine[index+2] in valid_ints:
          try:
            processed = myLine[index+3:] + processed
            a = str(myLine[index+1] + myLine[index] + myLine[index+2])
            processed.insert(0,str(eval(a)))
            del myLine[index:]
          except:
            processed = [myLine[index],myLine[index+1],myLine[index+2]] + processed
            del myLine[index:]
      elif len(myLine) < 3:
        processed = myLine + processed
        del myLine[index]
      index -= 1
  processed = myLine + processed
  return processed

for line in sys.stdin:
    myLine = line.split()
    processed = []
    index = len(myLine)-1
    savedprocessed = []
    processed = simplify(index,processed)
    while True:
      if savedprocessed == processed:
        break
      else:
        savedprocessed = []
        savedprocessed += processed
        processed = simplify(len(processed)-1,processed,[])
        
    result = " ".join(savedprocessed)
    print("Case " + str(case) + ": " + result)
    case += 1
    if case > 5:
      break

解决方法

您要为Python带来其他语言风格,这是不必要的,因为Python更灵活。

我在这里已尽力简化了。

将输入字符串分隔在空白处并遍历标记。

对于表达式中的每个运算符,将list推入堆栈,并将运算符及其操作数附加到list

现在将每个list弹出堆栈并处理list

def simplify(exp):                                                                                                                                                        
    stack1 = []                                                                                                                                                           
    ops = set('+*-')                                                                                                                                                      
    for token in exp.split():                                                                                                                                             
        if token in ops:                                                                                                                                                  
            stack1.append([])                                                                                                                                             
        stack1[-1].append(token)                                                                                                                                          
                                                                                                                                                                          
    stack2 = []                                                                                                                                                           
    while stack1:                                                                                                                                                         
        top = stack1.pop()                                                                                                                                                
        while len(top) < 3 and stack2:                                                                                                                                    
            top.append(stack2.pop())                                                                                                                                      
        if any(x.isalpha() for x in top):                                                                                                                                 
            simplified = ' '.join(top)                                                                                                                                    
        else:                                                                                                                                                             
            top[0],top[1] = top[1],top[0]                                                                                                                               
            simplified = str(eval(''.join(top)))                                                                                                                          
        stack2.append(simplified)                                                                                                                                         
    return simplified                                                                                                                                                     

exp = '* - 6 + x -6 - - 9 6 * 0 c'                                                                                                                                        
print(exp)
simplify(exp)                        

输出;

* - 6 + x -6 - - 9 6 * 0 c
* - 6 + x -6 - - 3 * 0 c