A
A
Andrey Nosov2016-05-26 22:04:37
Python
Andrey Nosov, 2016-05-26 22:04:37

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

It takes a list of 6 digits (numbers in string representation) .
As planned, this function should use recursion to go through all possible combinations of characters (or their absence) , place between numbers and check if the resulting expression is equal to 100.
The interpreter returns this:
Traceback (most recent call last):
File "D:\Dmitry\ Documents\Python\programs\def.py", line 46, in
main()
File "D:\Dmitry\Documents\Python\programs\def.py", line 42, in main
numeric_expression = check_possibility(number_list)
File "D :\Dmitry\Documents\Python\programs\def.py", line 32, in check_possibility

The last line is duplicated endlessly.
I suspect that everything is wrong in this function. Could you explain how I can create the correct function (or even fix the old one) .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tsarevfs, 2016-05-26
@tde283

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)])

ps. ** dangerous operation will take a very long time to count if large arguments are encountered. python counts in long arithmetic for integers.

#
#algooptimize #bottize, 2016-05-26
@user004

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 question

Ask a Question

731 491 924 answers to any question