Questions : python-How to reverse an negative integer or non-integer
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00
857
I am learning python and I meet some anycodings_python troubles.
I want to write the script to reverse a anycodings_python negative integer " -1234 to 4321- " and anycodings_python non-integer " 1.234 to 432.1". please help anycodings_python me. P.S. cannot use "str[]" function
I just only can write the script to reverse anycodings_python positive integer 1234 to 4321
def reverse_int[n]:
x = 0
while n > 0:
x *= 10
x += n % 10
n /= 10
return x
print reverse_int[1234]
Total Answers 7
24
Answers 1 : of python-How to reverse an negative integer or non-integer
def reve[x]:
x=str[x]
if x[0]=='-':
a=x[::-1]
return f"{x[0]}{a[:-1]}"
else:
return x[::-1]
print[reve["abc"]]
print[reve[123]]
print[reve[-123]]
#output cba 321 -321
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
mRahman
5
Answers 2 : of python-How to reverse an negative integer or non-integer
how about using your code, but just anycodings_python-2.x concatenate a - when n is negative?
rev_int.py:
def reverse_int[m]:
x = 0
n = m
if m < 0 :
n *= -1
while n > 0 :
x *= 10
x += n % 10
n /= 10
if m < 0:
#concatenate a - sign at the end
return `x` + "-"
return x
print reverse_int[1234]
print reverse_int[-1234]
This produces:
$ python rev_int.py
4321
4321-
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
jidam
2
Answers 3 : of python-How to reverse an negative integer or non-integer
Using SLICING EASILY DONE IT
def uuu[num]:
if num >= 0:
return int[str[num][::-1]]
else:
return int['-{val}'.format[val = str[num][1:][::-1]]]
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
miraj
1
Answers 4 : of python-How to reverse an negative integer or non-integer
Below code runs fine on Python-3 and anycodings_python-2.x handles positive and negative integer anycodings_python-2.x case. Below code takes STDIN and prints anycodings_python-2.x the output on STDOUT.
Note: below code handles only the anycodings_python-2.x integer case and doesn't handles anycodings_python-2.x the non-integer case.
def reverseNumber[number]:
x = 0
#Taking absolute of number for reversion logic
n = abs[number]
rev = 0
#Below logic is to reverse the integer
while[n > 0]:
a = n % 10
rev = rev * 10 + a
n = n // 10
#Below case handles negative integer case
if[number < 0]:
return [str[rev] + "-"]
return [rev]
#Takes STDIN input from the user
number=int[input[]]
#Calls the reverseNumber function and prints the output to STDOUT
print[reverseNumber[number]]
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
joy
1
Answers 5 : of python-How to reverse an negative integer or non-integer
Using str convert method.
num = 123
print[str[num][::-1]]
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
joy
2
Answers 6 : of python-How to reverse an negative integer or non-integer
Use this as a guide and make it work for anycodings_python-2.x floating point values as well:
import math
def reverse_int[n]:
if abs[n] < 10:
v = chr[abs[n] + ord['0']]
if n < 0: v += '-'
return v
else:
x = abs[n] % 10
if n < 0: return chr[x + ord['0']] + reverse_int[math.ceil[n / 10]]
else: return chr[x + ord['0']] + reverse_int[math.floor[n / 10]]
print reverse_int[1234]
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
miraj
4
Answers 7 : of python-How to reverse an negative integer or non-integer
Why not just do the following?:
def reverse[num]:
revNum = ''
for i in `num`:
revNum = i + revNum
return revNum
print reverse[1.12345]
print reverse[-12345]
These would print 54321.1 and 54321-.
Lol, I don't understand the down-vote.. anycodings_python-2.x I guess this is too simple and doesn't anycodings_python-2.x require over complicating the process to anycodings_python-2.x get to the same result? Does this not anycodings_python-2.x solve the problem at hand no matter the anycodings_python-2.x number used?
0
2022-10-06T10:36:56+00:00 2022-10-06T10:36:56+00:00Answer Link
joy