Skip to content

Instantly share code, notes, and snippets.

@vagelim
Created February 19, 2016 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vagelim/c8073617a2937d2e831f to your computer and use it in GitHub Desktop.
Save vagelim/c8073617a2937d2e831f to your computer and use it in GitHub Desktop.
Find all substrings
def locations_of_substring(string, substring):
"""Return a list of locations of a substring."""
substring_length = len(substring)
def recurse(locations_found, start):
location = string.find(substring, start)
if location != -1:
return recurse(locations_found + [location], location+substring_length)
else:
return locations_found
return recurse([], 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment