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

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs

lab11-top-down-intro
top-down design
introduction

Warm-up:


Top-down design

“Top-down design” means that you start with the large problem you want to solve, and break it into several steps; then you go back and refine each step into sub-steps, until you reach something that is code.

Task: From somebody's full name, generate what their RU username1 should be:

  1. If fullName = "Ian Barland", then we want to conclude userName = "ibarland".
  2. If fullName = "Jay Leno", then we want to conclude userName =                 .
  3. If fullName = "your name here", then we want to conclude userName = "                ".
  4. If fullName = "Jay Z", then we want to conclude userName =                 .
  5. If fullName = "P Diddy", then we want to conclude userName =                 .
We want to be able to compute the answer for any2 full name.

How do we approach this problem? We can pretty readily break it down into 3 steps:

  1. Figure out/extract the first initial
    (perhaps storing the result in a variable “firstInit”).
  2. Figure out/extract the last name
    (perhaps storing the result in a variable “                ”).
  3. Concatenate the above two, and convert that result to lower-case (storing the result in a variable “userName”).
We can (kinda) start writing code already:
firstInit = "?"  # replace with: extract the first initial from `fullName`
lastName = "???" # replace with: extract the last name from `fullName`
userName = "???" # replace with: `firstInit` concatenated to `lastName`, converted to lower case.
  

Refine Step #1: first initial.

To be sure we understand step #1, figure out what the contents of firstInit should be for each of the five samples above.

Can you figure out how to extract the first character from a string? (Hint: substring is your friend, but you need to figure out what three inputs to give it, so that it answers with the first character of the full name.)

firstInit = substring( fullName, 0, 1 )
lastName = "???" # replace with: extract the last name from `fullName`
userName = "???" # replace with: `firstInit` concatenated to `lastName`, converted to lower case.
  

Refine Step #2: last name (first try)

This seems harder for now. Let's come back to it!

Refine Step #3: final result

For this one, we will presume we already have not just firstInit from step#1, but also lastName from step#2 too!!

Let's make this concrete, re-visiting our 5 examples:

  1. (If fullName = "Ian Barland", then at this point we'll already have computed that firstInit = "I" and lastName = "Barland".)

    What do we need to do to get userName = "ibarland"?
    (Your answer should be in terms of firstInit and lastName, and not directly mention "i" or "barland".)

  2. (If fullName = "Jay Leno", then at this point we'll already have computed that firstInit = "J" and lastName = "Leno".)

    What do we need to do to get userName = "jleno"?
    (Your answer should be in terms of firstInit and lastName, and not directly mention "J" or "Leno".)

  3. (If fullName = "your name here", then at this point we'll already have computed that firstInit = "your first initial" and lastName = "your last name".)

    What do we need to do to get userName = "your suggested username"?
    (Your answer should be in terms of firstInit and lastName, and not directly mention your first initial or your last name.)

  4. (If fullName = "Jay Z", then at this point we'll already have computed that firstInit = "J" and lastName = "Z".)

    What do we need to do to get userName = "jz"?
    (Your answer should be in terms of firstInit and lastName, and not directly mention "Jay" or "Z".)

  5. (If fullName = "P Diddy", then at this point we'll already have computed that firstInit = "P" and lastName = "Diddy".)

    What do we need to do to get userName = "pdiddy"?
    (Your answer should be in terms of firstInit and lastName, and not directly mention "P" or "Diddy".)

firstInit = substring( fullName, 0, 1 )
lastName = "???" # replace with: extract the last name from `fullName`
userName = firstInit + lastName
  
Wait, that's not quite right: we forgot to convert to lower case! We can fix that:
firstInit = substring( fullName, 0, 1 )
lastName = "???" # replace with: extract the last name from `fullName`
userName = string.lower(firstInit + lastName)
  

Refine Step #2: last name (second try)

Having finished steps #1 and #3, we're nearly done! But we punted on #2; we need to come back to that.

What function will we use, to extract the last name, out of fullName? It seems like substring will do, but it's not always clear what numbers to give to substring so that it will return exactly the last name to us.

firstInit = substring( fullName, 0, 1 )
lastName = substring( fullName, ???, ??? )
userName = string.lower( firstInit + lastName )
  

Let's approach this by working through our concrete examples: For each of the following, write a call to substring that returns the last name:

  1. If fullName holds "Ian Barland", substring( fullName, 4, 11 ) returns "Barland". Where did the 4 and 11 come from?
  2. If fullName holds "Jay Leno", substring( fullName",     ,      ) returns "Leno". How did you figure out those two numbers?
  3. If fullName holds "your name here", substring( fullName,     ,      ) returns your last name. How did you figure out those two numbers?
    Note: Your name really does have spaces between the first and last name — I've seen many people who suddenly, for this exercise, decided to start writing their full name without using any spaces at all?!
At this point, do we have a general formula for what to use as the first and third arguments, to substring?
firstInit = substring( fullName, 0, 1 )
lastName = substring( fullName, ???, len(fullName) )
userName = string.lower( firstInit + lastName )
  

We're almost there — but we still need to figure out the index of where the last-name begins, inside fullName:

  1. If fullName holds "Jay Z", substring( fullName,     , len(fullName) ) returns "Z".
    How did you figure out that middle number?
  2. If fullName holds "P Diddy", substring( fullName,     , len(fullName) ) returns "Diddy".
    How did you figure out that middle number?
Come up with a general formula for the index of where the second name starts.
hint:Use string.find, and notice that it's the space which cues us to where one word ends and the next begins!
careful:Remember to count carefully: The index of the space-character is always slightly different than (the index of) where the last-name starts. How do you need to adjust it?

firstInit = substring( fullName, 0, 1 )
startLast = string.find(fullName, " ") + 1
lastName = substring( fullName, startLast, len(fullName) )
userName = string.lower( firstInit + lastName )
  

1 We'll just generate the username and presume that it's not taken; we can revisit this later in the semester, to account for adding digits when the initial name is still in use.      

2Well -- we can find a general formula that works when the full name has a first name following by a single space followed by a last name. If there are multiple spaces (or none), then all bets would are off. In particular, if somebody claims their first name includes a space character as part of their name, our approach won't work any more. Fortunately for this problem, first names don't tend to include spaces in our culture. (Last names sometimes do, though e.g. “Van Patten”.)      

homeinfolectslabsexamshws
D2Lbreeze (snow day)tutor/PIs


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