15-112 Fall 2014 Homework 1
Part 1 (Collaborative) due Friday, 29-Aug, at 10pm
Part 2 ( SOLO) due Sunday, 31-Aug, at 10pm


Homework 1, part 1 (Collaborative)

Read these instructions first!

By its nature, part 1 (this part) of hw1 is basically collaborative. However, part 2 of this hw (see below) is strictly SOLO. See the syllabus for details.

The purpose of this part of hw1 is to be sure that you are familiarized with the various course resources you will need to be successful this semester. This portion of the hw is due by Friday at 10pm. Most hw is submitted via Autolab, including hw1 part 2. But hw1 part 1 will be submitted via email to all of your section CA's. You can find your CA's names, andrew email addresses, and pictures all in the course syllabus. Your grade on hw1 will be determined by part 2, except for each incomplete part of part1, we will deduct 10 points off your part2 score.
  1. Meet your CA's
    You must meet your CA's in person. This will mostly happen at recitation, but in any case it must happen! They have roster sheets and will check off your name when you met them. Be sure your name is checked off!!

  2. Email all your CA's
    Note: read every step below before sending this email to your CA's, since you have to include some specific info in the email. With that said: prior to the Friday night deadline, and preferably well before then, email all your section's CA's (that is, 2 CA's for nearly all of you, and 3 for one or two sections). They will respond confirming that they received your email.

  3. Attend Lectures and Recitation
    Of course, this is required, but still, be sure to do it! In your email to your CA's, confirm that you attended all of these. We will be reviewing materials there which will make hw1 part 2 much more doable for you!

  4. Ask a good question on Piazza
    Don't just ask a question, but ask an interesting question (presumably based on the lecture material, or the reading, or practice materials). A good Piazza question is short and specific. Open-ended questions are better for office hours, not Piazza. Be sure to make your question private! Also, in your email to your CA's, include the day and time of your Piazza post. (Note: the point of this is to get you used to using Piazza, so don't fret too much over dreaming up the most awesomely perfect question; just put a little thought into it.)

  5. Ask a good question at CA Office Hours (OH)
    Once again, ask a good question, this time at CA office hours (see syllabus for schedule). OH questions can be more open-ended as needed. Sometimes you might not even know how to ask a question, as you might have more of a general confusion on some topic. No worries, that's perfect for OH! In any case, when you ask the question, note the time, location, and CA who answers. Include this information in your email to your CA's.

  6. Attend a 1-hour week1 practice session with one of your CA's
    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. Include the day, time, and location of this meeting in your email to your CA's. (Note: you only get credit for attending if you are on time and stay the whole time.)

  7. Attend at least 10 minutes of one of the evening review sessions
    These sessions are generally optional, but just this week, to get everyone started, you need to attend at least 10 minutes of one of the evening sessions. Every other week, these are entirely optional and are in WEH 7500 from 8-10pm on Tue, Wed, and Thu. This week, though, they are semi-required (well, 10 minutes are required), and are in these locations:
    • Thursday: 8pm-10pm in WEH 7500
    • Friday: 5:30pm-7:30pm in DH 2315 (Doherty, not Wean!)
    Of course, you might find that it's really helpful to attend quite a bit more than that (we hope so!). But 10 minutes is not much to invest, and will give you an idea what these sessions are like. Include the day and time you attended in your email to your CA's.

  8. Carefully Read the Syllabus
    In your email to your CA's, confirm that you read it and in particular confirm that you read and understand the policy on cheating and collaboration. If you have any questions about that policy, come see a professor asap!

  9. Get to Know at least 5 students in 15-112
    In your email to your CA's, include the names and andrew id's of at least 5 of your classmates in 15-112 this semester. Your classmates are an invaluable resource to you, as you are to them!

  10. Meet in a study group for at least 10 minutes
    Students should form study groups of between 2 and 6 students each. Groups should meet for at least 15 minutes, and study, and study together. Not just all sitting nearby each other but working independently! No! Rather, working all as one group on one problem at a time. Asking questions and discussing solutions together. Some of you may find this extremely helpful, others not so much. That's ok. Do it for 15 minutes and see if it works for you. Also, don't feel bound by these study groups. You may join, leave, and rejoin study groups however you wish this semester. We may help organize them, but we will not require them. Except this week. Everyone must meet in a student-formed study group for at least 15 minutes. In your email to your CA's, include the names and andrew id's of your study group partners, along with the day, time, and location of your meeting.

If you think about it, all of this is about PEOPLE. Know the people who can help you, and the various mechanisms for obtaining that help. These are tremendous resources for you to use. Then, use them! Judiciously, of course. But use them! Have fun, good luck, and carpe diem!


Homework 1, part 2 (SOLO)

Read these instructions first!

As noted above, part 1 of this hw is basically collaborative. However, part 2 of this hw (this part) is not collaborative and instead is strictly SOLO. See the syllabus for details. Be sure to properly use the course resources (CA's, office hours, Piazza, review sessions, etc)! And do not even discuss the problems in part 2 with any other students or anyone except the course staff. You may use any resources included on the course website (such as, say, the official Python website, or several free online textbooks), but you also may not use any other web resources on part 2 of this hw. Besides any standard consequences listed in the syllabus, anyone who violates the SOLO terms of part 2 of hw1 will be summarily removed from the course.

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.

A. Manually-Graded Portion [15 pts; 5 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.

Reasoning Over Code
For each of the following functions (f, g, and h), find values of the arguments that will make the function return True. You only need one set of arguments for each function, even if there are multiple correct answers. Show your work. Hint: reason your way through this. Consider only the possible inputs. For example, if you are given that x+y==16 and x>y>0, then we only need consider (15,1), (14,2), ..., (9,7). That's only 7 (x,y) points in total, one of which must be correct!

def f(x):
  assert((type(x) == int) or (type(x) == long))
  z = x
  z += 3
  z *= 10
  z **= 2
  z -= x
  return (z == 104046120906035084020967429491109643279)

# Hint #1: you'll need the quadratic formula here!

# Hint #2: you might use Python to do these calculations, since they are
# more precise than most calculators would support... 

# Hint #3: actually, the square root calculation in particular requires
# even more precision that Python will give you by default.  But there is
# an easy workaround: use an "exact integer square root" function, and many
# such functions are published on the web (though be careful for errors -- for
# example, square your answer and confirm you get the original number!).
# Here (http://tinyurl.com/isqrt-in-python) is one such page (chosen from a
# random Google search, but it's the isqrt() function that we used for our
# sample solution to this problem).

# Note about hint #3: it is ok to use that function we found online here,
# because we just gave you special permission.  For the rest of these SOLO problems,
# though, as has been noted repeatedly, you may not use online sources.

def g(x):
  assert(type(x) == int)
  y = x%x*x**x + x**2%x/x + x/x*x/2
  return ((2*y != x) and ((x+y) == 1501))

def h(x, d):
  assert(type(x) == int)
  assert(type(d) == float)
  e = int(round((d % 1) * 100))
  return ((x > d > 25) and
          ((x%10) == (e/10)) and
          ((x/10) == (e%10)) and
          (d * 100 % 1 == 0) and
          (int(round(d)) == x) and
          (int(d) != x))

# Hint: because of the floating-point == in (d * 100 % 1 == 0),
# a few solutions that "should" work do not, due to roundoff error.
# So be sure to check your solution by calling h(x,d) on it!

B. Autograded Portion: Problem-Solving (Writing functions) [85 pts; 12 pts each; 1 free pt]
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. 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).

  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!