![]() |
![]() |
|
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
We saw last time, an example of breaking down a problem (compute the RU username, given the full name) by using top-down design, and refining each step.
sneak peak:After working through those steps, we arrived at a solution:Now, after all that, would you be surprised if I told you that python already had a built-in function that computed the username, given a full name? I hope so! But, now that we have figured out the general rule, we can create our own function that is treated just like any other built-in function! The trick is to indent the above code inside a “
firstInit = substring( fullName, 0, 1 ) startLast = string.find(fullName, " ") + 1 lastName = substring( fullName, startLast, len(fullName) ) userName = string.lower( firstInit + lastName )def ”inition, and toreturn the answer:Once you have loaded this, now we can type in the interactions window:
def ruUserNameFor( fullName ): firstInit = substring( fullName, 0, 1 ) startLast = string.find(fullName, " ") + 1 lastName = substring( fullName, startLast, len(fullName) ) userName = string.lower( firstInit + lastName ) return userNameruUserNameFor( "Jay Leno" ) orruUserNameFor( "Jay Z" ) orruUserNameFor( "Engelbert Humperdinck" ) .Defining/naming our functions is going to be the primary way we build up big solutions, out of small steps! It has the advantage that once we've written the function, we can totally forget about the steps involved (and, any local variables used inside the function); all we need to remember is
This turns out to be huge; it's the way that we can organize the millions of lines of code needed to launch a Mars rover, without having any human need to understand all the lines of code at once; you understand a small chunk, make sure it's correct, and then forget the details and only remember the conclusion of what the code does/returns.
- the function's name,
and- the (type of) the inputs it wants,
and- the (type of) value it returns!
Today, we'll give you and your lab partner a chance to work on the same.
Note:For today, switch off which of you and your partner traditionally does most of the typing. (You can both work at the same keyboard).
Task: Compute the Body-Mass Index (BMI), given a weight (in pounds), and a height (a number of feet, plus any extra inches).
formula: The BMI is computed by: weight/height² …
where that the weight must be in kg (not lbs), and the height in m (not ft/in).
LB_PER_KG = 2.2 IN_PER_FOOT = 12 CM_PER_IN = 2.54 |
Aside: When you're not sure if you should multiply or divide, just use Dimensional analysis! The name is high-falutin’, but the idea is simple:
- In addition to numbers, also carry along your units.
- You can cancel units, when they're in the numerator and denominator.
- You can always multiply by 1, in various guises.
For example, since 2.54cm = 1in, we can divide each side to get 1 = (2.54cm/1in); we can also conclude 1 = (1in/2.54cm). Depending on which unit you want to cancel out, you can multiply either of these versions of 1.
Example: Suppose you want to convert 300rpm (revolutions/min) to Hertz (revolutions/sec). Do you multiply by 60, or divide? Well, we have minutes in the denominator, and we want to cancel it out:
300 revs 300 revs 1 min 300 revsSince 1min = 60sec (a true equality — these are different was of expressing exactly the same timespan), we know that 1 = (1min/60sec), and so we know that multiplying by 1 hasn't changed our quantity — we're just expressing that same quantity in different units.min--------- * 1 = --------- * -------- = -------------- = 5 revs/sec = 5Hz. min min 60 secmin60 sec
Back to BMI. First, as a class, we'll compute some examples by hand:
It'd be convenient to compute if we had 100kg, and 1m tall (we can compute the answer in our head!). In that case, the BMI is . To make our life easy, we can reverse-engineer what ft/in/lbs result in 100kg and 1m: weight = 220lbs, and height is 3ft, 3.37in. Now, using those imperial units, go through the steps by hand to reach the answer, paying attention to when you need to multiply by the various conversion factors, and when you need to divide instead.
Your task: Now that we have worked some concrete examples, we can decide what the major steps are, and refining them:
Steps 1 and 3 aren't bad, but step 2 has some further parts to it. By refining our top-down approach, we arrive at a solution:
To get checked off: Your file should start with three “input” variables (the weight in pounds, and the heighth in two parts — whole-feet, and remaining-inches). If we change just the values in those variables at the top, then no other changes should be necessary to compute the final BMI.
See if you can put your code inside a
1 I guess I want to say our answer is 28.2 kg/m², because I've learned to like carrying units around. However, BMI is already in those units by definition, so maybe it's redundant to include them? Hrmmm. ↩
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Feb.13 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |