CMU 15-110: Principles of Computing
Week2 Practice (Due never)
Contents:
- Code Tracing
- distance(x1, y1, x2, y2)
- fabricYards(inches)
- fabricExcess(inches)
- isMultiple(m, n) and isFactor(f, n)
- numberOfPoolBalls(rows)
- getFacultyOfficeHours(day)
- nearestBusStop(street)
1. 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:
def f(n): return n + 1 print(f(3) + f(5))
- Trace #2:
def f(n): return n + 1 def g(n): return 10 * f(n) print(g(2)) print(g(2) + f(6))
- Trace #3:
def f(n): result = '' if (n < 0): result += 'A' elif (n % 10 >= 5): result += 'B' if (n > 5): result += 'C' else: result += 'D' return result print(f(-5)) print(f(0)) print(f(25))
- 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.def distance(x1, y1, x2, y2): return 42 # replace with your solution! def almostEqual(d1, d2): epsilon = 10**-7 return (abs(d2 - d1) < epsilon) def testDistance(): print('Testing distance()... ', end='') assert(almostEqual(distance(0, 0, 3, 4), 5)) assert(almostEqual(distance(-1, -2, 3, 1), 5)) assert(almostEqual(distance(-.5, .5, .5, -.5), 2**0.5)) print('Passed.') testDistance() - fabricYards(inches)
Fabric must be purchased in whole yards. Write the function fabricYards(inches) that takes the number of inches of fabric desired, which you may assume is a non-negative int, and returns as an int the smallest number of whole yards of fabric that must be purchased.def fabricYards(inches): return 42 # replace with your solution! def testFabricYards(): print('Testing fabricYards()... ', end='') assert(fabricYards(0) == 0) assert(fabricYards(1) == 1) assert(fabricYards(35) == 1) assert(fabricYards(36) == 1) assert(fabricYards(37) == 2) assert(fabricYards(72) == 2) assert(fabricYards(73) == 3) print('Passed.') testFabricYards() - fabricExcess(inches)
Write the function fabricExcess(inches) that takes the number of inches of fabric desired, which is a non-negative int, and returns the number of inches of excess fabric that must be purchased (as purchases must be in whole yards). Hint: you may want to use fabricYards, which you just wrote!def fabricExcess(inches): return 42 # replace with your solution! def testFabricExcess(): print('Testing fabricExcess()... ', end='') assert(fabricExcess(0) == 0) assert(fabricExcess(1) == 35) assert(fabricExcess(35) == 1) assert(fabricExcess(36) == 0) assert(fabricExcess(37) == 35) assert(fabricExcess(72) == 0) assert(fabricExcess(73) == 35) print('Passed.') testFabricExcess() - isMultiple(m, n) and isFactor(f, n)
Write the function isMultiple that takes two int values m and n and returns True if m is a multiple of n and False otherwise. Note that 0 is a multiple of every integer.
Then, write the function isFactor(f, n) that takes two int values f and n, and returns True if f is a factor of n, and False otherwise. Note that every integer is a factor of 0. Hint: use isMultiple(m, n) when writing isFactor(f, n).def isMultiple(m, n): return 42 # replace with your solution! def isFactor(f, n): return 42 # replace with your solution! def testIsMultiple(): print('Testing isMultiple()... ', end='') assert(isMultiple(1,1) == True) assert(isMultiple(2,10) == False) assert(isMultiple(-5,25) == False) assert(isMultiple(5,0) == False) assert(isMultiple(0,0) == True) assert(isMultiple(2,11) == False) assert(isMultiple(10,2) == True) assert(isMultiple(0,5) == True) assert(isMultiple(25,-5) == True) print('Passed.') def testIsFactor(): print('Testing isFactor()... ', end='') assert(isFactor(1,1) == True) assert(isFactor(2,10) == True) assert(isFactor(-5,25) == True) assert(isFactor(5,0) == True) assert(isFactor(0,0) == True) assert(isFactor(2,11) == False) assert(isFactor(10,2) == False) assert(isFactor(0,5) == False) print('Passed.') testIsMultiple() testIsFactor() - numberOfPoolBalls(rows)
Pool balls are arranged in rows where the first row contains 1 pool ball and each row contains 1 more pool ball than the previous row. Thus, for example, 3 rows contain 6 total pool balls (1+2+3). With this in mind, write the function numberOfPoolBalls(rows) that takes a non-negative int value, the number of rows, and returns another int value, the number of pool balls in that number of full rows. For example, numberOfPoolBalls(3) returns 6. We will not limit our analysis to a "rack" of 15 balls. Rather, our pool table can contain an unlimited number of rows. For this problem and the next, you should research Triangular Numbers.def numberOfPoolBalls(rows): return 42 # replace with your solution! def testNumberOfPoolBalls(): print('Testing numberOfPoolBalls()... ', end='') assert(numberOfPoolBalls(0) == 0) assert(numberOfPoolBalls(1) == 1) assert(numberOfPoolBalls(2) == 3) # 1+2 == 3 assert(numberOfPoolBalls(3) == 6) # 1+2+3 == 6 assert(numberOfPoolBalls(10) == 55) # 1+2+...+10 == 55 print('Passed.') testNumberOfPoolBalls() - getFacultyOfficeHours(day)
The faculty for 15-110 have office hours on Monday, Wednesday, and Friday, for 2 hours each of those days. With that in mind, write the function getFacultyOfficeHours(day) that takes a day (as a 3-character string, so 'Mon' for Monday), and returns the number of office hours for that day. You may assume the day is a legal day (so, one of 'Mon', 'Tue', 'Wed',...).def getFacultyOfficeHours(day): return 42 # replace with your solution! def testGetFacultyOfficeHours(): print('Testing getFacultyOfficeHours()... ', end='') assert(getFacultyOfficeHours('Mon') == 2) assert(getFacultyOfficeHours('Tue') == 0) assert(getFacultyOfficeHours('Wed') == 2) assert(getFacultyOfficeHours('Thu') == 0) assert(getFacultyOfficeHours('Fri') == 2) assert(getFacultyOfficeHours('Sat') == 0) assert(getFacultyOfficeHours('Sun') == 0) print('Passed.') testGetFacultyOfficeHours() - nearestBusStop(street)
Write the function nearestBusStop(street) that takes a non-negative int street number, and returns the nearest bus stop to the given street, where buses stop every 8th street, including street 0, and ties go to the lower street, so the nearest bus stop to 12th street is 8th street, and the nearest bus stop to 13 street is 16th street.def nearestBusStop(street): return 42 # replace with your solution! def testNearestBusStop(): print('Testing nearestBusStop()... ', end='') assert(nearestBusStop(0) == 0) assert(nearestBusStop(4) == 0) assert(nearestBusStop(5) == 8) assert(nearestBusStop(12) == 8) assert(nearestBusStop(13) == 16) assert(nearestBusStop(20) == 16) assert(nearestBusStop(21) == 24) print('Passed.') testNearestBusStop()
Free Response (Problem-Solving)
Thu Recitation
Fri Lecture
Team Hw2