Created
February 19, 2016 20:54
-
-
Save vagelim/c8073617a2937d2e831f to your computer and use it in GitHub Desktop.
Find all substrings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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