15-112 Spring 2013
Homework 1
Due Monday, 21-Jan, at 9pm
Read these
instructions first!
- Start by downloading this file: hw1.py.
Edit that file and submit that edited file to Autolab.
- This entire hw is COLLABORATIVE.
You will work in groups of up to 3 other students who are in this course this
semester (and then with nobody else except current CA's and instructors).
You may not split up the work -- everyone must work on every problem. And
you may not simply copy any code but rather truly work together.
- All the work for this lab should be done collaboratively, with other students
in your assigned group. Plan to spend at least 5 hours with your group before
Monday night. Also, if you need help, ask for it. If you are in the position
to offer help, please do so!
- In extreme cases, such as athletes who are away all weekend, you can be
creative, and use Skype or other means of remote collaboration. However,
you must inform the professor (koz) via email prior to any such team
arrangements.
- Peer effort grading: to encourage everyone to participate
appropriately, after the deadline, each of you will provide a grade on the
effort of your teammates. This is limited just to effort, so no matter how
well someone understands the material, so long as they tried reasonably hard
they should score 100% on this grade. It is expected that nearly everyone
will score 100% on this grade. However, students who score too low on
their effort grade will have to meet with the professor, and may have their hw
grade reduced (perhaps significantly).
- Keep a log of your team meetings. Include the names and andrew id's of
everyone else in your group, a list of everyone in attendance, and the start
time, end time, and location of each team meeting. Show your log to your
CA at your manual grading session (see below).
- Even though you work collaboratively, you still must submit your own
solutions.
- The point of this lab is to get you started with the (soon-to-be-harder!)
material, to create an environment where you collaborate with your classmates,
and to be sure you know your CA's and are comfortable asking them for help.
- For all programs: You may not use loops or conditionals (which we've not
covered yet).
- Regarding the handwritten, manually-graded portion of this hw:
- While the entire hw is technically due Monday night, and the online
autograded portion (part B) is indeed due by then, you will not submit the
physical portion (Part A) then. Instead, to receive your grade, each group must
meet with one of their section's CA's on Monday, Tuesday, or Wednesday (so meet
by Wed 23-Jan at 10pm at the latest). You will submit your physical copies
then (along with your meeting logs (see above)), and you will review solutions to the hw. And get to know your CA
better!
- Your physical copy must be handwritten, not typed. Show your supporting
calculations as appropriate. Write neatly. Staple multiple pages. Include
your name, andrew id, and section clearly at the top of the first page, and the
names and andrew id's of your collaborators.
- Even though you will review solutions with your CA, you would also probably
benefit from attending the optional homework solution sessions, which will be on
Monday and Tuesday nights (exact time and locations will be provided via email).
Hint #1: You may find the round function helpful
at times. For example, round(2.4) returns 2.0, and round(2.5) returns 3.0.
Hint #2: For one of the problems in the autograded
portion, the following may be very helpful:
def ceiling(n):
# We'll require that n be positive since this only works in
that case.
assert(n >= 0)
# This uses "boolean arithmetic", which is not recommended, but
# we are using it here as a workaround for not having
conditionals yet
return int(n) + (n % 1 > 0)
- Manually-Graded Portion: Reasoning Over Code [25 pts; 4 pts each,
1 free pt!]
Submit this in person to your CA, as per the directions above. For
each of the following functions, find values of the arguments that will make the
function return True. 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! Also, CIRCLE YOUR ANSWERS!
And SHOW YOUR WORK!!! When grading, your CA will ask you to explain some
of your answers!def f1(x, y):
# This problem was solved in recitation!
assert(type(x) == type(y) == int)
z = 10+x*y
# note: almostEqual is as defined in the class notes
return (100>x>y>0) and (x-y<10) and almostEqual(z%(z-5),z**0.5)
def f2(x):
# This problem was also solved in recitation!
return ((type(x) != type(int(x))) and
((x % 1)**2 == int(x % 1)) and
(100 > x**2 > 8*x > 0))
def f3(x, y):
return (100>x>y>0) and (x+y == 10) and (y/x == x/y - 1)
def f4(x, y):
return ((100>x>y>0) and (x+x**y == 100))
def f5(x, y):
return ((100>x>y>0) and
(x+y == 16) and
(round(float(x/y)) != round(float(x)/y)))
def f6(x):
assert ((type(x) == int) and
(1000 > x > 0))
y = x%10*100 + x/10%10*10 + x/100
return ((x == y) and
(int(x**0.5) == 11) and
(x / 125) > (x / 135))
- Autograded Portion: Problem-Solving (Writing functions) [75 pts;
7.5 pts each]
Start by downloading this file: hw1.py.
Edit that file and submit that edited file to Autolab.
Submit this via Autolab (you will practice this in recitation this week)
Remember: No loops, no conditionals, no strings, no imports, no recursion.
Be sure to write reasonable test functions for every function!
- kthDigit(x, k)
Given two integers, x and k, return the kth digit of x, counting from
the right. So:
kthDigit(789, 0) returns 9
kthDigit(789, 1) returns 8
kthDigit(789, 2) returns 7
kthDigit(789, 3) returns 0
Negative
numbers should work, too, so:
kthDigit(-789, 0) returns 9
- 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, if
there are 3 rows, then we'd have 6 total pool balls (1+2+3). Write a
function that takes an int value, the number of rows, and returns another
int value, the number of pool balls in that number of rows. For
example, numberOfPoolBalls(3) returns 6. Do not use loops.
- numberOfPoolBallRows(balls)
This problem is the inverse of the
previous problem. In this case, return the number of rows required for
the given number of pool balls. Thus, numberOfPoolBallRows(6) returns
3. Note that if any balls must be in a row, then you count that
row, and so numberOfPoolBallRows(7) returns 4 (since the 4th row must have a
single ball in it).
- isEvenPositiveInt(x)
Given an arbitrary value x, return True if it is an integer, and it is
positive, and it is even (all 3 must be True), or False otherwise. Do
not crash if the value is not an integer. So, isEvenPositiveInt("yikes!")
returns False (rather than crashing), and isEvenPositiveInt(123456) returns
True.
- isPerfectCube(x)
Given an integer value x, returns True if it
is a perfect cube and False otherwise. That is, return True if there
is another integer y such that x = y3. Thus,
isPerfectCube(27) returns True, but isPerfectCube(16) returns False.
- isTriangular(x)
A number is triangular if it equals 1+2+3+...+N for some integer N.
Given an integer value x, return True if it is triangular, and False
otherwise. Note that this relates to the pool ball problems above.
- fabricYards(inches)
Fabric must be purchased in whole yards. Write a function that takes
a non-negative
number of inches of fabric desired, and returns the smallest number of whole
yards of fabric that must be purchased.
Thus,
fabricYards(1) is 1 (you need a full yard if you buy one inch) and
fabricYards(36) is also 1, but fabricYards(37) is 2.
- fabricExcess(inches)
Write a function that takes
a non-negative number of inches of fabric desired and
returns the number of inches of excess fabric that must be purchased (as
purchases must be in whole yards). Thus, since you need a whole yard
when you buy 1 inch, fabricExcess(1) is 35. Similarly,
fabricExcess(36) is 0, and fabricExcess(37) is 35.
Hint: there
are (at least) two good ways to write this. One way involves a simple
expression using one of the math operators we have learned. The other
way uses fabricYards (which you just wrote!). To restate: one
way uses div (/) and the other way uses mod (%). You should
understand both ways!!!
- nearestBusStop(street)
Write a function that takes a non-negative 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.
- areCollinear(x1, y1, x2, y2, x3, y3)
Write a function that takes six integer values representing three points
(x1,y1), (x2,y2), and (x3,y3), and returns True if those points all lie on a
single line, and False otherwise. Don't crash when checking for a
vertical line!
carpe diem - carpe
diem - carpe diem - carpe diem - carpe diem - carpe diem -
carpe diem - carpe diem - carpe diem