1 24 Point Introduction
Take a deck of cards, the size of the Queen (the first practice can also take j/q/k), the remaining 1~10 40 cards (The following 1 in lieu of a). Arbitrarily extract 4 cards (known as a card set), add, subtract, multiply, divide (can be parentheses) the number on the board to calculate 24. Each card must and can only be used once. If the card drawn is 3, 8, 8, 9, then the formula for (9-8) x8x3=24.
Two common ways to solve this problem:
1. Expression parentheses. You can choose: no parentheses, 1 brackets, 2 parentheses.
2. Inverse Polish expression.
2 Inverse Polish expression
The inverse Polish expression is also called the suffix expression. In the usual expression, the two-dollar operator is always placed between the two operands associated with it, and this notation is also known as infix notation. Polish logic J.lukasiewicz in 1929 another way of expressing expressions, in which each operator is placed after its operand, is called a suffix.
2.1 Explanation
In inverse Polish notation, the operator is placed after the operand. For example, when expressing "three plus four", write "3 4 +" instead of "3 + 4". If there are multiple operators, the operator is placed after the second operand, so the conventional infix notation of "3-4 + 5" in inverse Polish notation "3 4-5 +": first 3 minus 4, plus 5. One advantage of using inverse Polish notation is that you do not need to use parentheses. For example, the infix notation "3-4 * 5" and "(3-4)" is not the same, but the suffix notation in the former write "3 4 5 *-", the Unambiguous Expression "3 (4 5 *) −", the latter write "3 4-5 *".
The interpreter of the inverse Polish expression is generally stack-based. The interpretation process is generally: the operand into the stack, when the operator is encountered, the operand out of the stack, evaluated, the result into the stack; once again, the top of the stack is the value of the expression. The evaluation of inverse Polish expressions is therefore easy to implement using a stack structure and can be evaluated quickly.
Normal expression inverse Polish expression a+b---> a B + A + (B-C)---> A b C-+ A + (B-C) *d---> A b c-d * + a+d* (B-C)---> a d b C-* +
2.2 Examples
Infix expression "5 + ((1 + 2) * 4) −3" writing
-
5 1 2 + 4 * + 3−
The following table shows the process of evaluating the inverse Polish expression from left to right, and the stack bar gives the median value for the tracking algorithm.
When the calculation is complete, there is only one operand in the stack, which is the result of the expression: 14
2.3 Python Implementation
1 #--rpn.py--2 fromItertoolsImportPermutations#Full arrangement3 fromFractionsImportFraction#Fractional Operations4 fromOperator Omport Add, Sub, mul, div#function +-x/5 6Card = map (str, [5, 5, 5, 1])#Enter 4 cards to save as characters7Dict_op = {'+': Add,'-': Sub,'*': Mul,'/':d IV}#to convert an operator to a calling function8OP = ['+','-','*','/'] * 3#arithmetic, 4 cards require 3 operations9OP1 = List (permutations (OP, 3))#op takes three of all permutations, resulting in a list of resultsTenOP2 = Map (LambdaX:list (x), OP1)#list elements are converted from tuples to lists OneOP3 = Map (Lambdax:sorted (x), OP2)#Sort AOPS = Set (Map (LambdaX:tuple (x), OP3))#list to set, go to heavy - - #Inverse Polish expression implementation the defRPN (LST): -stack = []#Initialize Stack - forIinchLst:#iterating through the list of passed-in expressions - ifI.isdigit ():#if it is a number, go directly to the stack + stack.append (Fraction (I.strip ())) - Else: + ifIinchDict_op:#if operator, two elements are removed for operation A Try: atA =Stack.pop () -b =Stack.pop () - Stack.append (Dict_op (b, a)) - except: Break - ifLen (Stack) = = 1:#if one element is left in the last stack, the result is output - returnStack[0] in - #Calculate The to foroperatorinchOps#iterate through each combination of operators +Space = [] -Space = card + list (operator)#combination cards and operators thes = set (permutations (space))#Arranging card operators all possible * forJinchS: $ ifRPN (List (j)) = = 24:Panax Notoginseng PrintJ - the Print 'Game over'
24-point Computing program [Python]