python小题:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
我的代码
#!/bin/env python
x = raw_input("input a string x:")
a = '-'
if a in x:
x = list(x)
del x[0]
x.reverse()
x.insert(0,'-')
y = "".join(x)
print y
else:
y = x[::-1]
print y
萌叔的代码
input_number = input('Please input a number: ')
if input_number > 0:
number = list(str(input_number))
number.reverse()
print int(''.join(number))
elif input_number < 0:
number = list(str(input_number))
number.pop(0)
number.reverse()
print int('-'+''.join(number))
elif input_number == 0:
print 0
宝库同学的代码
def reverse(x):
answer = 0
sign = 1 if x > 0 else -1
x = abs(x)
while x > 0:
answer = answer * 10 + x % 10
x /= 10
return sign*answer