+ 1

Hello. Python

How do i write a funcion where if given number does not end in 0 it would rrturn itin the oposite order. Can only use int. Ex. 1024 return 4201

3rd Dec 2018, 3:24 PM
Leaf
Leaf - avatar
5 Antworten
+ 3
Try this code. def g(x): if(str(x)[-1]!='0'): return int(str(x)[::-1]) print(g(123)) #output=321
3rd Dec 2018, 4:29 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
Benny Kosasih now check my new answer you will get your answer.why you got weird output.
3rd Dec 2018, 4:57 PM
Maninder $ingh
Maninder $ingh - avatar
+ 1
convert the number to an array of digits, reverse the order and convert the array to a number.
3rd Dec 2018, 3:40 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Tested Maninder $ingh code, it gives weird result, some number with 0 gone, some stayed the same like 1000 become 0001. https://code.sololearn.com/cEVO79BK3Sgv/?ref=app
3rd Dec 2018, 4:42 PM
Benny Kosasih
Benny Kosasih - avatar
+ 1
Benny Kosasih brother you are passing a list in function so for loop is require for every number now check my code.my old code only work with single number not with list of number now you can also work with list of numbers. https://code.sololearn.com/cKO0U6IVgdwl/?ref=app
3rd Dec 2018, 4:48 PM
Maninder $ingh
Maninder $ingh - avatar