Posts

Showing posts from November, 2020

Evaluate Postfix Expression.

Evaluate Postfix Expression.  Problem:  Accept a postfix expression, evaluate it and print the result.  Input:  10 3 1 * + 9 - Output:  4 Program: s = input().split()  stack = []  for i in s:      if i.isdigit():          stack.append(int(i))      else:          b = stack.pop()          a = stack.pop()          if i == '+':              stack.append(a+b)          elif i == '-':              stack.append(a-b)          elif i == '*':              stack.append(a*b)          elif i == '/':             stack.append(a//b) print(*stack) Explanation:  1. Accept a string S (postfix expression) and split the string with res...