RU beehive logo ITEC dept promo banner
ITEC 109
2014spring
ibarland

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs

lab12-top-down-practice
top down design
practice

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:
firstInit = substring( fullName, 0, 1 )
startLast = string.find(fullName, " ") + 1
lastName = substring( fullName, startLast, len(fullName) )
userName = string.lower( firstInit + lastName )
  
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 “def”inition, and to return the answer:
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 userName
Once you have loaded this, now we can type in the interactions window: ruUserNameFor( "Jay Leno" ) or ruUserNameFor( "Jay Z" ) or ruUserNameFor( "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.


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).

You can use the following conversion factors:
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:

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 revs min 
    --------- * 1  =  --------- * --------  =  --------------  =  5 revs/sec = 5Hz.
       min              min        60 sec       min 60 sec
Since 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.


Back to BMI. First, as a class, we'll compute some examples by hand:

Your task: Now that we have worked some concrete examples, we can decide what the major steps are, and refining them:

  1. We'll presume that we start with three already-initialized variables: weightLbs, heightFt, extraInches.
    (Recall: the idea is that if we change only the values in these three variables, then we should not need to change anything else — pressing load should leave the result sitting in some other variable that we compute.)
  2. Compute the weight in kg
  3. Compute the height in m
  4. Divide weight by the height²
  5. Profit!
You can choose to store each intermediate result in variables, if you like.

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:

  1. We'll presume that we start with three already-initialized variables: weightLbs, heightFt, heightIn.
  2. Compute the weight in kg
  3. Compute the height in m
    1. Compute the height entirely in inches.
    2. From that, compute the height entirely in cm.
    3. From that, compute the height entirely in m.
    (Some people might skip step 2b; that is a matter of personal preference.)
  4. Divide weight by the height²
  5. Profit!

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.

Challenge/for fun

See if you can put your code inside a def (analagous to how we modified ruUserNameFor, above), so that you can just pass three values to your function, and now you can call it like any other python function!


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.      

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs


©2014, Ian Barland, Radford University
Last modified 2014.Feb.13 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme