Menu
CoddyTech

Riepilogo: batch convalidato

Lezione 36 di 48 del corso Swift per sviluppatori C# di Coddy.

challenge icon

Sfida

Facile

Implementa BatchError: Error con il caso negative. Implementa checkedTotal(_ values: [Int]) -> Result<Int, BatchError> in Functions.swift: restituisci un errore se un qualsiasi valore è negativo, altrimenti restituisci la somma (incluso zero per un input vuoto). L'input contiene da 0 a 50 numeri interi compresi tra -100 e 100. L'output è il totale oppure negative.

Implementa le API richieste e solve(_ input: String) -> String. Restituisci il testo dell'output senza newline finale; non stampare nel codice modificabile. Il driver bloccato verifica indipendentemente le API con altri input validi, poi stampa il testo restituito con esattamente un newline. Tutte le operazioni numeriche restano entro i limiti di Int.

Il tuo codice va in BatchError.swift e Functions.swift. main.swift contiene il codice fornito per l'input e l'output e non può essere modificato.

Provalo tu

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)!
// INIZIO DEI TEST DELL'API PUBBLICA
let apiOK = apiChecks(input)
// FINE DEI TEST DELL'API PUBBLICA
if apiOK { print(solve(input)) } else { print("API mismatch") }

Tutte le lezioni di Swift per sviluppatori C#

Esercitati da solo: Playground Swift