Introduction to Core Graphics (iOS/Swift)

Leave a comment

What are Nibs?

Leave a comment

Onboarding with the UIPageViewController

Leave a comment

Google Code Jam – Swift reading input

Leave a comment

Once of the frustrating things about getting started with Google Coding Competition questions was the lack of examples on how to read the file input in Swift.

Each question requires you to read input from the command line, and then print the answer in a specific format.

I created this cheat sheet to help me read this input in so I could get on with the question. It really comes down to this.

import Foundation

func readInput() -> Int {
    return readLine().flatMap { Int($0) }!
}

func readInput() -> [Int] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Int($0)} }!
}

func readInput() -> (Int, Int) {
    let inputs = readInput() as [Int]
    return (inputs[0], inputs[1])
}

func readInput() -> Double {
    return readLine().flatMap { Double($0) }!
}

func readInput() -> [Double] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Double($0)} }!
}

func readInput() -> (Double, Double) {
    let inputs = readInput() as [Double]
    return (inputs[0], inputs[1])
}

func readInput() -> String {
    return readLine()!
}

func readInput() -> [String] {
    let str = readLine()!
    return str.components(separatedBy: " ")
}

var t: Int = readInput()

for i in 1..<t + 1 {
    let nm: (Int, Int) = readInput()
    print("Case #\(i): \(nm.0) \(nm.1)")
}

Here is what my solution looks like to the Retype question of Google 2020.

import Foundation

func readInput() -> Int {
    return readLine().flatMap { Int($0) }!
}

func readInput() -> [Int] {
    return readLine().flatMap { $0.split(separator: " ").compactMap { Int($0)} }!
}

func readInput() -> (Int, Int, Int) {
    let inputs = readInput() as [Int]
    return (inputs[0], inputs[1], inputs[2])
}

let restartTime = 1

func worstCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    let firstLevels = K - 1
    return firstLevels + N + restartTime
}

func possibleCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    let firstLevels = K - 1 // 4
    let backward = K - S // 3
    let forward = N - S
    
    return firstLevels + backward + forward + restartTime
}

func bestCase(_ N: Int, _ K: Int, _ S: Int) -> Int {
    return min(worstCase(N, K, S), possibleCase(N, K, S))
}

var t: Int = readInput()

for i in 1..<t + 1 {
    let nks: (Int, Int, Int) = readInput()
    print("Case #\(i): \(bestCase(nks.0, nks.1, nks.2))")
}

Hope this helps. Good luck!

Swift Deep Linking in iOS

Leave a comment

https://github.com/jrasmusson/swift-arcade/blob/master/Foundation/Deeplink/README.md

How to handle pluralization in iOS

Leave a comment

Navigation Patterns: How to setup your iOS architecture using a MainViewController

Leave a comment

How to write Unit Tests with Core Data in Swift

Leave a comment

How to safely work with Core Data on a background thread

Leave a comment

Core Data Codegen Explained

Leave a comment

Older Entries

%d bloggers like this: