  Letters and numbers (Python 3.x)   Solve the TV number puzzles like a pro! Popular in Australia SBS game. This example demonstrates the power of Reverse Polish Notation (RPN) as well as the List Comprehension syntax. The answer to the puzzle on the photo is (75 * 50 - 100 - 2) / 8 - 25 = 431 Or in RPN form: 75 ^ 50 * 100 - 2 - 8 / 25 - = 431   Download source code: Numbers_Solver.py   import time class RPN_Calc: """ Implements a simple RPN calculator Handles 5 operations: enter(^), plus, minus, multiply and divide """ def __init__(self): self.Reset() return def Compute( self, Operand, Operator): self._Up() self.Stack = Operand self.Equation += str( Operand) if Operator == '^': self.Equation += ' ^ ' return self.Stack if Operator == '+': self.Stack += self.Stack self._Down() self.Equation += ' + ' return self.Stack if Operator == '-': self.Stack = self.Stack - self.Stack self._Down() self.Equation += ' - ' return self.Stack if Operator == '*': self.Stack *= self.Stack self._Down() self.Equation += ' * ' return self.Stack self.Equation += ' / ' if self.Stack <= 0 or (self.Stack % self.Stack != 0): self.Stack = -999 self._Down() return -999 self.Stack = int( self.Stack // self.Stack) self._Down() return self.Stack def Reset( self): self.Stack = [0,0,0,0,0,0] self.Equation = "" return def Result( self): return self.Stack def PrintStack( self): for i in range(5,-1,-1): print( self.Stack[i]) print() return def _Up(self): for i in range(5,0,-1): self.Stack[i] = self.Stack[i-1] return def _Down(self): for i in range(1,5): self.Stack[i] = self.Stack[i+1] self.Stack = 0 return class Number_Solver: """ Solves the classic "Letters and Numbers" game """ def __init__(self, n=6): self.RPN = RPN_Calc() nums = "" for i in range(n): nums += str(i) self.Permutations = self.Create_Permutations( nums) self.Algorithms = [] for p in self.Permutations: self.Algorithms += [p + '^' + alg for alg in self.Create_Algorithms( p[1:])] return def Create_Permutations( self, nums): if len(nums)==1: return [nums] tmp = [] for c in nums: tmp += [c+num for num in self.Create_Permutations( nums.replace(c, ''))] return tmp def Create_Algorithms( self, permutation): if len(permutation) == 1: return [permutation + o for o in "+-*/"] tmp = [] c0 = permutation algorithms = self.Create_Algorithms( permutation[1:]) for o in "^+-*/": start = c0 + o tmp += [ start + alg for alg in algorithms] return tmp def RunAlgorithm( self, operands, algorithm, target): self.RPN.Reset() for i in range(0, len(algorithm), 2): operand = algorithm[i] operator = algorithm[i+1] r = self.RPN.Compute( operands[int(operand)], operator) if r < 0: break if r == target: break return self.RPN.Result() def SolveTarget( self, operands, target): self.Target = target self.Result = 0 self.Mindiff = target self.Solution = "No solution found" for a in self.Algorithms: r = self.RunAlgorithm(operands, a, target) if r < 0: continue diff = target - r if diff < 0: diff = -diff if diff == 0: self.Mindiff = 0 self.Result = target self.Solution = self.RPN.Equation.strip() return if diff > self.Mindiff: continue self.Mindiff = diff self.Result = r self.Solution = self.RPN.Equation.strip() return def ReportSolution( self): if self.Mindiff == 0: print( "Exact solution found:") print( self.Solution + ' = ' + str(self.Result)) return if self.Mindiff < 10: print( "Solution within the scoring range:") else: print( "Cannot do any better:") print( self.Solution + ' = ' + str(self.Result) + ' (' + str(self.Mindiff) + ' off)') return ns = Number_Solver() #for i, p in enumerate(ns.Permutations): # print(i+1, p) #for i, a in enumerate(ns.Algorithms): # print(i+1, a) print("Solving Letters and Numbers by RPN calc!") print() while True: nums = input( "Input numbers (with space): ") if len(nums) == 0: break #nums = "10 25 1 2 9 3" targ = input( "Input Target: ") if len(targ) == 0: break #targ = "253" try: operands = [int(n) for n in nums.split(' ')] if len(operands) < 6: print("Not enough numbers") continue target = int(targ) except: print("Must be an Integer") continue t = time.process_time() ns.SolveTarget(operands, target) ns.ReportSolution() print( "Solved in {:.3f} seconds".format(time.process_time() - t)) print() (C) M.Yakimov, 2006 