Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python find permutations of operators between each digit in a given string of digits will result in a particular answer

from itertools import permutations
numbers   = ["1","9","8","2"]
target    = 24
operators = ["+","-","*","/"]
for values in permutations(numbers,len(numbers)):
    for oper in permutations(operators,len(numbers)-1):
        formula = "".join(o+v for o,v in zip([""]+list(oper),values))
        if eval(formula) == target: print(formula,"=",target)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Python #find #permutations #operators #digit #string #digits #result #answer
ADD COMMENT
Topic
Name
7+1 =