Related questions
Concept explainers
1. You are to create a well-formed Python recursive function (i.e, there must be a stopping/base case and the recursive case must present a smaller problem), along with a call to that function.
2. Trace your function and call. Remember that you must keep track of memory for each function call and provide output.
How would I trace a function? and keep track of its memory for each function call?
Tracing a function and keep tracking its memory:
- The below python program computes the sum of natural numbers using recursive function.
- The value of num is passed to the function "sum".
- If the num value is 1 or 0, it return the n value to main. It the base case for the program.
- Now compute the sum of natural number using a recursive call “res=sum+sum(num-1)”.
- The resource library is used to keep the memory size.
- Then print the size of memory block and sum of natural numbers.
PROGRAM:
#Import module
import resource
#Define the function
def sum(num):
#Check if n is either 1 or 0/base case
if num == 1 or num == 0:
#Return n
return num
#Update num with recursive function call
res = num+sum(num-1)
#Keep tracking the memory block size
print ("Size of allocated memory block := ", resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
#/Print the sum
print ("Sum of first %d natural numbers := %d\n" %(num, res))
#Return value
return res
#Call the function
sum(5)
Step by stepSolved in 4 steps with 2 images
- 1) Write a recursive void function that has one parameter that is a positive integer and that writes out that number of asterisks (*) to the screen, all on one line. How do I do this in python?arrow_forwardThere are two important parts to every simple recursive function: the base case, and the recursive call that makes progress towards the base case. Something that can go wrong with recursion when it is used incorrectly is a stack overflow. Explain two different ways that a recursive function could be written incorrectly that could lead to stack overflow. Hint: one has something to do with the base case, and the other with the recursive call. 1. Enter your answer here 2. Enter your answer herearrow_forwardIn Python: Which of the following is true about recursive functions? Can make some repetitive problems easier to solve and understand. Require less memory than non-recursive functions Faster to execute than non-recursive functions can only be called from the main functionarrow_forward
- In C++: Explain and show how to use the recursive function int gcd (inta, int b).arrow_forwardDO NOT COPY ANSWER FROM ANOTHER QUESTION LEAVE IF you cannot do it perfectly. Otherwise, I will not only DOWN the vote but also REPORT you to the official Chegg team.I won't stop calling Chegg until they really pay attention to this trash behavior. Write a structurally recursive function (prefix->postfix exp) that takes as input an expression in the same little language as Problem 4. prefix->postfix returns an expression of the same form as its input, except that all function applications have been reversed. Finish this Racket code below to answer the question above.(define prefix->postfix(lambda (exp)))arrow_forward‘Write a function nesting(), which takes an arbitrary number of parameters, and returns a list containing the arguments. Write another function unnesting(), which takes a list L as the parameter and retums a 1D list. Notice that, you should be able to use loops to solve thisproblem and should not use recursive functions (which we will not cover in this course). ‘Write assertions to test the two functions.For example nesting(1, nesting(2, 3, nesting(4, 5, [6])), [7, 8], 9)unnesting([1, [2, 3, [4, 5, [6]]], [7. 8], 9]) (1, [2, 3, [4, 5, [6]]], [7, 8], 9][1, 2,3, 4, 5, 6, 7, 8 9]arrow_forward
- Answer the given question with a proper explanation and step-by-step solution.arrow_forward9. Explore the concept of recursion in user-defined functions, highlighting its advantages and potential pitfalls in programming.arrow_forwardIN PYTHON, Using recursion: ask the user for the length of a square and the character they want to use. Draw the square (similar to mini assignment #15 in functions part 3). Once the user creates the square with their specification, ask them if they want it to be smaller or larger and how many times. Draw all the squares growing in size to the specified range.arrow_forward
- What is the purpose of the base case in a recursive function? A. The base case starts off the recursive process. B. The base case defines the basis for the function calling itself. C. The base case is what stops the recursive process. D. All of the above are true. E. None of the above are true.arrow_forwardHow is it regulated that a recursion function be called several times? What type of command and control structure is used here?arrow_forwardI'm stuck on this question and I don't know how I should be approaching this. What should I do? Our focus is on pointers and dynamic arrays through C++arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education