Nov-08-2021, 10:33 PM
Recursive Python
I am trying to learn recursive programming in Python by reading a GitHub document titled “Understanding Recursion Using Python ” subtitled, “A shamelessly verbose guide to the wonders of recursion”. The document is contributed by -- © Copyright 2019, panopticonopolis.
I have done the suggested exercises which work as desired, but I would very much like to see how others are coding the same exercises. So if possible I would like to correspond with other students who are using this document as a learning tool.
I am posting the last exercise I coded. It takes a string of digits and converts them to an integer sum using recursion. The code works, but I think there is likely a more elegant, efficient way to do it. The area where I think this code fails is that it doesn’t really use the post recursive code to arrive at the answer. I would appreciate a hint to fix this. The code is heavily annotated per the exercise instructions.
I am using Python 3.9 with Pycharm Community Edition 2021.2 running Windows 10 Home.
I am new to python-forum. Is this post correctly posted to this forum?
I am trying to learn recursive programming in Python by reading a GitHub document titled “Understanding Recursion Using Python ” subtitled, “A shamelessly verbose guide to the wonders of recursion”. The document is contributed by -- © Copyright 2019, panopticonopolis.
I have done the suggested exercises which work as desired, but I would very much like to see how others are coding the same exercises. So if possible I would like to correspond with other students who are using this document as a learning tool.
I am posting the last exercise I coded. It takes a string of digits and converts them to an integer sum using recursion. The code works, but I think there is likely a more elegant, efficient way to do it. The area where I think this code fails is that it doesn’t really use the post recursive code to arrive at the answer. I would appreciate a hint to fix this. The code is heavily annotated per the exercise instructions.
I am using Python 3.9 with Pycharm Community Edition 2021.2 running Windows 10 Home.
I am new to python-forum. Is this post correctly posted to this forum?
summ = 0 frame = 0 """ s is the test string """ def stoi(s, n): global summ, frame if frame == 0: print() print(' in global frame=0 s= string' , s, ' summ=0 n=0' ) if n < len(s): frame += 1 print() print(' in recur frame =', frame) print(' rcvd: s= ', s, ' summ=', summ, ' n= ', n) summ = int(s[n]) + summ print(' passed: s= ', s , ' summ=', summ, ' n= ', n+1) # the base case is reached when n=len(s) elif n == len(s): print() print(' in base frame', frame+1, ' s= ', s , ' summ=', summ, ' n= ', n+1) else: print() print(' in outcur frame =', frame) print(' s= ', s,' summ=', summ, ' n= ', n, ) frame -= 1 if frame == 0: return stoi(s, n + 1) stoi(str(473), 0)
Output:C:\Users\sfn22\AppData\Local\Programs\Python\Python39\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 50168 --file "C:/Users/sfn22/PycharmProjects/Pythonbook_2108/Chpt. 5/convert a string of digits to an integer sum.py"
Connected to pydev debugger (build 212.4746.96)
in global frame=0 s= string 473 summ=0 n=0
in recur frame = 1
rcvd: s= 473 summ= 0 n= 0
passed: s= 473 summ= 4 n= 1
in recur frame = 2
rcvd: s= 473 summ= 4 n= 1
passed: s= 473 summ= 11 n= 2
in recur frame = 3
rcvd: s= 473 summ= 11 n= 2
passed: s= 473 summ= 14 n= 3
in base frame 4 s= 473 summ= 14 n= 4
in outcur frame = 3
s= 473 summ= 14 n= 4
in outcur frame = 2
s= 473 summ= 14 n= 5
in outcur frame = 1
s= 473 summ= 14 n= 6
Process finished with exit code 0