Menu
CoddyTech

Working With Files

Lesson 1 of 48 in Coddy's Swift for Go Developers course.

Go console programs enter main in package main, while these Swift exercises compile sibling files as one module and execute top-level statements in main.swift. Run the supplied starter unchanged. Functions.swift supplies its free helper, and later named types appear in separate matching files. Compare Go value copies, shared slices and maps, interfaces and error returns with the specific Swift contracts introduced by each lesson.

The course uses standard console Swift without SwiftUI or a package manager. Import Foundation in each file that needs it; sibling Swift files do not need imports of their filenames. Go package imports and exported names provide a different organization and visibility model. Preserve the locked driver and return exactly the output text required by each editable API.

challenge icon

Challenge

Easy

Run the supplied program. Input is one ASCII name of 1 to 20 letters. welcome(_:) returns Hello, followed by that name. The driver prints it. All Swift files compile together; top-level statements belong only in main.swift. Unlike a Go main function in package main, this layout uses top-level statements in main.swift as the executable entry point. Free functions live in Functions.swift; later named types each have their own file.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
return welcome("Ada") == "Hello, Ada"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }

All lessons in Swift for Go Developers

Practice on your own: Swift playground