The are a couple of things wrong with your code:
- You have a return statement in every branch of your code. That means that you will return from the first iteration of the loop no matter what.
list.appendmodifies the target instance in place. As is conventional in Python for such methods, it returns None, which, combined with #1 means that you always get a return value of None.
There's also something very strange about your code. You flip the sign of a positive number by multiplying by -1. That makes sense. But then you take the absolute value of a negative number. Why? Flipping the sign is the same as multiplying by -1 for negative numbers too. And even for zero.
In fact, you don't even need to multiply by -1. There's already a "flip the sign" operator: unary -.
You can write your function as a single list comprehension:
def reverse_sign_of_nos_in_a_list(list1):
return [-x for x in list1]
Answer from Mad Physicist on Stack OverflowThe are a couple of things wrong with your code:
- You have a return statement in every branch of your code. That means that you will return from the first iteration of the loop no matter what.
list.appendmodifies the target instance in place. As is conventional in Python for such methods, it returns None, which, combined with #1 means that you always get a return value of None.
There's also something very strange about your code. You flip the sign of a positive number by multiplying by -1. That makes sense. But then you take the absolute value of a negative number. Why? Flipping the sign is the same as multiplying by -1 for negative numbers too. And even for zero.
In fact, you don't even need to multiply by -1. There's already a "flip the sign" operator: unary -.
You can write your function as a single list comprehension:
def reverse_sign_of_nos_in_a_list(list1):
return [-x for x in list1]
list.append() method will append the new element in-place and then will return None. So your function returns that None. Also it will return immediatelly after first element of the original list is processed.
def reverse_sign_of_nos_in_a_list(list1):
""" This function reverses sign of numbers
in a list and returns a list.
"""
list2 = []
for num in list1:
if num > 0:
list2.append(num * -1)
elif num < 0:
list2.append(abs(num))
else:
list2.append(num)
return list2
print(reverse_sign_of_nos_in_a_list([1,2,3,-1,-2,-3,0]))
Please, note I keep the code as close as possible to the original code. I would implement the function differently.
How to reverse an int in python? - Stack Overflow
if statement - Reverse number in Python - Stack Overflow
slice - Reversing a negative number in Python - Stack Overflow
Can anyone explain how to reverse a integer number?
Videos
Without converting the number to a string:
def reverse_number(n):
r = 0
while n > 0:
r *= 10
r += n % 10
n /= 10
return r
print(reverse_number(123))
You are approaching this in quite an odd way. You already have a reversing function, so why not make line just build the line the normal way around?
def line(bottles, ending):
return "{0} {1} {2}".format(bottles,
plural("bottle", bottles),
ending)
Which runs like:
>>> line(49, "of beer on the wall")
'49 bottles of beer on the wall'
Then pass the result to reverse:
>>> reverse(line(49, "of beer on the wall"))
'llaw eht no reeb fo selttob 94'
This makes it much easier to test each part of the code separately and see what's going on when you put it all together.
Assuming that you want to preserve the sign, try this:
def reverse(x):
ans = int(str(x)[::-1]) if x >= 0 else -int(str(-x)[::-1])
return ans if -2**31 <= ans <= 2**31 - 1 else 0
It works as expected for all the edge cases introduced by the new requirements:
reverse(321)
=> 123
reverse(-321)
=> -123
reverse(120)
=> 21
reverse(-120)
=> -21
reverse(7463847412)
=> 2147483647
reverse(8463847412)
=> 0
reverse(-8463847412)
=> -2147483648
reverse(-9463847412)
=> 0
I have posted the unwrapped, simplified code to make it easier to understand the answer:
def int_reverser(test):
if test >= 0:
answer = int(str(test)[::-1])
else:
answer = -int(str(-test)[::-1])
if -2**31 <= answer <= 2**31 - 1:
return answer
else:
return 0