15-112 Spring 2015 Homework 1
Due Sunday, 18-Jan, at 10pm

Read these instructions first!

Except for the 1-hour meeting with your CA, this hw is SOLO. See the syllabus for details.

Start by downloading this file: hw1.py. Edit that file and submit that edited file to Autolab.

For all programs: You may not use loops or conditionals (which we've not covered yet). You also may not use strings (except in maximumHeartRate), or recursion, or imports besides math, or anything else we've not yet covered, as the object of this week is to reinforce your understanding of basic operators and expressions.

Hint: in some cases, you may want to make use of some math functions, say perhaps math.floor or math.ceil.

Also, while you may not use the internet (or anywhere else) to find code or to get code questions answered, you most certainly may use the web to find math formulas or things of that nature.

A. Manually-Graded Portion [30 pts; 15 pts each]
Submit the solutions to these problems in a triple-quoted string at the top of hw1.py (it is already there for you). Be sure to include these solutions entirely within the triple-quoted string, so Python basically ignores them.
  1. 1-Hour CA-Led Practice Session
    For this exercise, you need to attend a 1-hour week1 practice session with one of your CA's from your assigned recitation. Your CA's will contact you via email with numerous different times when they will offer these 1-hour sessions. You need to attend one of these by Friday night. The sooner, the better, as this will be a great help for you prior to starting hw1 part2. Note: you only get credit for attending if you are on time and stay the whole time. You have nothing to submit for this -- the CA's will take attendance and will enter this directly into Autolab, so you then receive the points for attending. Never mind the points, though: this is an invaluable time to practice with an expert, and to let you see just how wonderfully helpful it is to work with our CA's!

  2. f14 Quiz 1
    In the triple-quoted string at the top of your hw1.py file, include the solutions to f14's quiz1 (except for the bonus, which you should skip (or do for fun if you wish)). Remember that this is SOLO!
B. Autograded Portion: Problem-Solving (Writing functions) [70 pts; 10 pts each]
Be sure you started by downloading the hw1.py file we provided! Absolutely do not retype that file, but edit it directly! Edit it using a Python editor, like IDLE or Sublime. Do not, do not, do not use a non-Python editor! Submit the edited hw1.py file via Autolab (you will practice this in recitation this week) Remember: No loops, no conditionals, no strings (except in maximumHeartRate), no imports (except math), no recursion.

Big hint: the file hw1.py contains test functions for each of these functions. Before you try to solve a problem, be sure to carefully look over its test function. That can be a huge help for you to understand exactly what the problem is looking for.

As you adjust to the autograder, this week (and only this week) you may have unlimited submissions. However, your grade is determined only by your last submission.
  1. sphereVolumeFromSurfaceArea(surfaceArea)
    Returns the volume of a sphere given its surface area, which you may assume is a float. You may need to look up the formulas for the surface area and volume of a sphere.

  2. isDivisible(x, y)
    Returns True if x and y are both integer types and x is divisible by y, and False otherwise

  3. pascalsTriangleValue(row, col)
    Given int values row and col, this function 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 row and col are not legal values, returns False, instead of crashing. Hint: math.factorial may be helpful!

  4. nearestOdd(x)
    Returns the nearest odd integer to x, which may be a float.

  5. maximumHeartRate(age, gender)
    You may assume that age is a positive integer and gender a string and is either "male" or "female". Given these values, use the formulas on this page to compute and and return the maximum heart rate. Actually, one difference: here you should return the nearest int value to what the formula computes. Remember that you may not use conditionals this week, so you may have to use some boolean arithmetic on this problem (sigh). Note that boolean arithmetic in general is a bad idea, and you should not use that technique on any other problem if possible (and indeed it is possible to solve them without it), and also note that if you do use boolean arithmetic, be sure to convert to an int first with int().

  6. rectanglesOverlap(left1, top1, width1, height1, left2, top2, width2, height2)
    A rectangle can be described by its left, top, width, and height. This function takes two rectangles described this way, and returns True if the rectangles overlap at all (even if just at a point), and False otherwise.
    Important note: here we will represent coordinates the way they are usually represented in computer graphics, where (0,0) is at the left-top corner of the screen, and while the x-coordinate goes up while you head right, the y-coordinate goes up while you head down. Yes, up is down! This is quite common in computer graphics, and is how Tkinter and Brython in particular both work. Check out the examples in the test code we provided to see this in action. Up is down. Weird, but true.

  7. cosineZerosCount(r)
    Assume that r is a float, and this function returns the integer number of zeros of cosine(x) for r radians where 0 <= x <= r. You may need to Google about the shape of the graph of cosine, if you don't know where its zeros are (where it crosses the x axis). For example, pi/2 is one such zero. You can also look at the test function in hw1.py to see some others!