right면 numbers의 마지막 요소를 맨 앞으로 추가해주고 마지막 요소 삭제
left면 처음 요소를 맨 뒤로 추가해주고 처음 요소 삭제
import Foundation
func solution(_ numbers:[Int], _ direction:String) -> [Int] {
var arr: [Int] = numbers
if direction == "right" {
arr.insert(arr[arr.count-1], at: 0)
arr.removeLast()
return arr
} else {
arr.append(arr[0])
arr.removeFirst(1)
return arr
}
}
import Foundation
func solution(_ numbers:[Int], _ direction:String) -> [Int] {
var nums = numbers
direction == "right" ? nums.insert(nums.removeLast(), at: 0) : nums.append(nums.removeFirst())
return nums
}
삼항연산자로 가독성이 좋게 !