Recursion (Knowledge Quiz)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Look at this recursive program: def foo(a_str, n): if n == 0: return 0 n -= 1 if a_str[n] == 'a': return 1 + foo(a_str, n) return foo(a_str, n) What does it do? a. Returns the length of the string a_str if 'a' occurs in it, otherwise 0 b. Returns the number of times 'a' occurs in the first n letters of a_str c. Recurs endlessly and never returns d. Returns 1 if 'a' occurs in the first n letters of a_str, otherwise 0

b. Returns the number of times 'a' occurs in the first n letters of a_str

Power, part 2/2 A programmer is asked to make a function that calculates n to the power of m. This recursive function is what they come up with: def bar(n, m): return n * bar(n, m-1) We test it on the positive integers 2 and 3: print(bar(2, 3)) What change is needed for it to work? a. In the recursive call we need to call bar(n, m+1) instead of bar(n, m-1) b. We need to add a condition: if m == 0: return 1 c. No change, it works correctly already d. In the recursive call we need to call bar(n-1, m) instead of bar(n, m-1)

b. We need to add a condition: if m == 0: return 1

Sum of natural numbers, part 2/2 A programmer is asked to make a function that returns the sum of the first n natural numbers. This recursive function is what they come up with: def bar(n): if n == 1: return n return n + bar(n) We test it on the natural number 6: print(bar(6)) What change is needed for it to work? a. If the condition, n == 1, is true it should return 0 instead of n b. In the if condition, we should check if n == 0 instead of n == 1 c. In the recursive call we need to call bar(n-1) instead of bar(n) d. No change, it works correctly already

c. In the recursive call we need to call bar(n-1) instead of bar(n)

Power, part 1/2 A programmer is asked to make a function that calculates n to the power of m. This recursive function is what they come up with: def bar(n, m): return n * bar(n, m-1) We test it on the positive integers 2 and 3: print(bar(2, 3)) How will it behave? a. It will return 2 b. It will return 6 c. It will recur endlessly and never return d. It will return 8

c. It will recur endlessly and never return

Sum of natural numbers, part 1/2 A programmer is asked to make a function that returns the sum of the first n natural numbers. This recursive function is what they come up with: def bar(n): if n == 1: return n return n + bar(n) We test it on the natural number 6: print(bar(6)) How will it behave? a. It will return 1 b. It will return the sum of the first n-1 natural numbers c. It will return the sum of the first n natural numbers d. It will recur endlessly and never return

d. It will recur endlessly and never return


Kaugnay na mga set ng pag-aaral

Chapter 16 - Enlightened Absolutism

View Set

Socrative Bio 234 Exam 2 Ch. 24 Digestive System

View Set

Introduction to Data Structures and Algorithms

View Set

NURS 202 Drug routes (Advantages & Disadvantages of each)

View Set

History of the Theatre II Exam 2

View Set

Cognitive Psych Chap 4,6, 7 Quiz Questions

View Set