Menu
CoddyTech

Numbers and Conversion

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

Kotlin numeric conversions such as toDouble() are explicit. Swift also rejects implicit conversion between stored numeric types: construct Double from an Int when a fractional result is needed. Integer division truncates toward zero in both languages, so converting after division cannot recover a discarded fraction. Swift integer literals can be inferred from their context, but that convenience does not mean a variable of type Int can be passed wherever a Double is required. Ordinary arithmetic checks overflow rather than silently wrapping; stay within the stated bounds of each exercise. Use parentheses to make the conversion point clear.

Kotlin

fun main() {
    val a = 7
    val b = 2
    println(a / b)
    println(a.toDouble() / b)
}

Swift

let a = 7
let b = 2
print(a / b)
print(Double(a) / Double(b))
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 Kotlin Developers

Practice on your own: Swift playground