Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

Traceback (most recent call last): File "/run-1341144766-1067082874/solution.py", line 27, in main() File "/run-1341144766-1067082874/solution.py", line 11, in main if len(s[i:j+1]) > 0: MemoryError Error in sys.excepthook: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 64, in apport_excepthook from apport.fileutils import likely_packaged, get_recent_crashes File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in from apport.report import Report MemoryError Original exception was: Traceback (most recent call last): File "/run-1341144766-1067082874/solution.py", line 27, in main() File "/run-1341144766-1067082874/solution.py", line 11, in main if len(s[i:j+1]) > 0: MemoryError

The above errors appeared when I tried to run the following program. Can someone explain what is a memory error, and how to overcome this problem? . The program takes strings as input and finds all possible substrings and creates a set(in lexicographical order) out of it and it should print the value at the respective index asked by the user otherwise it should print 'Invalid'

def main(): 

no_str = int(raw_input()) 

sub_strings= [] 

for k in xrange(0,no_str): 

s = raw_input() 

a=len(s) 

for i in xrange(0, a): 

  for j in xrange(0, a): 

                   if j >= i: 

                     if len(s[i:j+1]) > 0: 

                        sub_strings.append(s[i:j+1]) 

      sub_strings = list(set(sub_strings)) 

      sub_strings.sort() 

      queries= int(raw_input())

      resul = [] 

      for i in xrange(0,queries): 

resul.append(int(raw_input())) 

for p in resul: 

try: 

   print sub_strings[p-1] 

except IndexError: 

   print 'INVALID' 

if __name__ == "__main__":

 main()

2 Answers

0 votes
by (106k points)
edited by

To get rid of memory error in Python you can use the following error:-

s = raw_input() 

a=len(s) 

for i in xrange(0, a): 

     for j in xrange(0, a): 

          if j >= i: 

              if len(s[i:j+1]) > 0: 

                   sub_strings.append(s[i:j+1])

To know more about this you can have a look at the following video tutorial:-

0 votes
by (20.3k points)

If you are getting an unexpected MemoryError and you think you should have plenty of RAM available, Then it might happen because you are using a 32-bit python installation.

The easier solution would be, if you have a 64-bit operating system, is to switch to a 64-bit installation of python.

But, the issue is that 32-bit python only has access to ~4GB of RAM. This can shrink even further if your operating system is 32-bit, because of the operating system overhead.

For more information about 32-bit operating systems, ~4GB of RAM you can refer here: https://superuser.com/questions/372881/is-there-a-technical-reason-why-32-bit-windows-is-limited-to-4gb-of-ram

Related questions

0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Dec 2, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...