Kattis Polish Notation challenge in Python

337 views Asked by At

I'm trying to do the polish notation challenge on kattis.com. Thing is, I feel I have done everything they asked for and I've tried fixing everything I could think of. I even looked up some other's solutions and while theirs are more clean I want to continue on mine as I am learning.

Why is it that for example this person's code works but not mine?

Here is my current 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, myLine, 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
1

There are 1 answers

3
Nizam Mohamed On

You're bringing some other language style to Python, that's unnecessary because Python is more flexible.

I've simplified as much as I can here.

Split the input string on white spaces and iterate over the tokens.

For every operator in the expression, push a list onto the stack and append the operator and its operands to the list.

Now pop each list off the stack and process the 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)                        

Output;

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