I'm new to Python and I'm currently working on solving problems to improve my coding skills. Currently I'm working on a question where I had to stable sort the input and output the reverse-stable sorted values. I've programmed it and executed the code in the website's online judge and for one test case (don't know the test case), I got Memory Limit Exceeded error. So after some research, I understood that there is a memory leak happening in the code and the code is not fully efficient. So I have installed the python's memory_profiler to monitoring memory consumption of a process as well as line-by-line analysis of memory consumption of my code.
Please find below the input details, code, output and the memory profiling analysis taken from the memory_profiler.
Input:
8
1 2
16 3
11 2
20 3
3 5
26 4
7 1
22 4
Code:
from collections import OrderedDict
@profile
def test_1():
print "Enter the number: "
n = raw_input()
k = []
v = []
print "Enter ID and M: "
for i in range(0,int(n)):
a, b = raw_input().split(' ')
k.append(a)
v.append(b)
d = OrderedDict(zip(k,v))
sorted_items = sorted(d.items(), key=lambda (k,v):int(v), reverse=True)
for i, j in sorted_items:
print i, j
if __name__ == '__main__':
test_1()
Output:
Line # Mem usage Increment Line Contents
================================================
2 10.520 MiB 0.000 MiB @profile
3 def test_1():
4 10.531 MiB 0.012 MiB print "Enter the number: "
5 10.551 MiB 0.020 MiB n = raw_input()
6 10.551 MiB 0.000 MiB k = []
7 10.551 MiB 0.000 MiB v = []
8 10.551 MiB 0.000 MiB print "Enter ID and M: "
9 10.551 MiB 0.000 MiB for i in range(0,int(n)):
10 10.551 MiB 0.000 MiB a, b = raw_input().split(' ')
11 10.551 MiB 0.000 MiB k.append(a)
12 10.551 MiB 0.000 MiB v.append(b)
13
14 10.551 MiB 0.000 MiB d = OrderedDict(zip(k,v))
15 10.555 MiB 0.004 MiB sorted_items = sorted(d.items(), key=lambda (k,v):int(v), reverse=True)
16 10.555 MiB 0.000 MiB for i, j in sorted_items:
17 10.555 MiB 0.000 MiB print i, j
Expected Output (I'm able to get the desired output):
3 5
26 4
22 4
16 3
20 3
1 2
11 2
7 1
Is this code not efficient for higher input or higher numbers? from the analysis I could see that there is only less memory utilized but for that particular test case, I could see that memory utilization is more than 16MB. Can someone tell me where am I doing wrong. Is my approach wrong or flow is wrong? Could you please tell me why I'm not able to get the output as expected. Thanks in advance. Any help would be much appreciated.
rangetoxrangewill help.xrangeand see the execution time and memory it is giving in the result.Memory limit exceeded 0.374 16628 KB:(