Question: WRITE A PYTHON PROGRAM USING THEM TEMPLATE AND HELPER CODE BELOW: from itertools import permutations as perms import math class Postfix_Expression: def __init__(self, exp): self.exp

WRITE A PYTHON PROGRAM USING THEM TEMPLATE AND HELPER CODE BELOW:

WRITE A PYTHON PROGRAM USING THEM TEMPLATE AND HELPER CODE BELOW: fromitertools import permutations as perms import math class Postfix_Expression: def __init__(self, exp):

from itertools import permutations as perms import math

class Postfix_Expression:

def __init__(self, exp): self.exp = exp self.ops = {'+': Fraction.__add__, '-': Fraction.__sub__, '*': Fraction.__mul__, '/': Fraction.__truediv__, '%': Fraction.__mod__, 'c': concatenate} self.cards = {str(i): i for i in range(2,10)} self.cards.update({'A': 1, '0': 10, 'J': 11, 'Q': 12, 'K': 13})

def evaluate(self, seq): stack1 = [] for value in seq: if value in self.cards: stack1.append(self.cards[value]) elif value in self.ops: if len(stack1)

return stack1.pop()

def isvalid(self, seq): return self.evaluate(seq) != None

def concatenate(a, b): a = abs(math.ceil(float(a))) b = abs(math.ceil(float(b))) return int(str(a) + str(b))

1 24 Card Game In this assignment, you have to find all the valid postfix notations that include the four cards, and also addition, subtraction, multiplication, division, modulus and concatenation operations Evaluate these postfix notations and see which of them lead to 24 2 Generating valid postfix expressions The find_postfix0 function will find all valid postfix notations for each 4 card combination and evaluate them to see if they are equal to 24. You will first call your isvalidO method to check the expression is valid. Then you will call the evaluate() method inside of this function to evaluate the expression. There are five different possible forms of a valid postfix notation for 4 cards, where C denotes a card value and X denotes an operation: 1. CCXCCXX 4. CCXCXCX Every valid possible postfix expression will take one of these five forms. Note that when you have 4 values, it is only possible to have 3 operations in the postfix expression for it to be a valid expression. You will need to use three nested loops iterating over the ops list created for you in order to generate every possible postfix notation. Once you have generated all of these notations, evaluate them, and if an expression evaluates to 24, add it to the set of results

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!