Whats wrong in my code : reverse the number problem

This is the practice problem :FLOW007 Problem - CodeChef
This is my solution (using strings in python):CodeChef: Practical coding for everyone

PS: was just experimenting , would appreciate any suggestion on it :slight_smile:

The translate function will change every โ€œ0โ€ to None. Try 100010 as input your program returns 11, but the CA is 10001.
Try this:

for i in range(int(input())): #test_cases
    number=input()
    ans=number[::-1]  #reverse and store
    k = 0 ;
    for x in ans:  #loop through to remove zeroes at the beginning
        if x =='0': #delete the character from the string
            k += 1 ;
        else:   #exit as soon as a non zero number is found :)
            break
        
    print(ans[k:])
2 Likes

#doubt
but i have included a break statement once x!=0, i,e once a string other than 0 is encountered it stops doing that, i tested the result on compiler it was satisfied for the given sample test(the results were correct)

Anyways thankyou for the quick response :slightly_smiling_face: , i think i shouldnโ€™t have complicated the process using some function which i dont know completely about and just searched for it on the web.

Thnks for the solution as well i will appreciate if u could clarify on my #doubt , thnkx again :upside_down_face:

letโ€™s look at this if block

if x=='0':
  ans = ans.translate({ord(x): None})

It says that if the first digit is โ€˜0โ€™, call ans.translate({ord(x): None} which will do nothing but convert all zeroes in ans string to None, Keep in mind not only first but it converts all zeroes int ans to None and thats why you are getting WA for TCs like 100010.

2 Likes

Oh i get it , so the time i call it it just replaced all 0โ€™s to None , :slight_smile: thanks for taking time to clarify it

1 Like