Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Last active February 15, 2017 01:19
Show Gist options
  • Select an option

  • Save animatedlew/88694a04278904e609ddd5d3fecd15fc to your computer and use it in GitHub Desktop.

Select an option

Save animatedlew/88694a04278904e609ddd5d3fecd15fc to your computer and use it in GitHub Desktop.
# 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