CMU 15-112: Fundamentals of Programming and Computer Science
Check1 Practice (check1 in class on Thu 1-Sep)
- These problems will help you prepare for check1.
- To start:
- Create a folder named 'week1'
- download both check1.py and cs112_f16_wk1.py to that folder
- Edit check1.py using pyzo
- Do not use strings, loops, lists, or recursion this week.
- Do not hardcode the test cases in your solutions.
- Code Tracing
What will this code print? Figure it out by hand, then run the code to confirm. Then slightly edit the code and try again.- Trace #1 of 4:
import math def p(x): print(x, end=' ') # prints and stays on same line p(3 - 1 + 2 * 6 // 5) p(3 - 1 + 2 * 6 / 5) p(2**4/10 + 2**4//10) p(max(8/3,math.ceil(8/3))) print()
- Trace #2 of 4:
# Note: we provide roundHalfUp and roundHalfEven for you. # Use these instead of the builtin round function, since that # function may not behave as you expect. import decimal def roundHalfUp(d): # Round to nearest with ties going away from zero. rounding = decimal.ROUND_HALF_UP return int(decimal.Decimal(d).to_integral_value(rounding=rounding)) def roundHalfEven(d): # Round to nearest with ties going to nearest even integer. rounding = decimal.ROUND_HALF_EVEN return int(decimal.Decimal(d).to_integral_value(rounding=rounding)) def p(x): print(x, end=' ') # prints and stays on same line p(round(1.5)) p(roundHalfUp(1.5)) p(roundHalfEven(1.5)) print() p(round(2.5)) p(roundHalfUp(2.5)) p(roundHalfEven(2.5)) print()
- Trace #3 of 4:
def p(x): print(x, end=' ') # prints and stays on same line def f(x, y): if (x > y): if (x > 2*y): p('A') else: p('B') else: p('C') def g(x, y): if (abs(x%10 - y%10) < 2): p('D') elif (x%10 > y%10): p('E') else: if (x//10 == y//10): p('F') else: p('G') f(4,2) f(2,3) f(5,2) print() g(11,14) g(11,24) g(207,6) g(207,5) print()
- Trace #4 of 4:
def f(x): return 4*x + 2 def g(x): return f(x//2 + 1) def h(x): print(f(x-3)) x -= 1 print(g(x)+x) x %= 4 return g(f(x) % 4) // 2 print(1 + h(6))
- Trace #1 of 4:
- Reasoning Over Code
Find parameter(s) to the following functions so that they return True. Figure it out by hand, then run the code to confirm. There may be more than one correct answer for each function, and you can provide any one of them.- RC #1 of 2:
def rc1(n): if ((n < 0) or (n > 99)): return False d1 = n%10 d2 = n//10 m = 10*d1 + d2 return ((m < n) and (n < 12))
- RC #2 of 2:
def rc2(n): if ((n <= 0) or (n > 99)): return False if (n//2*2 != n): return False if (n//5*5 != n): return False return (n//7*7 == n)
- RC #1 of 2:
- distance(x1, y1, x2, y2)
Write the function distance(x1, y1, x2, y2) that takes four int or float values x1, y1, x2, y2 that represent the two points (x1, y1) and (x2, y2), and returns the distance between those points as a float. - circlesIntersect(x1, y1, r1, x2, y2, r2)
Write the function circlesIntersect(x1, y1, r1, x2, y2, r2) that takes 6 numbers (ints or floats) -- x1, y1, r1, x2, y2, r2 -- that describe the circle centered at (x1,y1) with radius r1, and the circle centered at (x2,y2) with radius r2, and returns True if the two circles intersect and False otherwise. - getInRange(x, bound1, bound2)
Write the function getInRange(x, bound1, bound2) which takes 3 int or float values -- x, bound1, and bound2, where bound1 is not necessarily less than bound2. If x is between the two bounds, just return it unmodified. Otherwise, if x is less than the lower bound, return the lower bound, or if x is greater than the upper bound, return the upper bound. For example:- getInRange(1, 3, 5) returns 3 (the lower bound, since 1 lies to the left of the range [3,5])
- getInRange(4, 3, 5) returns 4 (the original value, since 4 is in the range [3,5])
- getInRange(6, 3, 5) returns 5 (the upper bound, since 6 lies to the right of the range [3,5])
- getInRange(6, 5, 3) also returns 5 (the upper bound, since 6 lies to the right of the range [3,5])
- eggCartons(eggs)
Write the function eggCartons(eggs) that takes a non-negative integer number of eggs, and returns the smallest integer number of cartons required to hold that many eggs, where a carton may hold up to 12 eggs. - pascalsTriangleValue(row, col)
Write the function pascalsTriangleValue(row, col) that takes int values row and col, and returns the value in the given row and column of Pascal's Triangle where the triangle starts at row 0, and each row starts at column 0. If either row or col are not legal values, return None, instead of crashing. Hint: math.factorial may be helpful!