택배 발송
Coddy의 Go 개발자를 위한 Swift 코스 레슨. 48개 중 45번째.
챌린지
중급접수와 보고 기능은 유지하세요. dispatch(_ id: String) -> Parcel?를 완성하세요. 일치하는 소포를 제거해 반환하고, 없으면 변경 없이 nil을 반환하세요. 개수와 무게는 제거 결과를 반영해야 합니다. 네 단계 모두 같은 파일과 드라이버를 사용합니다. Parcel(id:weight:)는 값 타입이고, ParcelDesk()는 참조 타입입니다. 입력은 단계 번호 3으로 시작하고, 그 뒤에 ID/무게 쌍이 0~30개 이어집니다. ID는 소문자 ASCII 문자 1~10개이며, 무게는 0~100입니다. 중복된 ID는 무시하세요. 입력에 ID가 하나라도 있으면 첫 번째 ID의 소포를 발송하세요. 발송된 소포의 무게(없으면 0), 공백, 그리고 남은 개수를 출력하세요. 이후 단계의 메서드는 해당 단계가 될 때까지 자리 표시자로 유지하세요.
필수 API와 solve(_ input: String) -> String를 구현하세요. 마지막 개행 문자 없이 출력 텍스트를 반환하고, 편집 가능한 코드에서는 출력하지 마세요. 잠긴 드라이버는 추가로 유효한 입력을 사용해 API를 별도로 확인한 다음, 반환된 텍스트 뒤에 개행 문자를 정확히 하나 붙여 출력합니다. 모든 숫자 연산은 Int 범위 내에서 이루어집니다.
코드는 Parcel.swift, PricePolicy.swift, UnitPrice.swift, ParcelDesk.swift, Functions.swift에 작성하세요. main.swift에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다.
직접 해보기
import Foundation
func apiChecks(_ input: String) -> Bool {
struct ProbePrice: PricePolicy { func cost(weight: Int) -> Int { weight + 11 } }
let p = input.split(whereSeparator: { $0.isWhitespace }); let stage = Int(p[0])!
let d = ParcelDesk()
guard d.add(Parcel(id: "p", weight: 7)), !d.add(Parcel(id: "p", weight: 9)), d.add(Parcel(id: "q", weight: 2)), d.count == 2 else { return false }
if stage >= 2 { guard d.totalWeight == 9 else { return false } }
if stage >= 3 { guard d.dispatch("missing") == nil && d.count == 2 else { return false }; guard d.dispatch("p")?.weight == 7 && d.count == 1 && d.totalWeight == 2 && d.dispatch("p") == nil else { return false } }
if stage >= 4 { guard UnitPrice(rate: 5).cost(weight: 4) == 20 && d.quote(using: ProbePrice()) == 13 && d.quote(using: UnitPrice(rate: 4)) == 8 else { return false } }
return true
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// PUBLIC API 검사 시작
let apiOK = apiChecks(input)
// PUBLIC API 검사 끝
if apiOK { print(solve(input)) } else { print("API mismatch") }
Go 개발자를 위한 Swift의 모든 레슨
직접 연습해 보세요: Swift 플레이그라운드