reverse 함수에 정수인 숫자를 인자로 받습니다.
그 숫자를 뒤집어서 return해주세요.
x:
숫자
return: 뒤집어진숫자
를 반환!
예들 들어,
x: 1234
return: 4321
x: -1234
return: -4321
x: 1230
return: 321
저는 아래와 같이 짜봤습니다. x값에 음수, 양수, 0을 각각 넣어보세요.
x = 25640
def reverse(number):
if number == 0:
return number
list_number_r = list(str(number))[::-1] # 하나, str()메서드로 '23494'로바꿔줘요.
# 둘, list()를 이용해 -->['2','3','4','9','4]
# 셋, [::-1] 슬라이싱을 통해 ['4','9','4','3','2']로 변환하여 변수 list_number_r 저장
number_str_r = ''.join(list_number_r) # 하나, ['4','9','4','3','2'] -> '49432'로 str으로 변환해요.
if number > 0: # 양수 조건
return int(number_str_r) # 예. '49432' --> 정수 49432변환하여 return함
elif number < 0: # 음수 조건 예.
return -int(number_str_r[:-1]) # '49432-' --> -int('49432')
elif x % 10 == 0: # 일의 자리 숫자가 0일때
return int(number_str_r)[::-2] # 예. '494320' --> '23494' --> int('23494') 바뀌어 반환됨
print(reverse(x))