Форум сайта python.su
scidam
for combination in product():#
expression = ...
Офлайн
Так не интересно…
import ast from itertools import product number = 100 for combination in product(*[['+', '-', '']] * 8): expression = '1'+''.join(sum([[x, y] for x, y in zip(combination, '23456789')], [])) print('Trying the expression:', expression) answer = ast.literal_eval(expression) # tested in Python 3.5.x, import ast first! if answer == number: # compare the result and the target value print('Target expression is: ', expression) break else: #see for-else docs for details... print('Sorry... Target expression wasnt found.')
..... Trying the expression: 1+2+3+4567-89 Trying the expression: 1+2+3+45678+9 Trying the expression: 1+2+3+45678-9 Trying the expression: 1+2+3+456789 Trying the expression: 1+2+3-4+5+6+7+8+9 Trying the expression: 1+2+3-4+5+6+7+8-9 Trying the expression: 1+2+3-4+5+6+7+89 Trying the expression: 1+2+3-4+5+6+7-8+9 Trying the expression: 1+2+3-4+5+6+7-8-9 Trying the expression: 1+2+3-4+5+6+7-89 Trying the expression: 1+2+3-4+5+6+78+9 Target expression is: 1+2+3-4+5+6+78+9
Офлайн