새로 알아간 것들
if - 삼항연산자 구현 꼭 기억하기
array == [] ? [-1] : array
reduce
monthDay[0..<a-1].reduce(0, +) + b
stack 구현하기
import Foundation
struct Stack {
private var items: [String] = []
func peek() -> String {
guard let topElement = items.first else { fatalError("This stack is empty.") }
return topElement
}
mutating func pop() -> String {
return items.removeFirst()
}
mutating func push(_ element: String) {
items.insert(element, at: 0)
}
}
extension Stack: CustomStringConvertible {
var description: String {
let topDivider = "---Stack---\n"
let bottomDivider = "\n-----------\n"
let stackElements = array.joined(separator: "\n")
return topDivider + stackElements + bottomDivider
}
}
reference