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..
14 odpowiedzi
+ 2
number='12345'
Z=list(number)
list.reverse(Z)
s="".join(Z)
print(s)
+ 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
+ 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!
+ 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
- 1
Slick How?
- 1
Slick sir using logic we have to solve. We should use any formula like this
- 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
- 1
Make it
a = list('12345')
- 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))
- 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.
- 2
a = '12345'.split()
b = []
Theres your two lists, you know what to do
- 2
num = 12345
rev = 0
while num > 0 :
rem = num % 10
rev = (rev * 10) + rem
What mean by double slashes here?
num = num // 10