Answer the question
In order to leave comments, you need to log in
How to use recursion in Python?
Good evening.
I know that laying out large pieces of code is disgusting, but otherwise it will not work. There is a function:
def check_possibility(figures, signs = [], is_100 = False):
for sign in ('', '+', '-', '*', '//', '**'):
signs.append(sign)
if len(signs) == 5:
numeric_expression = ''
numeric_expression += figures[0]
for i in range(5):
numeric_expression += signs[i]
numeric_expression += figures[i + 1]
result = eval(numeric_expression)
if result == 100:
return numeric_expression
else:
return False
elif is_100 == False:
is_100 = check_possibility(figures, signs, is_100)
return is_100
Answer the question
In order to leave comments, you need to log in
def check_possibility(figures, signs = [], is_100 = False):
for sign in ('', '+', '-', '*', '//'):
new_signs = signs[:]
new_signs.append(sign)
if len(signs) == 5:
numeric_expression = ''
numeric_expression += figures[0]
for i in range(5):
numeric_expression += signs[i]
numeric_expression += figures[i + 1]
result = eval(numeric_expression)
if result == 100:
return numeric_expression
else:
return False
elif is_100 == False:
is_100 = check_possibility(figures, new_signs, is_100)
return is_100
print check_possibility([str(n) for n in range(2, 8)])
signs is passed as a copy or by reference?
Take a look under the debugger, and the code is good, I took it and ran it, (I can’t yet)
In this code, I don’t see iteration of a different sequence of these characters
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question