CMU 15-110 Fall 2018: Principles of Computing
Homework 2 (Due Tuesday 11-Sep at 8pm)



Team Hw2


Solo Hw2

  1. eggCartons [25 pts]
    Hint: This function is similar to fabricYards from recitation! That function is also in the week2-practice problems, and here is a video explaining how to write it:
    Write the function eggCartons(eggs) that takes a non-negative integer number of eggs, and returns the number of egg cartons required to hold that many eggs (given that each egg carton holds one dozen eggs, and you cannot buy fractional egg cartons). Be sure your function works for multiples of 12, including 0. Here are some test cases (that are already included in the the starter file for you):
    def testEggCartons(): print('Testing eggCartons()... ', end='') assert(eggCartons(0) == 0) assert(eggCartons(1) == 1) assert(eggCartons(12) == 1) assert(eggCartons(13) == 2) assert(eggCartons(24) == 2) assert(eggCartons(25) == 3) print('Passed.')


  2. circlesIntersect [25 pts]
    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.
    Hints:
    • Two circle intersect if the distance between their centers is not larger than the sum of their radii (do you see why?).
    • You almost surely will want to use the distance function we wrote in recitation as a helper function here. Here is a helpful video on how to write that function:


  3. getTaOfficeHours [15 pts]
    Hint: This function is similar to getFacultyOfficeHours from the week2-practice problems!
    At the time this was written, the TA's for 15-110 have office hours for 4 hours on Mondays, and 2 hours every other day, except not at all on Thursdays or Fridays. With this in mind, write the function getTaOfficeHours(day) that takes a day (as a 3-character string, so 'Mon' for Monday), and returns the number of TA office hours for that day. You may assume the day is a legal day (so, one of 'Mon', 'Tue', 'Wed',...).


  4. getAllOfficeHours [10 pts]
    Hint: You will want to use both getTaOfficeHours and getFacultyOfficeHours for this exercise!
    Write the function getAllOfficeHours(day) that works like the previous function, only it returns all the office hours for that day, including both faculty and TA office hours. This function should include just one line that does nothing except return the sum of getTaOfficeHours and getFacultyOfficeHours!

  5. customMadLib() [25 pts]
    First, study the madLibs example from this week's Case Studies. Then, inspired by that example, write your own custom MadLib in the function customMadLib that's provided for you in the hw2.py file. This should be your own invention, and definitely very different from the one you write in the team hw. Do not copy a MadLib from an online (or any other) source. Be original, be creative, have some fun. We are not expecting Pulitzer-Prize-winner prose, but we are expecting some thoughtful, fun, cleverness, at least a bit. So have some fun with this!
    Note: this exercise is not autograded. TA's will manually grade it.


  6. Bonus/optional: customInteractiveFiction [up to 5 pts]
    This exercise is bonus and optional. This is just like the madLibs exercise, but now for interactive fiction. Based on the interactiveFiction example from this week's Case Studies, write your own custom interactive fiction in the function customInteractiveFiction that's provided for you in the hw2.py file. Again, be original, do not copy from an online (or any other) source. Have fun with this!!!