You may not run any code in Part 1. Once you move to Part 2, you may not return to Part 1.
Note: The Free Response Previews are previews of the question you must solve in Part 2, so that you can choose when to move on from Part 1. You will be able to run code in the testing environment in Part 2. Remember, you will not be able to return to Part 1 once you have moved to Part 2.
Answer SA1 on your physical quiz paper.
In words (not code) describe the functionality of the base case of drawSierpinskyTriangle()
from the notes.
Write a recursive backtracking function distList(n)
that takes an integer n
and returns a list of size 2n
which contains all the numbers from 1 to n twice such that if x is in the list
then the two xs are separated by exactly x other numbers. If such a list cannot be
generated, return None
.
distList(3) returns [3, 1, 2, 1, 3, 2] distList(4) returns [4, 1, 3, 1, 2, 4, 3, 2] distList(2) returns None
Notes/Hints:
def distList(n):
return 42
def testDistList():
print('Testing distList...', end='')
result1 = distList(3)
assert(result1 == [3, 1, 2, 1, 3, 2] or
result1 == reversed([3, 1, 2, 1, 3, 2]))
result2 = distList(4)
assert(result2 == [4, 1, 3, 1, 2, 4, 3, 2] or
result2 == reversed([4, 1, 3, 1, 2, 4, 3, 2]))
assert(distList(2) is None)
assert(distList(6) is None)
print('Passed!')
testDistList()