solve this problem as if it is on a paper ... thnx Solve the following problem o
ID: 3831970 • Letter: S
Question
solve this problem as if it is on a paper ... thnx
Solve the following problem on paper and bring your sheet of paper to your section on Thursday: Assertions. You will identify various assertions as being either always true, never true or sometimes true/sometimes false at various points in program execution. The comments in the method below indicate the points of interest. def mystery(x): y = 1 z = 0 # Point A while (x > y): # Point B z = z + x - y x = x//2 # Point C y = y * 2 # Point D # Point E return z Copy the table below onto a sheet of paper and fill it in with the words ALWAYS, NEVER or SOMETIMES.Explanation / Answer
def mystery(x):
y = 1
z = 0
#Point A
while (x > y):
#Point B
z = z + x - y
x = x // 2
#Point C
y = y * 2
#Point D
#Point E
return z
At Point A:
x > y (SOMETIMES), this is based on x value passed as parameter.
z > 0 (NEVER), z is initialized to 0, and therefore will be equal to 0.
y % 2 == 0 (NEVER) y is initialized to 1, and 1 when divided by 2 results in 1, and not equal to 0.
At Point B:
x > y (ALWAYS), Only iff x is greater than y, the loop will enter.
z > 0 (SOMETIMES), z value is being modified inside the loop, and the loop may be executed multiple times, and the value of z may be greater than 0.
y % 2 == 0 (SOMETIMES), infact for the first time the loop runs, y value is 1, and is not an even, but from there on, everytime y is being multiplied by 2 at the end of the loop, and therefore, will be even for sure.
At Point C:
x > y (SOMETIMES), x value is being modified inside the loop, so it may or may not be the case.
z > 0 (SOMETIMES), z value is being modified inside the loop, and the loop may be executed multiple times, and the value of z may be greater than 0.
y % 2 == 0 (SOMETIMES), infact for the first time the loop runs, y value is 1, and is not an even, but from there on, everytime y is being multiplied by 2 at the end of the loop, and therefore, will be even for sure.
At Point D:
x > y (SOMETIMES), x value is being modified inside the loop, so it may or may not be the case.
z > 0 (SOMETIMES), z value is being modified inside the loop, and the loop may be executed multiple times, and the value of z may be greater than 0.
y % 2 == 0 (ALWAYS), as y value is being multiplied by 2, it is definitely an even, and therefore, y % 2 == 0 is true.
At Point E:
x > y (NEVER), The loop will continue as long as x is greater than y, and will come out only if that condition fails.
z > 0 (SOMETIMES), z value is being modified inside the loop, and the loop may be executed multiple times, and the value of z may be greater than 0.
y % 2 == 0 (ALWAYS), as y value is being multiplied by 2, it is definitely an even, and therefore, y % 2 == 0 is true.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.