Skip to content

Instantly share code, notes, and snippets.

@rohit-jamuar
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save rohit-jamuar/b8c71409486f541befbc to your computer and use it in GitHub Desktop.

Select an option

Save rohit-jamuar/b8c71409486f541befbc to your computer and use it in GitHub Desktop.
Yields all subsequences of input string
#!/usr/bin/python
from itertools import combinations
def get_subsequences(input_str):
'''
Yields all subsequences of size in range [1, len(input_str)]
'''
if type(input_str) == str:
for length in range(1, len(input_str)+1):
for elems in combinations(input_str, length):
yield ','.join(elems)
else:
raise GeneratorExit
if __name__ == '__main__':
for output in get_subsequences('hello'):
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment