Question: Question 1 Consider the triangleArea function from the textbook shown below: def triangleArea(sideLength) : if sideLength = 4 : tSquare(width / 2, x, y, canvas)tSquare(width
Question 1
Consider the triangleArea function from the textbook shown below:
def triangleArea(sideLength) :
if sideLength <= 0 :
return 0
if sideLength == 1 :
return 1
smallerSideLength = sideLength - 1
smallerArea = triangleArea(smallerSideLength)
area = smallerArea + sideLength
return area
What will happen if line #6 is changed to: smallerSideLength = sideLength
Question options:
Infinite recursion will occur when sideLength is equal to 0.
Infinite recursion will occur when sideLength is equal to 1.
Infinite recursion will occur when sideLength is greater than or equal to 2.
Infinite recursion will occur for any value of sideLength.
Question 2
How many squares are drawn by the following code segment?
def tSquare(width, x, y, canvas) : canvas.drawRect(x, y, width, width) if width >= 4 :
tSquare(width / 2, x, y, canvas)tSquare(width / 2, x + width / 2, canvas)tSquare(width / 2, x, y + width / 2, canvas) tSquare(width / 2, x + width / 2, y + width / 2, canvas)
# Code to setup the canvas has been omitted tSquare(0, 0, 8, canvas)
Question options:
1
4
5
8
Question 3
Complete the code for the myFactorial recursive function shown below, which is intended to compute the factorial of the value passed to the function:
1. 2. 3. 4. 5.
def myFactorial(n) :if _____________________________ :
return 1
else :
return n * myFactorial(n - 1)
Question options:
n == 1
(n - 1) == 1
n * (n - 1) == 1
myFactorial(n) == 1
Question 4
Recall the permutations function from Section 11.5 in the textbook.
def permutations(word) : result = []
if len(word) == 0 : result.append(word) return result
else:for i in range(len(word)) :
shorter = word[ : i] + word[i + 1 : ] shorterPermutations = permutations(shorter) for string in shorterPermutations :
result.append(word[i] + string) return result
What is the base case for this function? Question options:
The empty list
Any list containing exactly one character
The empty string
Any string containing exactly one character
Question 5
particular algorithm visits n3 + nlog(n) + n! elements in order to perform its task on a list of n elements. Which big-Oh expression best describes the growth rate of this algorithm?
Question options:
O(n)
O(n3)
O(n!)
O(nlog(n)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
