0

I have developed code for reversing number which was on leetcode. I runned it on python idle it worked fine. But when i am trying to run it on leetcode it throws this error : ValueError: invalid literal for int() with base 10: code on idle:

def Reverse(x):
    s_num = str(x)  //converting int to str
    r_num = s_num[::-1] //reversing str
    l= r_num
    temp = 0
    for i in range(l):
        num =(int(int(r_num[i]))*(10**(l-i-1)))+temp
        temp = num
    return num
print(Reverse(1212))

code on leetcode:

class Solution:
    def reverse(self,x):
        s_num = str(x)
        r_num = s_num[: : -1]
        l= int(len(r_num))
        temp = 0
        for i in range(l):
            num = int(int(r_num[i]))  //showing error here
            mul = 10**(l-i-1)
            total = num*mul
        return total

i tried converting string to integer twice

  • Welcome! I suggest you to add the `python` tag to your question, for better audience. I can do that for you, but the time the edit follow the whole edit queue and be accepted could be longer than you, editing your post. – Marc Le Bihan Dec 25 '22 at 06:01
  • What could be the input in this problem? Can you include that in the question? Also calling `int()` twice is not required here – kuro Dec 26 '22 at 09:49

0 Answers0