Last active
February 15, 2017 01:19
-
-
Save animatedlew/88694a04278904e609ddd5d3fecd15fc to your computer and use it in GitHub Desktop.
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
| # list[t] -> list[list[t]] | |
| def perm(xs): | |
| if len(xs) < 2: | |
| yield xs | |
| else: | |
| for i in range(len(xs)): | |
| head, tail = xs[i], xs[:i] + xs[i+1:] | |
| for p in perm(tail): | |
| yield [head]+p | |
| # str -> list[str] -> list[int] | |
| def findSubstring(s, words): | |
| result = [] | |
| for i, c in enumerate(s): | |
| for j, p in enumerate(perm(words)): | |
| word = str.join('', p) | |
| if subword(word, s[i:]): | |
| result.append(i) | |
| return result | |
| # str -> str -> bool | |
| def subword(word, s): | |
| for i, c in enumerate(word): | |
| if s[i] != c: | |
| return False | |
| return True | |
| # result: [0,9] | |
| print(findSubstring('barfoothefoobarman', ['foo', 'bar']) == [0, 9]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment