![]() |
![]() |
|
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
# mix1 : Picture, Picture -> Picture # Return a mixture of p1 and p2 (every other pixel) # Both pixtures *must* by 640x480. # (Challenge: rewrite to allow any-two-same-sizes; # add'l challenge: re-write to allow different sizes, clamping the result to the smaller size.) # def mix1( p1, p2 ): mixPict = makeEmptyPicture( 640, 480 ) show(mixPict) for rowNum in range( getHeight(p1) ): for colNum in range( getWidth(p1) ): if atEvenGridPosition(rowNum,colNum): sourcePx = getPixelAt(p1,colNum,rowNum) else: sourcePx = getPixelAt(p2,colNum,rowNum) newPx = getPixelAt(mixPict,colNum,rowNum) setRed( newPx, getRed( sourcePx) ) setGreen( newPx, getGreen(sourcePx) ) setBlue( newPx, getBlue( sourcePx) ) repaint(mixPict) return mixPict # atEvenGridPosition : int, int -> boolean # Is location (x,y) at an 'even' grid location # (e.g., a *red* check-square, where the checker-tiles are 1x1 pixel) # def atEvenGridPosition(x,y): if ((x+y)%2==0): return True else: return False # Note: a simpler one-line version: `return (n%2==0)`. |
Use top-down design to write the above function, inventing helper-functions (“the method of wishful thinking”); then we can go back and complete those helper-functions later; each helper-function will be a smaller/easier problem than the overall task.
Although BMI isn't a great measure of overall health, current standards classify people as "underweight", "normal", "overweight", or "obese" based on various BMI levels.
Write a function which computes somebody's
# weightStatus : float, float, float -> string # def weightStatus( weightLbs, wholeFt, extraIn ): return "TODO" def test(): print( "status of a skinny beanpole:" print( "actual: " + str(weightStatus(150,6,3)) ) print( "expect: " + "underweight" ) print( "status of a normal person:" ) print( "actual: " + str(weightStatus(150,5,4)) ) print( "expect: " + "underweight" ) print( "status of a stout person:" ) print( "actual: " + str(weightStatus(220,6,6)) ) print( "expect: " + "underweight" ) print( "status of the penguin:" ) print( "actual: " + str(weightStatus(220,3,3)) ) print( "expect: " + "obese" ) |
Don't repeat code;
we've already written
# bmi : float, float, float -> float # def bmi( weightLbs, wholeFt, extraIn ): kgs = weightLbs / 2.2 totalInches = 12*wholeFt + extraIn heightCm = totalInches * 2.54 heightM = heightCm / 100 return kgs / pow(heightM,2) |
home—info—labs—exams—hws
—D2L—MediaSamples/—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Apr.23 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |