Write a logic to reverse the number '12345', without using len function and (::-1) formulla? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a logic to reverse the number '12345', without using len function and (::-1) formulla?

Reverse the '12345' as '54321' without using (::-1) and without using len function..

8th May 2020, 12:37 PM
Nikhil Kulkarni
Nikhil Kulkarni - avatar
12 Answers
+ 2
number='12345' Z=list(number) list.reverse(Z) s="".join(Z) print(s)
9th May 2020, 4:07 AM
Baljinder Singh
Baljinder Singh - avatar
+ 2
num = 12345 rev = 0 while num > 0 : rem = num % 10 rev = (rev * 10) + rem num = num // 10 print(rev) You can either use this or you can create a list and then reverse it
9th May 2020, 5:02 PM
ASG
ASG - avatar
+ 1
Nikhil Kulkarni, can you please show us your attempt what you have done so far in this task? It would be helpful if you put your code in playground and link it here. Thanks!
8th May 2020, 2:40 PM
Lothar
Lothar - avatar
+ 1
num="12345" revnum="" c=-1 for i in num: revnum += num[c] c-=1 print(revnum) #logic is simple. Just use negative counter as index
9th May 2020, 5:51 PM
Stefan Kothaj
Stefan Kothaj - avatar
- 1
Slick How?
8th May 2020, 1:13 PM
Nikhil Kulkarni
Nikhil Kulkarni - avatar
- 1
Slick sir using logic we have to solve. We should use any formula like this
8th May 2020, 1:17 PM
Nikhil Kulkarni
Nikhil Kulkarni - avatar
- 1
Right. We convert the string of numbers to a list, reorder them one by one into a new list backwards, then convert the new list to a string. Giving you your answer without doing it the easy way
8th May 2020, 1:19 PM
Slick
Slick - avatar
- 1
Make it a = list('12345')
8th May 2020, 1:21 PM
Slick
Slick - avatar
- 1
mystring = "12345" mystring2 = '' for i in range(max([int(x) for x in mystring])-1, -1, -1): mystring2+=mystring[i] mystring = int(mystring2) print(mystring, type(mystring))
8th May 2020, 5:08 PM
rodwynnejones
rodwynnejones - avatar
- 2
split it and assign the list to a variable. Make another variable that holds an empty list. Append your first list backwards from the last value to the blank list. Revel in your victory.
8th May 2020, 1:09 PM
Slick
Slick - avatar
- 2
a = '12345'.split() b = [] Theres your two lists, you know what to do
8th May 2020, 1:15 PM
Slick
Slick - avatar
- 2
num = 12345 rev = 0 while num > 0 : rem = num % 10 rev = (rev * 10) + rem What mean by double slashes here? num = num // 10
9th May 2020, 9:12 PM
Imran
Imran - avatar