Advertisement
Guest User

Untitled

a guest
May 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. '''Exercise to complete printLocations as described below.'''
  2.  
  3. def printLocations(s, target):
  4.     '''s is a string to search through, and target is the substring to look for.
  5.    Print each index where the target starts.
  6.    For example:
  7.    >>> printLocations('Here, there, everywhere!', 'ere')
  8.    1
  9.    8
  10.    20
  11.    '''
  12.  
  13.  
  14.     repetitions = s.count(target)
  15.     end_position = 0
  16.     start_position = s.find(target)
  17.     for i in range(repetitions):
  18.         start_position = s.find(target, end_position)
  19.         end_position = s.find(target[-1], start_position) + 1
  20.         print(start_position)
  21.  
  22. def main():
  23.     phrase = 'Here, there, everywhere!'
  24.     print('Phrase:', phrase)
  25.     for target in ['ere', 'er', 'e', 'eh', 'zx']:
  26.         print('finding:', target)
  27.         print(printLocations(phrase, target))
  28.     print('All done!')
  29.  
  30. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement