1 / 6
Mar 2015

Hello,
I am used to coding in Java and C++, and right now trying my hand at Python, starting with rather simpler codes to learn on the way. I have tried to implement it on CANDY3 but even for small input, the program is hogging a lot of ram( I have 8gigs and its showing almost 7.7 when I run the code).

Code Removed

I couldnt find anything on the web. Hope you guys can help.
Thanks in advance

  • created

    Mar '15
  • last reply

    Mar '15
  • 5

    replies

  • 614

    views

  • 2

    users

  • 1

    link

There is nothing wrong with your code!
Make sure you are entering input in the same format as mentioned in the problem. (a blank line at the beginning of each test case)
Don't translate C++ code to Python stuck_out_tongue
Avoid this:

t = int(raw_input())
while t:
    # code
    t -= 1

Instead use:

for t in range(int(raw_input())):
    # code

Generally constants are labelled in all CAPS. So if it is a variable, don't name it in all CAPS.

@vamsi_ism I changed all the int conversions to long and that did it. But I still cant figure out why int failed when the input was small(yes, the input was in the same format as indicated)

The caps is just to name the variables same as in the problem statement, for easy debuging.

As for the C++ translation, I'll try my best :mrgreen: . Thanks again for your response.

In [1]: a = int(10**21)
In [2]: type(a)
Out[2]: long

int implies long was added after Python 2.7 I think (definitely in 3.4) I was using an earlier version so I had to change them.

int was discontinued in Python 3.x instead long was renamed to int.

But that is not what I was trying to tell!
Even in Python 2.7(and probably the versions before it) you need not explicitly convert any numbers to long data type.
If you are trying to convert a number which overflows int(i.e > 2^31), Python automatically changes it to a long.