reverse 함수에 정수인 숫자를 인자로 받습니다.
그 숫자를 뒤집어서 return해주세요.
def reverse(x):
if x < 0:
y = ""
temp = list(str(x))
temp.pop(0)
temp.reverse()
for i in temp:
y += i
z = int(y)
return -z
else:
y = ""
temp = list(str(x))
temp.reverse()
for i in temp:
y += i
return int(y)
따로 list를 만들 필요 없이 String의 reverse는 [::-1]를 해주면 된다.
def reverse(number):
string = str(number)
if string[0] == '-':
string = string[::-1]
new_string = string[-1]+string[:-1]
return int(new_string)
string = string[::-1]
return int(string)
문자 number 를 뒤집어서 다시 숫자로 반환하는데 -로 시작할 때는 -를 띠어 내고 뒤집어서 다시 더하는 방식으로 반환