Difference between revisions of "E-mail Harvester Assignment"

From WLCS
Line 10: Line 10:
 
# Recreate the find function from Chapter 7. '''find(strng, ch, index)''' should '''return''' the location of the character ch in string strng, BUT the search should start at index
 
# Recreate the find function from Chapter 7. '''find(strng, ch, index)''' should '''return''' the location of the character ch in string strng, BUT the search should start at index
 
# Create a find function that does the same thing as the above find function, but instead of searching forwards, make it search backwards. Name the function '''findBackwards(strng, ch, index)'''.  findBackwards() should also '''return''' an index
 
# Create a find function that does the same thing as the above find function, but instead of searching forwards, make it search backwards. Name the function '''findBackwards(strng, ch, index)'''.  findBackwards() should also '''return''' an index
# Prompt the user for a sentence that contains an e-mail address and store it in a variable named s
 
 
# Create a new function named harvestEmail(s) where the user's sentence is passed in as s
 
# Create a new function named harvestEmail(s) where the user's sentence is passed in as s
# Inside harvetsEmail(s), use the functions that you defined above to isolate the e-mail address
+
# Inside harvestEmail(s), use the functions that you defined above to isolate the e-mail address
 
## First, find the @-symbol using findAtSymbol() and storing the result in a variable.     
 
## First, find the @-symbol using findAtSymbol() and storing the result in a variable.     
 
## Find the space before the @-symbol using findBackwards() with the index of the AT-symbol.  Store its result in a variable.   
 
## Find the space before the @-symbol using findBackwards() with the index of the AT-symbol.  Store its result in a variable.   

Revision as of 11:02, 24 February 2009

Objective:

  • Be able to define functions with parameters
  • Be able to call functions with parameters
  • Be able to use while loops
  • Be able to traverse a string
  • Be able to manipulate strings

Directions:

  1. Write a function named findAtSymbol(strng) that takes a single parameter, strng, which is a string. The function should traverse the string and find the "@" symbol. Your function should then return the index of the "@" symbol
  2. Recreate the find function from Chapter 7. find(strng, ch, index) should return the location of the character ch in string strng, BUT the search should start at index
  3. Create a find function that does the same thing as the above find function, but instead of searching forwards, make it search backwards. Name the function findBackwards(strng, ch, index). findBackwards() should also return an index
  4. Create a new function named harvestEmail(s) where the user's sentence is passed in as s
  5. Inside harvestEmail(s), use the functions that you defined above to isolate the e-mail address
    1. First, find the @-symbol using findAtSymbol() and storing the result in a variable.
    2. Find the space before the @-symbol using findBackwards() with the index of the AT-symbol. Store its result in a variable.
    3. Lastly, find the space after the @-symbol by using the find() function and storing the result in a variable.
    4. You should then return the slice of the string that contains the e-mail address .

Testing:

# You should test harvestEmail() with the following doctest

def harvestEmail(s):
    """
      >>> harvestEmail("here is an email@address.com to test")
      'email@address.com'
      >>> harvestEmail("test this@this.com out")
      'this@this.com'
      >>> harvestEmail("is your e-mail a@a.com even in this sentence?")
      'a@a.com'
    """
 
if __name__ == '__main__':
    import doctest
    doctest.testmod()