Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The built-in <string>.split() procedure works
  2. # okay, but fails to find all the words on a page
  3. # because it only uses whitespace to split the
  4. # string. To do better, we should also use punctuation
  5. # marks to split the page into words.
  6.  
  7. # Define a procedure, split_string, that takes two
  8. # inputs: the string to split and a string containing
  9. # all of the characters considered separators. The
  10. # procedure should return a list of strings that break
  11. # the source string up by the characters in the
  12. # splitlist.
  13.  
  14. def split_string(source,splitlist):
  15.     n = 0
  16.     length = len(splitlist)
  17.     while n < length:
  18.         searchitem = splitlist[n]
  19.         if searchitem in source:
  20.             source = source.replace(searchitem,'!')
  21.         n+=1
  22.     return source.split('!')
  23.  
  24. ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code', '']
  25. ['After', '', 'the', 'flood', '', '', '', '', '', '', '', 'all', 'the', 'colors', 'came', 'out', '']
  26. ['First Name', 'Last Name', 'Street Address', 'City', 'State', 'Zip Code']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement