Menu
CoddyTech

Numbers and Conversion

Lesson 3 of 48 in Coddy's Swift for Lua Developers course.

Lua 5.4 distinguishes integer and floating-point numeric values. Integer width is a build choice; the course runtime uses 64-bit integers. Integer overflow wraps, whereas ordinary overflowing Swift Int arithmetic traps. Neither is an unlimited-precision integer, so respect the task bounds.

Lua floor division // rounds down, and its % remainder follows that floor quotient. Swift integer division truncates toward zero, so negative operands expose a difference. Swift needs explicit Double conversion for a fractional quotient. Converting an already truncated quotient afterwards cannot restore its fraction.

The examples divide -7 by 2. Lua floor division produces -4, while Swift integer division produces -3; the floating-point quotient is -3.5. The exercise uses nonnegative minutes, where quotient and remainder can split the units without this sign distinction.

Complete Lua comparison program:

print(-7 // 2)
print(-7 / 2)
print(math.maxinteger > 0)

Complete Swift comparison program, compiled from these files:

main.swift

print(-7 / 2)
print(Double(-7) / 2)
print(Int.max > 0)
challenge icon

Challenge

Easy

Implement clockParts(_ minutes: Int) -> String. Input is total minutes, 0 through 10000. Return whole hours, a colon, then remaining minutes, without zero padding.

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 clockParts(181) == "3:1" && clockParts(60) == "1:0"
}
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") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for Lua Developers

Practice on your own: Swift playground