Chapter 13

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How is a problem usually reduced with a recursive function?

*The base case is identified first to end the recursion *One recursive case is designed, so as to loop the problem, again and again through the same function *In the recursive case the tree (Recursive tree) deepens its roots every time with a new set of arguments. Division of the problem into subproblems.

What is a recursive case?

After finding the base case, determine a way for solving the problems in all conditions by using recursion.

If n=0 then factorial(n)=1 if n>0 then factorial(n)=n X factorial(n-1) If you were designing a function from these rules, what would the base case be? What would the recursive case be?

Base: If n=0 then factorial(n)=1 Recursive: If n=0 then factorial(n)=n*factorial(n-1)

When a function explicitly calls itself it is called recursion.

Direct

What is direct recursion? What is indirect recursion?

Direct Recursion: If a function is calling itself that is called direct recursion. Indirect Recursion: If a function is calling some function and in that function if there is call to the caller function from the called function.

In the base case, a recursive method calls itself with a smaller version of the original problem.

False

It is not necessary to have a base case in all recursive algorithms.

False

Some problems can be solved through recursion only

False

What causes a recursive algorithm to stop calling itself?

For stopping the recursive algorithm by itself base case should be reached.

When function A calls Function B, which calls function A it is called

Indirect

What is a base case?

Is that case which terminates the loop of the recursive function and hence avoiding the chance of becoming an infinite recursion. is nothing but the normal case for which terminates the loop of the recursive function and hence avoiding the chance of becoming an infinite recursion.

The part of a problem that is solved with recursion is the case

Iterative case

It is said that a recursive algorithm has more overheard than an iterative algorithm. What does this mean?

Overhead: Iterative algorithms are more efficient than the recursive algorithms. The reason behind this is that, if the process calls a function then it requires many actions which are performed by the computer. In these action some action are such as allocating memory for variables, and stores the location of the program where the control returns after the termination of the function. These actions which are performed in recursive algorithm at the time of function calling are known as overhead. These overhead takes place with each call of the function.

When recursion is used to solve a problem, why must the recursive function call itself to solve a smaller version of the original problem?

Recursion lessens the amount of work done. The work is divided into chunks every time the function is called There by dividing the big problem into smaller sub problems and constructing base cases from them, make the program to perform really better in cases of the large data.

An algorithm that uses a loop will usually run faster than an equivalent recursive algorithm

True

A recursive function

calls itself

Ackermann's Function Design a function ackermann(m,n), which solves Ackermann's function. Use the following logic in you function: if m=0 then return n+1 if n=0 then return ackermann(m-1,1) Otherwise, return ackermann(m-1,1,ackermann(m,n-1))

def ackermann(m,n) if(m==0): return(n+1) if(n==0): return(ackermann(m-1,1)) if(m!=0 and n!=0): return(ackermann(m-1,ackermann(m,n-1))))

Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n.

def main(): num=int(input('Enter an integer: ')) recursive_function(num) def recursive_function(n): if n>0: recursive_function(n-1) print(n) main()

The following function uses a loop. Rewrite it as a recursive function that performs the same operation. def traffic_sign(n): while n>0: print('No Parking') n=n-1

def main(): number=int(input('Enter a nonnegative integer:')) traffic_sign(number) def traffic_sign(n): if(n>0): traffic_sign(n-1) print('No Parking') main()

Largest List Item Design a function that accepts a list as an argument, and returns the largest value in the list. The function should use recursion to find the largest item.

def maxi(a,b): if(a>b): return a else: return b def large(a): if(len(a)>2): return maxm elif(len(a)==2): return(max(a[0],a[1])) elif(len(a)==1): return(a[0])

Design a reursive function that accepts two arguements into the parameters x and y. It should return values x*y example: 7*4= 4+4+4+4+4+4+4

def multiply(x,y): if (x==0): return 0 print("+",end="") return y + multiply (x-1, y) def main(): x=int(input("enter first num: ")) y=int(input("enter second num: ")) print(x,"x",y,"y","= ",end="") print("=",multiply(x,y)) main()

Recursive Power Method Design a function that uses recursion to raise a number to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer.

def recur_pow(x,y): if(y==0): return 1 return x*recur_pow(a,y-1)

Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks.

def show_asteriks(n): if(n>0): #base case show_Asteriks(n-1) #Recursive num=n; str='' while(num>0): str+='*' num-=1 print(str)

Recursive List Sum Design a function that accepts a list of numbers as an argument. The function should recursively calculate the sum of all the numbers in the list and return that value.

def sum_list(a): if(len(a)>1): return(a[0]+sum_list(a[1:len(a)])) if(len(a)==1): return(a[0])

Sum of Numbers Design a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. Ex: input 50 output 1,2,3,4,...50 to calculate the sum

def sum_num(n): if (n==1): return 1 else: return(n+sum_num(n-1))

A function is called once from a program's main function, and then it calls itself four times. The depth of recursion is.

four This is a depth recursion.

In program 13-2, presented in earlier in this chapter, what is the base case of the message function

if times > 0

Any problem that can be solved recursively can also be solved with a

loop

What will the following program display? def main(): num=0 show_me(num) def show_me(arg): if arg<10: show_me(arg+2) else: print(arg) main()

output: 10

A recursive algorithm must in the recursive case

reduce the problem to a smaller version of the original problem

Action taken by the computer when a function is called, such as allocating memory for parameters and local variables, as referred to as

set up

A recursive algorithm must in the base case

solve the problem without recursion


Set pelajaran terkait

Chapter 55 Clients W/Male Reproductive Disorders

View Set

Alterations of Electrolyte Balance

View Set

Chapter 19 Magnetism (for final)

View Set

Constitution Test Review- Constitution Principles

View Set

The Scout Law - Cub Scouts - Boy Scouts of America

View Set

chapter 52 Removable Prosthodontics

View Set

Chambers of the Heart/Valve (The Cardiovascular System: Heart)

View Set

CHAPTER 4 EMPLOYEE TRAINING AND DEVELOPMENT

View Set

Chapter 31: Abdominal and Genitourinary Injuries

View Set