import itertools def calculate(numbers): ops = ['+', '-', '*', '/'] questions = [''] answers = [] tmp = [] for idx, number in enumerate(numbers): for question in questions: if idx == len(numbers)-1: tmp.append(question+number) else: for op in ops: tmp.append(question+number+op) questions = tmp tmp = [] for question in questions: if eval(question) == 10: print question try: if eval(str(eval(question[0:7])) + question[7] + str(eval(question[8:]))) == 10: print '(' + question[0:7] + ')' + question[7] + question[8:] except: pass try: if eval(str(eval(question[0:11])) + question[11] + str(eval(question[12:]))) == 10: print '(' + question[0:11] + ')' + question[11] + question[12:] except: pass try: if eval(str(eval(question[0:3])) + question[3] + str(eval(question[4:]))) == 10: print question[0:3] + question[3] + '(' + question[4:] + ')' except: pass numbers = ['1.0', '1.0', '5.0', '8.0'] permutations = list(itertools.permutations(numbers)) for permutation in permutations: calculate(permutation)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question