Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How inverse a number?
#1
Hello guys,
I need for a little project a function or a command that can inverse a number for example, if the input is 196; the output will be 691.
Can you help me please with this, please? Because it seems way too complicated for me😅, I am a beginner!
Reply
#2
Searching the internet is too hard? It is too hard to even make an attempt? It is too hard to even look if this question's been asked here before?

https://python-forum.io/Thread-Input-a-n...rse+number

Drop the "Its too hard for me", give things a good try, and you will find this forum to be very helpful.
Reply
#3
(Oct-30-2020, 07:44 PM)deanhystad Wrote: Searching the internet is too hard? It is too hard to even make an attempt? It is too hard to even look if this question's been asked here before?

https://python-forum.io/Thread-Input-a-n...rse+number

Drop the "Its too hard for me", give things a good try, and you will find this forum to be very helpful.

Sorry is just that I've saw this
n=int(''.join(map(str,reversed(list(str(n))))))
when I was looking on the internet.
By saying this with this tone it's was just for saying that I don't have the level for things like this. And BTW I just discovered the forum one hour ago and thought that this type of question got places in a forum like this.
But thank you for the link and thank you for showing me that this type of tone doesn't get a place here.
Reply
#4
@deanhystad
I find your response to be borderline of being too abrasive. I understand that people need to search the forum, but 9 out of 10 people do not. It is just a part of life to link old responses or re-answer the same question. We do not have any information on the OP, such as age. There are often children (age 8) asking questions, etc. Our forum aims to be a discussion board and not a Stack Overflow Q&A, so we promote discussion. I think that kind of response deters people way from the forum or makes people afraid to ask questions on the forum.


@Capitaine_Flam
Using phrases like "please, pretty please" and saying "something is too complicated" tells users you are looking for a handout. Just to let you know. If your interested in reading more about it, read this. It is very useful for how to phrase your question.

a slice would probably be the best option
>>> int(str(196)[::-1])
691
Capitaine_Flam and buran like this post
Recommended Tutorials:
Reply
#5
To simplify, the idea is to take the integer, convert it to a string of digits, reverse the string, then turn it back into an integer. The posted solutions do this great and are one line solutions. But let's take the steps one at a time:
my_number = 691
my_string = str(my_number)
my_new_string = my_string[::-1] #slice notation, a good thing to learn
my_new_number = int(my_new_string)
print(my_new_number)
Output:
196
We've all been beginners, but we really like to see your attempts at a problem and will help where you are stuck. Give a man a fish, teach a man to fish.
metulburr likes this post
Reply
#6
@metullburr Thank you for your answer and for the link I will read this page with the attention! (I was looking for something like this for a while!)
And I understand that the way that I ask the question was maybe not the more appropriate, and you were right when you saying that an answer like this is discouraging peoples and making it hard to ask a question, it's not creating a friendly environment.

But thanks I will come back to this forum if I need help and also for helping others! (when my python level will improve, it's may take a while!)
Reply
#7
(Oct-31-2020, 12:01 PM)jefsummers Wrote: To simplify, the idea is to take the integer, convert it to a string of digits, reverse the string, then turn it back into an integer. The posted solutions do this great and are one line solutions. But let's take the steps one at a time:
my_number = 691
my_string = str(my_number)
my_new_string = my_string[::-1] #slice notation, a good thing to learn
my_new_number = int(my_new_string)
print(my_new_number)
Output:
196
We've all been beginners, but we really like to see your attempts at a problem and will help where you are stuck. Give a man a fish, teach a man to fish.

Thank you for this response, it's really helpful!
I needed an explanation like this for a better understanding! now I got it!
Thank you again for your explanations!
Reply
#8
Little improvement of what you found on the internet:

def reverse_number(value):
    return int("".join(reversed(str(value))))
But the slicing syntax is shorter and easier to read.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
One solution without converting integer into string:

>>> def reverse_int(num):
...     reversed = 0
...     while num:
...         num, reminder = divmod(num, 10)
...         reversed = reversed * 10 + reminder
...     return reversed
...
>>> reverse_int(691)
196
>>> reverse_int(0)
0
>>> reverse_int(100)
1
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Laplace Inverse Transform Implementation jafar1363 11 14,802 Nov-17-2016, 08:52 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020