Reasoning about Code
Think about the following pieces of code and diagnose them on paper rather than simply typing them into the interpreter.
Conditionals
- Which choice(s) assigns the correct, i.e., higher value, for higher speeds to the variable
fee. If one is not correct, explain what value is finally assigned tofee.if speed > 35: fee = 20
if speed > 50: fee = 40
if speed > 75: fee = 60if speed > 75: fee = 60
if speed > 50: fee = 40
if speed > 35: fee = 20if 35 < speed <= 50: fee = 20
if 50 < speed <= 75: fee = 40
if 75 < speed: fee = 60
- To which of the following is this expression equivalent? If one is not correct, give values for
xandythat were True in the original, but not in the revised version.not ((x > y) and (y <= 3))
(x > y) and (y <= 3)(x > y) or (y <= 3)(x < y) or (y >= 3)(x <= y) or (y > 3)(x <= y) and (y > 3)
- A year is a leap year when it has 366 days instead of 365 days. In the international Gregorian calendar a year is a leap year according to the following, correct but perhaps poorly worded, rules as obtained from timeanddate.com:
- Every year divisible by 4 is a leap year
- But every year divisible by 100 is NOT a leap year
- Unless the year is also divisible by 400, then it is still a leap year
This means 1800, 1900, and 2100 are not leap years but 2000 and 2004 are leap years.
Which of the following functions correctly identitfies a leap year according to the rules above. If a function is not correct, provide a value for
yearthat is misidentified.def is_leap1 (year): if year % 400 == 0: return True if year % 100 == 0: return False if year % 4 == 0: return True return False def is_leap2 (year): return year % 400 == 0 or (year % 4 == 0 and year % 100 != 0) def is_leap3 (year): if year % 100 == 0: return False if year % 400 == 0 or year % 4 == 0: return True return False
Loops
- When writing loops, it is important to understand how the values within the change to know when the loop will stop. Given the code below, complete the table of assertions as being either always True, never True, or sometimes True/sometimes False at the points of interest noted in the comments.
x = 10 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, inside loop # Point E, outside loopx > y z > 0 y % 2 == 0 Point A Point B Point C Point D Point E - Consider the function
findLongest(with line numbers added for reference), which is intended to find the longest consecutive block of the valuetargetoccurring in the listvalues. However, it does not work as intended.For example, the call
findLongest([7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 16, 10, 10], 10)correctly returns 3; but the callfindLongest([7, 10, 10, 15, 15, 15, 15, 10, 10, 16, 10, 10, 10], 10)does not return 3.def findLongest (values, target): 1 lengthCount = 0 2 maxLength = 0 3 4 for v in values: 5 if v == target: 6 lengthCount = lengthCount + 1 7 else: 8 if lengthCount > maxLength: 9 maxLength = lengthCount 10 lengthCount = 0 11 12 if lengthCount > maxLength: 13 maxLength = lengthCount 14 15 return maxLengthWhat is wrong with the function and what can be changed to fix it?