As a follow up to my concept map on assertions, here are two questions about assertions.
#1 Given the following Python code
MAX = 3
P1 = 0
P2 = 1
P3 = 2
def check_pythagoros(parameters):
# Pre-conditions
assert len(parameters) == MAX ,"Must specify 3 values"
assert parameters[P2] > 0 ,"Second Parameter must be positive integer value"
assert parameters[P3] > 0 ,"Third Parameter must be positive integer value"
assert parameters[P1] > 0 ,"First Parameter must be positive integer value"
assert parameters[P1] < parameters[P2] < parameters[P3], "Parameters must be provided in increasing order"
if (parameters[P1] * parameters[P1] + parameters[P2] * parameters[P2] == parameters[P3] * parameters[P3]):
return True
return False
What will be produced by the following statement:
print check_pythagoros([9, 15, 15.0 , ]) ?
a. Assertion raised ‘Must specify 3 values’
b. Assertion raised ‘Third parameter must be positive integer’
c. False
d. Assertion raised ‘Parameters must be provided in increasing order’
#2 The following code should print the set of prime numbers up to 100. However, due to a bug it only prints 2 and 3. Using ONLY assert statements, insert a number of assertions to find the source of the bug.
MAX = 100
number = 2
prime = True
while number < MAX:
for loop in range (number-1, 1, -1):
if number % loop == 0:
prime = False
if prime:
print number, " is a prime number"
number = number + 1