Python recursive function storing variable values

I am cleaning up some old old algorithms and doing it with python since I use it more often these days.

I am facing a problem when working with a recursive function; where the variable gets reset every time the recursive function itself calls:

def recursive_me(mystring):
    chars = len(mystring)
    if chars is 0:
        print("Done")
    else:
        first = int(str[0])
        total = + first
        print(total)
        recursive_me(mystring[1:])

recursive_me("4567")

I am doing here to get a string of numbers; take the first, convert it to int; and run the function recursively again, so I can take one digit in time from the string and sum all the values.

Ideally, the output should show the total, while it adds all the digits (4 + 5 + 6 + 7), although when the recursive function is called for the first time, the reset function is the total.

- ?

+4
5

, :

def recursive_me(mystring):
    if mystring: # recursive case
        return int(mystring[0]) + recursive_me(mystring[1:])
    else:        # base case
        return 0

def recursive_me(mystring, total = 0):
    if mystring: # recursive case
        return recursive_me(mystring[1:], total + int(mystring[0]))
    else:        # base case
        return total

Python, .

, :

def recursive_me(mystring, total = 0):
    if mystring: # recursive case
        newtotal = total + int(mystring[0])
        print(newtotal)
        return recursive_me(mystring[1:], newtotal)
    else:        # base case
        return total

4
9
15
22
22 # this is the return value; previous output is from `print()`
+7

. , .

python

t=raw_input()
print reduce(lambda a, b: a+b, map(int,t))

.

+1

: , . .

, . OP.

def recursive_me(mystring, total=0):
    chars = len(mystring)
    if chars is 0:
        print("Done")
        return total
    else:
        first = int(mystring[0])
        total += first
        print(total)
        recursive_me(mystring[1:], total)

, , . 0, .

int . , str[0]. str - python, "str ".

, str []. , 1[0], 1 . "[]" , (, ).

. total = + first, , , - +=, a = a+b.

, "python", "total". , , , "" , , .

recursive_me, . , @uselpa; , python , +:

return int(mystring[0]) + recursive_me(mystring[1:])

( recursive_me("4567"))

return int(4)+recursive_me("567")
return int(4)+int(5)+recursive_me("67")
....
return int(4)+int(5)+int(6)+int(7)+0

python , , python , ( , ).

+1

:

  • (0 ), .
  • total = + first total first, first total. total += first.
  • "" - "" , . .

:

def recursive_me(mystring):
    if not mystring: # True if mystring is empty
        return 0
    return int(mystring[0]) + recursive_me(mystring[1:])

print(recursive_me("4567")) # 22
0

, LEGB,

def sum_str(mystring):
    def recursive_me(pos):
        cur_char = int(mystring[pos])
        if pos:
            return cur_char + recursive_me(pos-1)
        else:
            return cur_char
    return recursive_me(len(mystring)-1)

s = '4567'
print('summing', s)
print(sum_str(s))

,

def sum_str(mystring):
    def recursive_me(itx):
        try:
            cur_char = int(next(itx))
            return cur_char + recursive_me(itx)
        except StopIteration:
            return 0

    return recursive_me(iter(mystring))

,

summing 4567
22
0

Source: https://habr.com/ru/post/1622144/


All Articles