Difference between revisions of "E-mail Harvester Assignment"

From WLCS
(New page: '''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 manipula...)
 
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''Objective:'''
+
'''Objectives:'''
 
* Be able to define functions with parameters
 
* Be able to define functions with parameters
 
* Be able to call functions with parameters
 
* Be able to call functions with parameters
Line 5: Line 5:
 
* Be able to traverse a string
 
* Be able to traverse a string
 
* Be able to manipulate strings
 
* Be able to manipulate strings
 +
 +
'''References:'''
 +
* [[Media:Strings_Python.pptx]]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch07.html Strings]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch03.html Functions]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch04.html Conditionals (if-statements)]
 +
* [http://openbookproject.net/thinkcs/python/english2e/ch06.html Iteration (while loops)]
  
 
'''Directions:'''
 
'''Directions:'''
# 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
+
# Write a function named '''findAtSymbol(s)''' that takes a single parameter, '''s''', which is a string.  
# 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
+
#* The function should '''traverse''' the string with a loop and use an if statement check if any character '''s[x]''' matches the "@" symbol. Your function should then '''return''' the index of the "@" symbol
# 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).
+
#* Return -1 if no "@" symbol is found
# Prompt the user for a sentence that contains an e-mail address
+
# Write a more generalized find() function named '''find(s, ch, index)'''
# Using the functions that you defined, isolate the e-mail address by finding the space before the @ symbol and the space after the @ symbol. You should print out the slice of the string that contains the e-mail address.
+
#* This time, the function should walk through '''s''' with a loop and look to see if any '''s[x]''' matches '''ch'''
 +
#* The loop counter should start the search at index
 +
#* '''return''' the location of the character ch in string '''s''' when there is a match
 +
#* Return -1 if '''ch''' is not found
 +
# Create another find() function that does the same thing as the above find function, but instead of searching forwards, it searches backwards.  
 +
#* Name the function '''findBackwards(s, ch, index)'''
 +
#* findBackwards() should also '''return''' an index
 +
#* Return -1 if '''ch''' is not found
 +
# Create a new function named '''harvestEmail(s)''' where the user's sentence is passed in as '''s'''.  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.   
 +
## Find the space before the @-symbol using findBackwards() with the index of the AT-symbol.  Store its result in a variable.
 +
##* You will need to send the " " space character and index of the AT-symbol into your findBackwards() function call.
 +
##* Example: findBackwards(s, " ", atSymbolIndex)
 +
## Lastly, find the space after the @-symbol by using the find() function and storing the result in a variable.
 +
##* You will need to send the " " space character and index of the AT-symbol into your find() function call.
 +
##* Example: find(s, " ", atSymbolIndex)
 +
## You should then return the '''slice''' of the string that contains the e-mail address .
 +
 
 +
'''Challenges:'''
 +
* After you have tested '''harvestE-mail(s)''', create a new function named:
 +
** '''harvestAllEmails(lines)''' which prints out all the e-mail addresses from a given paragraph
 +
* The e-mail harvester requires an e-mail to be separated by spaces.  Modify your program so that it can detect an e-mail separated by '''non''' e-mail characters
 +
** Assume e-mail addresses are limited to using characters: A-Z, a-Z, 0-9, ., _, @
 +
 
 +
'''Testing:'''
 +
<source lang="python">
 +
# 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()
 +
</source>
 +
 
 +
==Rubric==
 +
 
 +
{| class="wikitable"
 +
|-
 +
! Criteria
 +
! Pts
 +
|-
 +
| 'find' works perfectly 
 +
| 2
 +
|-
 +
| 'findAtSymbol' works perfectly 
 +
| 2
 +
|-
 +
| 'findBackwards' works perfectly 
 +
| 2
 +
 
 +
|-
 +
| 'harvestEmail' return correct string 
 +
| 2
 +
|-
 +
| Handles emails at the start or end of the string (e.g. no spaces before or after).
 +
| 2
 +
|-
 +
| Bonus: Good variable names
 +
| +2
 +
|-
 +
| Bonus: Simple, clear code
 +
| +2
 +
|-
 +
| Maximum points
 +
| 10 + 4
 +
|}

Latest revision as of 09:37, 31 January 2017

Objectives:

  • 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

References:

Directions:

  1. Write a function named findAtSymbol(s) that takes a single parameter, s, which is a string.
    • The function should traverse the string with a loop and use an if statement check if any character s[x] matches the "@" symbol. Your function should then return the index of the "@" symbol
    • Return -1 if no "@" symbol is found
  2. Write a more generalized find() function named find(s, ch, index)
    • This time, the function should walk through s with a loop and look to see if any s[x] matches ch
    • The loop counter should start the search at index
    • return the location of the character ch in string s when there is a match
    • Return -1 if ch is not found
  3. Create another find() function that does the same thing as the above find function, but instead of searching forwards, it searches backwards.
    • Name the function findBackwards(s, ch, index)
    • findBackwards() should also return an index
    • Return -1 if ch is not found
  4. Create a new function named harvestEmail(s) where the user's sentence is passed in as s. 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.
      • You will need to send the " " space character and index of the AT-symbol into your findBackwards() function call.
      • Example: findBackwards(s, " ", atSymbolIndex)
    3. Lastly, find the space after the @-symbol by using the find() function and storing the result in a variable.
      • You will need to send the " " space character and index of the AT-symbol into your find() function call.
      • Example: find(s, " ", atSymbolIndex)
    4. You should then return the slice of the string that contains the e-mail address .

Challenges:

  • After you have tested harvestE-mail(s), create a new function named:
    • harvestAllEmails(lines) which prints out all the e-mail addresses from a given paragraph
  • The e-mail harvester requires an e-mail to be separated by spaces. Modify your program so that it can detect an e-mail separated by non e-mail characters
    • Assume e-mail addresses are limited to using characters: A-Z, a-Z, 0-9, ., _, @

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

Rubric

Criteria Pts
'find' works perfectly 2
'findAtSymbol' works perfectly 2
'findBackwards' works perfectly 2
'harvestEmail' return correct string 2
Handles emails at the start or end of the string (e.g. no spaces before or after). 2
Bonus: Good variable names +2
Bonus: Simple, clear code +2
Maximum points 10 + 4