Menu
CoddyTech

복습: 검증된 배치

Coddy의 Lua 개발자를 위한 Swift 코스 레슨. 48개 중 36번째.

challenge icon

챌린지

쉬움

BatchError: Error를 negative 케이스와 함께 구현하세요. Functions.swift에 checkedTotal(_ values: [Int]) -> Result<Int, BatchError>를 구현하세요. 값 중 음수가 있으면 실패하고, 그렇지 않으면 합계와 함께 성공해야 합니다(입력이 비어 있으면 0). 입력에는 -100부터 100까지의 정수가 0개에서 50개까지 있습니다. 합계 또는 negative를 출력하세요.

필수 API와 solve(_ input: String) -> String을 구현하세요. 마지막 개행 없이 출력 텍스트를 반환하고, 편집 가능한 코드에서는 출력하지 마세요. 잠긴 드라이버가 추가 유효 입력으로 API를 각각 확인한 다음, 반환된 텍스트를 정확히 하나의 개행과 함께 출력합니다. 모든 숫자 연산은 Int의 범위 안에서 이루어집니다.

코드는 BatchError.swift, Functions.swift에 작성하세요. main.swift에는 제공된 입력/출력 코드가 있으며 수정할 수 없습니다.

직접 해보기

import Foundation
func apiChecks(_ input: String) -> Bool {
guard case .success(8) = checkedTotal([3,5]), case .failure(.negative) = checkedTotal([2,-1]), case .success(0) = checkedTotal([]) 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") }

Lua 개발자를 위한 Swift의 모든 레슨

직접 연습해 보세요: Swift 플레이그라운드