Consider the following function: 1. def mystery(n: int, m: int) -> int :2. if n == 0 : # special case 13. return 04. if n == 1 : # special case 25. return m6. return m + mystery(n – 1), m) What will happen if lines #2 and #3 were swapped with lines #4 and #5?
Category: Uncategorized
Consider the following code snippet for recursive addition:…
Consider the following code snippet for recursive addition: def add(i: int, j: int) -> int : # assumes i >= 0 if i == 0 : return j else : return add(i – 1, j + 1) Identify the base-case / stopping-case in this recursive function.
This function is supposed to recursively compute x to the po…
This function is supposed to recursively compute x to the power n, where x and n are both non-negative: 1. def power(x: int, n: int) -> int:2. if n == 0 :3. ________________________________4. else :5. return x * power(x, n – 1) What code should be placed in the blank to accomplish this goal?
Consider the following recursive code snippet: def mystery(n…
Consider the following recursive code snippet: def mystery(n: int, m: int) -> int: if n == 0 : return 0 if n == 1 return m return m + mystery(n – 1, m) What value is returned from a call to mystery(3, 6)?
The following code segment is supposed to determine whether…
The following code segment is supposed to determine whether or not a string is a palindrome, meaning that it is the same forward and backward. def isPalindrome(s: str) -> bool : return palindromeHelper(s, 0, len(s) – 1) def palidromeHelper(s: str, l: int, h: int) -> bool : if h
What is displayed by the following code segment? def mystery…
What is displayed by the following code segment? def mystery(n: int) -> int: if n == 0 : return 0 else : return n % 10 + mystery(n // 10) print(mystery(-1))
Consider the following recursive code snippet: def mystery(n…
Consider the following recursive code snippet: def mystery(n: int, m: int) -> int: if n == 0 : return 0 if n == 1 : return m return m + mystery(n – 1, m) What value is returned from a call to mystery(1, 5)?
Consider the following code segment: def triangle_area (side…
Consider the following code segment: def triangle_area (sideLength: int) -> int: if sideLength == 1: return 1 return triangle_area(sideLength – 1) + sideLength print(triangle_area(5)) How many times is the triangle_area function called when this code segment executes?
Consider the triangle_area function from the textbook shown…
Consider the triangle_area function from the textbook shown below: 1. def triangle_area(sideLength: int) -> int:2. if sideLength
Consider the code for the recursive function my_print shown…
Consider the code for the recursive function my_print shown in this code snippet: 1. def my_print(n: int) -> int:2. if n == 0 :3. return 04. else :5. return n + my_print(n – 1) To avoid infinite recursion, which of the following lines of code should replace the current terminating case?