S22 Quiz 1 version B
Part 1: CTs and Fill-In-The-Blank
You may not run any code in Part 1. Once you move to Part 2, you may not return to Part 1.
- CT1: Code Tracing [15pts]
Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.
def p(a, b): return abs(a - b) + (abs(b - a) if (a == b) else a*b) def q(a, b, c): if (a > b): return p(a, b+c) a += b if (a > c): return p(a+b, c) else: c %= b return p(a+c, b) def ct1(a, b, c): print(q(a, b, c)) print(q(b, c, a)) print(q(c, a, b)) ct1(3, 5, 2) # hint: prints 3 total lines
- CT2: Code Tracing [15pts]
Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.
Hint: From the notes, remember that f-strings evaluate whatever is inside {}. So, print(f'{max(3, 4)} is bigger than {min(3, 4)}') prints '4 is bigger than 3'def f(x): print(f'f({x})') x = (5*x)%4 return g(x+3) def g(x): print(f'g({x})') x += 1 return x**2//10 def ct2(x): print(g(f(g(x))) + x) print(ct2(4)) # hint: prints 6 total lines
- Fill-In-The-Blank: parallelLines(i, j, k, M, B) [20pts]
For this problem, you are given two lines. The first line is in this form:
i = jx + ky The second line is in this form:
y = Mx + B Complete the function parallelLines(i, j, k, M, B) that takes five numbers describing two lines, one in the first form and one in the second form, and returns True if the two lines are parallel and False otherwise. Note that two lines are parallel if they have the same slope but they are NOT the exact same line. We have provided some of the function for you already. Instead of writing the function from scratch, fill in the 6 blanks so that this function works properly. Note that you may not run this code. Hint: You may want to convert the first line into slope-intercept (the second form). Also, beware zero-division!def parallelLines(i, j, k, M, B): return (not (almostEqual(__________, ____________) or (almostEqual(____________, ____________))) and almostEqual(__________, _____________))
- Free Response: isNearlyFactorish [50pts]
You will answer this question in Part 2. We will say that a digit k is a "near-factor" of n if it is positive integer and exactly 1 away from a real factor of n. In other words, if k is a positive integer and either k+1 or k-1 is a factor of n, we claim k is near-factor of n. Also, a value n is "nearly factorish" (a coined term) if it is a positive integer between 100 and 999 inclusive (so with exactly 3 digits) where each digit is a near-factor of the number. With this in mind, you will write the function isNearlyFactorish(n) that takes any Python value n (including non-integers) and without crashing returns True if n is factorish and False otherwise.
Part 2: Free Response
- Free Response: isNearlyFactorish(n) [50pts]
We will say that a digit k is a "near-factor" of n if it is positive integer and exactly 1 away from a real factor of n. In other words, if k is a positive integer and either k+1 or k-1 is a factor of n, we claim k is near-factor of n.
Also, a value n is "nearly factorish" (a coined term) if it is a positive integer between 100 and 999 inclusive (so with exactly 3 digits) where each digit is a near-factor of the number.
For example, 152 is nearly factorish because each of 1, 5, and 2 are near-factors of 152. Specifically:
1 + 1 == 2, which is a factor of 152
5 - 1 == 4, which is a factor of 152
2 - 1 == 1, which is a factor of 152
By contrast, 264 is not nearly factorish because 6 is not a near-factor of 264.
With this in mind, write the function isNearlyFactorish(n) that takes any Python value n (including non-integers) and without crashing returns True if n is factorish and False otherwise.
Note: 0 is not a factor of anything# Note: almostEqual(x, y) and roundHalfUp(n) are both supplied for you. On all # quizzes, you may write additional helper functions if you wish. def almostEqual(d1, d2, epsilon=10**-7): #helper-fn return (abs(d2 - d1) < epsilon) import decimal def roundHalfUp(d): #helper-fn # Round to nearest with ties going away from zero. rounding = decimal.ROUND_HALF_UP return int(decimal.Decimal(d).to_integral_value(rounding=rounding)) #--------------- #Write your function here: def isNearlyFactorish(n): return 42 # We have provided these test cases: def testIsNearlyFactorish(): print('Testing isNearlyFactorish()...', end='') assert(isNearlyFactorish(152) == True) assert(isNearlyFactorish(264) == False) assert(isNearlyFactorish(130) == False) # 0 is not positive assert(isNearlyFactorish(1234) == False) # too big assert(isNearlyFactorish(12) == False) # too small assert(isNearlyFactorish(-124) == False) # ditto assert(isNearlyFactorish(124.0) == False) # not an integer assert(isNearlyFactorish('ack') == False) # ditto # we added a few more test cases after the quiz # to help with your Fix-It Friday submission: assert(isNearlyFactorish(153) == False) assert(isNearlyFactorish(-152) == False) assert(isNearlyFactorish(140) == False) assert(isNearlyFactorish(-140) == False) print('Passed!') testIsNearlyFactorish()
BonusCT section
- bonusCT1: Code Tracing (2pts)
This question is OPTIONAL. Indicate what the following code prints. Place your answers (and nothing else) in the box below. Note that you may not run this code.
def bonusCt1(n): def f(n): return 1 - 1/n return max(n, f(n), f(f(n)), f(f(f(n))), f(f(f(f(n)))), f(f(f(f(f(n))))), f(f(f(f(f(f(n))))))) print(0.5 + bonusCt1(0.5))