![]() |
![]() |
|
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
Warm-up:
# substring: return characters extracted from `txt`, # starting at index `start` going up through (but not including) index `stop`. # def substring( txt, start, stop): return txt[start:stop] # Load python's string-library: import string |
“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:
How do we approach this problem? We can pretty readily break it down into 3 steps:
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. |
To be sure we understand step #1,
figure out what the contents of
Can you figure out how to extract the first character from a string?
(Hint:
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. |
This seems harder for now. Let's come back to it!
For this one, we will presume we already have
not just
Let's make this concrete, re-visiting our 5 examples:
(If
What do we need to do to get
(Your answer should be in terms of
(If
What do we need to do to get
(Your answer should be in terms of
(If
What do we need to do to get
(Your answer should be in terms of
(If
What do we need to do to get
(Your answer should be in terms of
(If
What do we need to do to get
(Your answer should be in terms of
firstInit = substring( fullName, 0, 1 ) lastName = "???" # replace with: extract the last name from `fullName` userName = firstInit + lastName |
firstInit = substring( fullName, 0, 1 ) lastName = "???" # replace with: extract the last name from `fullName` userName = string.lower(firstInit + lastName) |
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
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
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?!
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
hint:Usestring.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”.) ↩
home—info—lects—labs—exams—hws
—D2L—breeze (snow day)—tutor/PIs
©2014, Ian Barland, Radford University Last modified 2014.Feb.10 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |