Computer Science 15-110, Fall 2010
Class Notes: Recitation 2 (Fri 3-Sep)
- Quiz 1
- Practice Writing Functions
- onesDigit and tensDigit
- triangleArea
- numberOfPoolBalls
- almostEqual
- isInteger
- isPerfectSquare
- isTriangular
- fabricYards
- fabricExcess
- dayOfWeek
- More Practice
Recitation 2
- Quiz 1
Quiz 1 will be administered at the start of recitation.
- Practice Writing Functions
- onesDigit and tensDigit
These were covered via email (and will not be covered in recitation): Writing Functions: Annotated Examples
- triangleArea
Write a function that
takes two numbers, a width and a height, and returns the area of a triangle
with that width and height. If either value is non-positive, your
function should return 0.
def triangleArea(width, height):
return 42
def testTriangleArea():
print "Testing triangleArea... ",
assert(triangleArea(1,1) == 0.5)
assert(triangleArea(2,1) == 1)
assert(triangleArea(0,5) == 0)
assert(triangleArea(-5,10) == 0)
print "Passed all tests!"
testTriangleArea()
- numberOfPoolBalls
Write a function that
takes the number of rows in a triangular rack of pool balls, and returns the
total number of pool balls in that rack. This just computes the sum 1 +
.. .+ n, which of course is n(n+1)/2. Simple enough. Return 0 for
negative values of n.
def numberOfPoolBalls(rows):
return 42
def testNumberOfPoolBalls():
print "Testing numberOfPoolBalls... ",
assert(numberOfPoolBalls(0) == 0)
assert(numberOfPoolBalls(1) == 1)
assert(numberOfPoolBalls(2) == (1+2))
assert(numberOfPoolBalls(3) == (1+2+3))
assert(numberOfPoolBalls(10) == (1+2+3+4+5+6+7+8+9+10))
assert(numberOfPoolBalls(100) == 5050)
assert(numberOfPoolBalls(-5) == 0)
print "Passed all tests!"
testNumberOfPoolBalls()
- almostEqual
Write a function that
takes two numbers and returns True if they are “almost equal” (within some very
small epsilon of each other) and False otherwise. Here, we'll use an epsilon of 0.000001.
def almostEqual(d1, d2):
epsilon = 0.000001
return False
def testAlmostEqual():
print "Testing almostEqual... ",
epsilon = 0.000001
assert(almostEqual(0, 0) == True)
assert(almostEqual(0, epsilon/2) == True)
assert(almostEqual(epsilon/2, 0) == True)
assert(almostEqual(0, 2*epsilon) == False)
assert(almostEqual(2*epsilon, 0) == False)
assert(almostEqual(-epsilon/3, +epsilon/3) == True)
print "Passed all tests!"
testAlmostEqual()
- isInteger
Write a function that
takes a number and returns True if it is an integer value and False
otherwise. This is not testing whether the type is “int”, because for
this question we will presume that 2.0 is an integer value, even though it is
of type ‘float’.
def isInteger(n):
return False
def testIsInteger():
print "Testing isInteger... ",
assert(isInteger(0) == True)
assert(isInteger(5) == True)
assert(isInteger(5.0) == True)
assert(isInteger(-5) == True)
assert(isInteger(-5.0) == True)
assert(isInteger(5.1) == False)
assert(isInteger(5.000000001) == False)
assert(isInteger(-5.1) == False)
assert(isInteger(-5.000000001) == False)
print "Passed all tests!"
testIsInteger()
- isPerfectSquare
Write a function that
takes a number and returns True if it is a perfect square and False otherwise.
def isPerfectSquare(n):
return False
def testIsPerfectSquare():
print "Testing isPerfectSquare... ",
assert(isPerfectSquare(0) == True)
assert(isPerfectSquare(1) == True)
assert(isPerfectSquare(4) == True)
assert(isPerfectSquare(36) == True)
assert(isPerfectSquare(12345*12345) == True)
assert(isPerfectSquare(2) == False)
assert(isPerfectSquare(5) == False)
assert(isPerfectSquare(12345*12345 - 1) == False)
assert(isPerfectSquare(-4) == False)
print "Passed all tests!"
testIsPerfectSquare()
- isTriangular
Write a function that
takes a number n and returns true if it is “triangular” – that is, if it is the
sum 1 + … + k for some value of k.
def isTriangular(n):
return False
def testIsTriangular():
print "Testing isTriangular... ",
assert(isTriangular(0) == True)
assert(isTriangular(1) == True)
assert(isTriangular(1+2) == True)
assert(isTriangular(1+2+3) == True)
assert(isTriangular(1+2+3+4+5+6+7+8+9+10) == True)
assert(isTriangular(2) == False)
assert(isTriangular(1+2+3+4+5+6+7+8+9+10-1) == False)
assert(isTriangular(-1) == False)
print "Passed all tests!"
testIsTriangular()
- fabricYards
Fabric must be
purchased in whole yards. Write a function that takes the number of
inches of fabric desired, and returns the smallest number of whole yards of
fabric that must be purchased.
def fabricYards(inches):
return 42
def testFabricYards():
print "Testing fabricYards... ",
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)
assert(fabricYards(108) == 3)
assert(fabricYards(109) == 4)
assert(fabricYards(-12345) == 0)
print "Passed all tests!"
testFabricYards()
- fabricExcess
Write a function that
takes the 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).
def fabricExcess(inches):
return 42
def testFabricExcess():
print "Testing fabricExcess... ",
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)
assert(fabricExcess(108) == 0)
assert(fabricExcess(109) == 35)
assert(fabricExcess(-12345) == 0)
print "Passed all tests!"
testFabricExcess()
- dayOfWeek
Write
a function that takes a date represented by three integers, the month
(1-12), the day (1-31), and the year, and returns an integer
representing the day-of-week for that date, where Sunday is 1, Monday
is 2, and so on, and Saturday is 7.
While there are several ways to do this, you must use this formula (from the most-excellent web site mathforum.org):
N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
Then
the remainder when you divide N by 7 is the day-of-week, where Saturday
is 0 and Friday is 6. Note that these values for the days are not
quite the same as those returned by this method.
Here is mathforum's description of the formula:
"d is the number or the day of the month, m is the number
of the month, and y is the year. The brackets around the
divisions mean to drop the remainder and just use the
integer part that you get.
Also, a VERY IMPORTANT RULE is the number to use for the
months for January and February. The numbers of these months
are 13 and 14 of the PREVIOUS YEAR. This means that to find
the day of the week of New Year's Day [of 1998], 1/1/98,
you must use the date 13/1/97."
Note:
you must make the adjustment to the month and year when
appropriate. So, for example, the date of New Year's Day for 1998
would be obtained in the natural way: dayOfWeek(1, 1,
1998). You may ignore the cases where the month, day, or year are
out of bounds.
def testDayOfWeek():
print "Testing dayOfWeek... ",
# On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
assert(dayOfWeek(2, 5, 2006) == 1)
# On 6/15/1215, the Magna Carta was signed on a Monday!
assert(dayOfWeek(6, 15, 1215) == 2)
# On 3/11/1952, the author Douglas Adams was born on a Tuesday!
assert(dayOfWeek(3, 11, 1952) == 3)
# on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
assert(dayOfWeek(4, 12, 1961) == 4)
# On 7/4/1776, the Declaration of Independence was signed on a Thursday!
assert(dayOfWeek(7, 4, 1776) == 5)
# on 1/2/1920, Isaac Asimov was born on a Friday!
assert(dayOfWeek(1, 2, 1920) == 6)
# on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
assert(dayOfWeek(10, 11, 1975) == 7)
print "Passed all tests!"
- More Practice
See http://kosbie.net/cmu/spring-10/15-110/handouts/quiz1-practice.html#Console_Programs
Ignore the Java references; these problems can all be done clearly in Python.
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem