Найти - Пользователи
Полная версия: расстановка математических знаков
Начало » Python для новичков » расстановка математических знаков
1 2
shiroi
scidam
  for combination in product():# 
в цикле в скобки надо поставить это (*[]*8) ?
 expression = ...  
а здесь объединение последовательности чисел, которую ввели, с product?
scidam
Так не интересно…



 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




This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB