Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Input a number with any number of digits and output it in reverse order of digits.
#1
hi, i'm a python beginner. few months ago i've started learning python and it's getting interesting to me everyday. So, today i need some help on an interesting problem. "Write a program to reverse the digits of a input number and check if the both numbers are equal or not". Now, i've written a code for "Reverse the digits of a five digit number", but i want to generalize this code for any digit number input. so, how to do it? i've learned so far operators, functions, decision making statements, looping, exception handling and modules to some extent. here is the code i've written for a number upto five digits-

sum1=0
n=int(input("Please enter five digit Number:"))
num=n
a=n%10
n=n//10
sum1=(sum1+(a*10000))

a=n%10
n=n//10
sum1=(sum1+(a*1000))

a=n%10
n=n//10
sum1=(sum1+(a*100))

a=n%10
n=n//10
sum1=(sum1+(a*10))


a=n%10
n=n//10
sum1=(sum1+(a*1))

print("Number:",sum1)
if (num==sum1):
    print("Input number and the reverse number are equal")
else:
    print("Input number and the reverse number are not equal")
    






please provide your valuable suggestions.. Smile
Quote
#2
num = 1234567
num1 = int(str(num)[::-1])
print('num: {}, num1: {}'.format(num, num1))
1st line you can replace this with your input statement.
2nd line - 1st convert num to  a string str(num)
then reverse the string with slice operation [::-1]
next re-convert result back to int int()


result:
Output:
num: 1234567, num1: 7654321
sarada2099 likes this post
Quote
#3
thnx @Larz60+ for your kind response. though i haven't use this 'slicing' method or '.format()' function before, but i got the logic and that's pretty awesome. having said that, i'm wondering what other methods can be used also to solve this problem?
Quote
#4
If you for some reason do not want to use Larz60+ suggestion, you can do it directly by "removing" either first or last digit of the input number and use it to construct new "reversed" number.

num = 12345

rev_num = 0
while num:
    rev_num = 10 * rev_num + num % 10   # extracts last digit of num and "appends" to "shifted" rev_num
    num //= 10                          # just shortcut for num = num // 10 - removes "used" digit

print("num = {}, reversed num = {}".format(num, rev_num))
Output:
num = 0, reversed num = 54321
As you can see, it destroys the input number in a process of reverting it. If you need to keep your number, you need to use other variable (or you can put that reverting code into a function).

And if your only problem is to check whether number is a palindrome, then simple check based on Larz60+ code is enough:
str(num) == str(num)[::-1]
sarada2099 likes this post
Quote
#5
Probably not what you're looking for, but howz about:

orig_num = [1, 2, 3, 4, 5]
list.sort(orig_num, reverse=True)

print("Reversed number = {}".format(orig_num))
Output:
Reversed number = [5, 4, 3, 2, 1]
Larz60+ and sarada2099 like this post
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Quote
#6
Little necronitpicking:

sort with reverse keyword gives a list ordered in a reverse order, not a reversed list. For the number/list used as an example it does not matter, as it was ordered, but otherwise it could. Beside [::-1] it could be done with .reverse() or reversed().

As "reversing" list is equivalent to sorting it on index in decreasing order, I tried to abuse sort/sorted to get such result and ended with:

In [38]: a = [1, 2, 5, 3]

In [39]: sorted(a, key = lambda x, i=iter(range(len(a))) : -next(i))
Out[39]: [3, 5, 2, 1]
it looks ugly, but probably less ugly than:

In [41]: list(map(lambda x:x[1], sorted(enumerate(a), reverse=True)))
Out[41]: [3, 5, 2, 1]
Perhaps there are other ways how to reverse list with sort/sorted one-liner and built-ins ...
sarada2099 likes this post
Quote
#7
Nitpicking the nitpicking, we are dealing with (immutable, by essence) strings here, without reverse or reversed methods. Since we need a new string to print the result anyway [::-1] is as good as it gets.
sarada2099 likes this post
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Find the complement of a number landlord1984 4 16,272 Feb-21-2020, 04:26 AM
Last Post: shreyaspadhye3011
  counting items in a list of number combinations Dixon 2 87 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Number range? rusty11 5 142 Feb-18-2020, 09:45 PM
Last Post: rusty11
  Adding elements to a list by number Olavv 3 114 Feb-07-2020, 06:12 PM
Last Post: Olavv
  Print 'X' a number of times Than999 1 85 Jan-18-2020, 06:41 PM
Last Post: Larz60+
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 186 Jan-03-2020, 10:34 AM
Last Post: perfringo
  how to make a program with a certain number of multiples? syafiq14 3 165 Jan-01-2020, 02:39 PM
Last Post: syafiq14
  how to divide array number chinting 5 300 Dec-30-2019, 11:29 AM
Last Post: ibreeden
  Unable to pull number using BeautifulSoup and Regex sarath_unrelax 2 171 Dec-18-2019, 08:09 PM
Last Post: sarath_unrelax
  Reverse the string word sneha 2 321 Dec-12-2019, 03:37 AM
Last Post: sneha

Forum Jump:


Users browsing this thread: 1 Guest(s)