I have this script that will calculate all permutations of a given string from a file:
import sys
from itertools import permutations
def find_permutations(word):
perms = [''.join(p) for p in permutations(word)]
return ','.join(perms)
if __name__ == '__main__':
with open(sys.argv[1]) as data:
for line in data.readlines():
print(find_permutations(line.rstrip()))
However I have run into a Memory error
with the following string American Pie
. Is there a way I could get all the permutations of this string without running into a memory error? Or is it truly that there are so many, the system cannot handle it?
Exact error:
Traceback (most recent call last):
File "string_perms.py", line 13, in <module>
print(find_permutations(line.rstrip()))
File "string_perms.py", line 7, in find_permutations
return ','.join(perms)
MemoryError