문자로 구성된 배열을 input으로 전달하면, 문자를 뒤집어서 return 하는 프로그램을 작성해주세요
Input : ["h", "e", "l", "l", "o"]
Output: ["o", "l", "l", "e", "h"]
Input : ["h", "a", "n", "n", "a", "h"]
Output: ["h", "a", "n", "n", "a", "h"]
def answer(str) :
s.reverse()
return s
def answer(str) :
return s[::-1]
def asnwer(str):
start_idx = 0
end_idx = len(s) - 1
while start_idx <= end_idx :
str[start_idx] = str[end_idx]
str[end_idx] = str[start_idx]
start_idx += 1
end_idx -= 1
가벼운 문제라고 생각할 수도 있습니다.
하지만 3번 풀이와 같이 배열의 인덱스를 활용하여 값의 위치를 바꿔주는 방식을 연습한다면 새로운 배열을 사용하거나 내장 함수를 활용하지 않고 문제를 해결할 수 있을 것 같네요!