def suffix(stng):
list = []
length = len(stng)
for i in range(length):
x = stng[i:length] ## This gives a Memory Error..See below
list.append(x)
return list
This piece of code is a part of my solution of a problem on interviewstreet.com but when i submit it i get a Memory error…i want to know how to correct it?
This is the traceback:
Original exception was:
Traceback (most recent call last):
File "/run-1342184337-542152202/solution.py", line 35, in
listofsuffix=suffix(var)
File "/run-1342184337-542152202/solution.py", line 13, in suffix
x=stng[i:length]
MemoryError
A
MemoryErrormeans you have consumed all your RAM. You are creating a list containing all trailing parts of an original string. If your original string is too long, you will consume a lot of memory.One possibility is to use a generator to produce the suffixes one at a time instead of creating a list of all of them:
If the caller of
suffixessimply iterates over the result, you don’t even have to change the caller. If you truly needed an explicit list, then you’ll need a different solution.