100 Days of SwiftUI Solutions – Checkpoints

100 Days of SwiftUI solutions

One of the greatest resources on the Internet to learn Swift and SwiftUI is Paul Hudson’s 100 days of SwiftUI. If you have already learned Swift before and just wanted a quick refresher, I found it is quite helpful to go over this post essential Swift in one hour, and just quickly go over the checkpoints from day 1-14.

If you are looking for 100 days of SwiftUI solutions for checkpoints, the following are from my earlier practice solutions for the programming checkpoints 4-9 from day 1-14 for your reference.

Checkpoint 4

  • Problem link is here
enum SqrtError:Error {
    case OutOfBound, NoRoot
}

func getSqrt(_ number: Int) throws -> Int {
    if number < 1 || number > 10000 {
        throw SqrtError.OutOfBound
    }
    for i in 1...(number/2 + 1) {
        if i * i == number {
            return i
        }
    }
    throw SqrtError.NoRoot
    
}

func sqrtTest() {
    let n = 10005
    do {
        let res = try getSqrt(n)
        print("The sqrt roort is \(res)")
    }
    catch SqrtError.OutOfBound {
        print ("Out of bound")
    }
    catch SqrtError.NoRoot {
        print ("No root")
    }
    catch {
        print ("Unknown error")
    }
}

Checkpoint 5

  • Problem link is here.
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
let res = luckyNumbers.filter { !$0.isMultiple(of: 2)}
    .sorted {$0 < $1}
    .map { "\($0) is a lucky number"}
for r in res {
    print(r)
}

Checkpoint 6

  • Problem link is here
struct Car {
    enum Gears {
        case l1, l2, l3, l4
    }
    private let model: String
    private let numSeats: Int
    private var currGear: Gears = Gears.l1
    
    init(model: String, numSeats: Int) {
        self.model = model
        self.numSeats = numSeats
    }
    
    mutating func changeGearUp() {
        switch self.currGear {
        case Gears.l1:
            self.currGear = Gears.l2
        case Gears.l2:
            self.currGear = Gears.l3
        case Gears.l3:
            self.currGear = Gears.l4
        default:
            return
        }
        print("Changing to gear \(self.currGear)")
    }
    mutating func changeGearDown() {
        switch self.currGear {
        case Gears.l4:
            self.currGear = Gears.l3
        case Gears.l3:
            self.currGear = Gears.l2
        case Gears.l2:
            self.currGear = Gears.l1
        default:
            return
        }
        print("Changing to gear \(self.currGear)")
    }
    
}

var car = Car(model: "Honda", numSeats: 4)
print(car)
car.changeGearUp()
car.changeGearUp()
car.changeGearUp()
car.changeGearUp()
car.changeGearUp()
car.changeGearDown()
car.changeGearDown()
car.changeGearDown()

Checkpoint 7

  • Problem link is here
class Animal {
    let legs: Int
    init(legs: Int){
        self.legs = legs
    }
}

class Dog: Animal {
    override init(legs: Int) {
        super.init(legs:legs)
    }
    func speak() {
        print("I am a barking dog")
    }
}

class Cat: Animal {
    let isTame: Bool
    
    init(legs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(legs:legs)
    }
    func speak() {
        print("I am a speaking cat")
    }
}

class Corgi: Dog {
    override init(legs: Int) {
        super.init(legs:legs)
    }
    override func speak() {
        print("I am a barking Corgi")
    }
}

class Poodle: Dog {
    override init(legs: Int) {
        super.init(legs:legs)
    }
    override func speak() {
        print("I am a barking Poodle")
    }
}

class Persian: Cat {
    override init(legs: Int, isTame: Bool) {
        super.init(legs:legs, isTame: isTame)
    }
    override func speak() {
        print("I am a speaking Persian")
    }
}

class Lion: Cat {
    override init(legs: Int, isTame: Bool) {
        super.init(legs:legs, isTame: isTame)
    }
    override func speak() {
        print("I am a speaking Lion")
    }
}

var dog = Dog(legs: 4)
var corgi = Corgi(legs: 4)
var persian = Persian(legs: 4, isTame: false)
var lion = Lion(legs: 4, isTame: true)
dog.speak()
corgi.speak()
persian.speak()
lion.speak()

Checkpoint 8

  • Problem link is here
protocol Building {
    var numRoom: Int { get }
    var cost: Int { get set }
    var agentName: String { get set }
    func summary()
}

extension Building {
    func summary() {
        print("The building has \(numRoom) rooms, costs \(cost) dollars, and is handled by agent \(agentName)")
    }
}

struct House: Building {
    var numRoom: Int = 4
    var cost: Int = 0
    var agentName: String
}

struct Office: Building {
    var numRoom: Int = 20
    var cost: Int = 10000
    var agentName: String
}

var house = House(agentName: "John")
house.summary()
var office = Office(agentName: "Mary")
office.summary()

Checkpoint 9

func getRandomNumber(nums: [Int]?) -> Int {
    return nums?.randomElement() ?? Int.random(in: 1...100)
}
print(getRandomNumber(nums: [1,2,3,4]))
print(getRandomNumber(nums: nil))

Leave a ReplyCancel reply