Уведомления

Группа в Telegram: @pythonsu

#1 Ноя. 12, 2017 17:01:26

shiroi
Зарегистрирован: 2017-10-25
Сообщения: 23
Репутация: +  0  -
Профиль   Отправить e-mail  

расстановка математических знаков

scidam

  for combination in product():# 
в цикле в скобки надо поставить это (*[]*8) ?
 expression = ...  
а здесь объединение последовательности чисел, которую ввели, с product?

Офлайн

#2 Ноя. 13, 2017 08:40:40

scidam
Зарегистрирован: 2016-06-15
Сообщения: 288
Репутация: +  35  -
Профиль   Отправить e-mail  

расстановка математических знаков

Так не интересно…



 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




Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version