LAST CHANCE: Save 50% on all my Swift books and bundles! >>

@metropol: Day 14 Updates

Forums > 100 Days of SwiftUI

Hi all. Started the 100 days yesterday.

Here's my fizz buzz solution for checkpoint 3.

enum FizzBuzz {
    case Fizz
    case Buzz
    case FizzBuzz
    case none
}

var numIs = FizzBuzz.none

for i in 1...100 {
    numIs = .none

    if i.isMultiple(of: 3) {
        numIs = .Fizz
    }
    if i.isMultiple(of: 5) {
        numIs = numIs == .Fizz ? .FizzBuzz : .Buzz
    }

    switch numIs {
    case .Fizz:
        print("Fizz")
    case .Buzz:
        print("Buzz")
    case .FizzBuzz:
        print("FizzBuzz")        
    case .none:
        print(i)
    default:
        fatalError("Unsupported")
    }
}

2      

Does posting solutions to checkpoints qualify as knowledge sharing?

That was the intention for this post, apart from being the first post to say "hi" to the community.

Anyways, posting in a comment my solution for checkpoint 4.

import Foundation

enum myError: Error {
    case outOfBounds, noRoot
}

func integerSqrt(_ number: Int) throws -> Int {
    if number < 1 || number > 10000 {
        throw myError.outOfBounds
    }

    for i in 1...100 {
        if i*i == number {
            return i 
        }
    }

    throw myError.noRoot
}

let numbers = [1, 25, 5000, 8100, 10000]
for n in numbers {
    do {
        let result = try integerSqrt(n)
        print("integer sqrt of \(n) is \(result)")
    } catch myError.outOfBounds {
        print("\(n) is out of bound")
    } catch myError.noRoot {
        print("\(n) has no integer square root")
    }
}

2      

And checkpoint 5

import Foundation

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

for num in luckyNumbers.filter { !$0.isMultiple(of: 2) }.sorted { $0 < $1 }.map { "\($0) is a lucky number" } {
    print(num)
}

2      

Just a little note on the FizzBuzz the switch statement does not need the default: as numIs is type FizzBuzz and you have all the cases covered. You only need the default: if you can not assign all cases eg a String or numbers etc.

You also might want to look at naming numIs not very readable but interesting way to do the project i have just tidy thing up a little bit and more readable

enum FizzBuzz {
    case Fizz
    case Buzz
    case FizzBuzz
    case number
}

var isNumber: FizzBuzz = .number

for i in 1...100 {
    if i.isMultiple(of: 5) && i.isMultiple(of: 3) {
        isNumber = .FizzBuzz
    } else if i.isMultiple(of: 3) {
        isNumber = .Buzz
    } else if i.isMultiple(of: 5) {
        isNumber = .Fizz
    } else {
        isNumber = .number
    }

    switch isNumber {
    case .Fizz:
        print("Fizz")
    case .Buzz:
        print("Buzz")
    case .FizzBuzz:
        print("FizzBuzz")
    case .number:
        print(i)
    }
}

Also the real challenge is that also print Fizz if it has a Five in the number and Buzz if has 3 in the number eg 13 would also be Buzz and 50 would be Fizz, 53 would be FizzBuzz

Just a side question I thought FizzBuzz was 5(Fizz) and 7(Buzz) to make FizzBuzz. With 3(Buzz) and 5(Fizz) it would be BuzzFizz!!!

2      

Indeed the default was not needed in this case. Maybe the challenge was updated over time? I see 3x are for Fizz and 5x for Buzz. Thanks for the feedback!

2      

Solution to Checkpoint 6 in Day 11.

Gear 0 is parking.

import Cocoa

struct Car {
    let model: String
    let seats: Int
    let minGear = 0
    let maxGear = 6
    private(set) var gear = 0 {
        didSet {
            print("gear = \(gear)")
        }
    }

    enum GearShift {
        case up, down
    }

    mutating func shiftGear(_ direction: GearShift) {
        switch direction {
        case .up:
            if gear < maxGear {
                gear += 1
            } else {
                print("reached highest gear")
            }
        case .down:
            if gear > minGear {
                gear -= 1
            } else {
                print("reached lowest gear")
            }
        }
    }
}

2      

Solution to Checkpoint 7 in Day 12.

import Cocoa

class Animal {
    let legs: Int

    init(legs: Int) {
        self.legs = legs
    }
}

class Dog: Animal {

    init() {
        super.init(legs: 4)
    }

    func speak() {
        print("bau")
    }
}

class Cat: Animal {
    let isTame: Bool

    init(isTame: Bool) {
        self.isTame = isTame
        super.init(legs: 4)
    }

    func speak() {
        print("miao")
    }
}

class Corgi: Dog {

    override func speak() {
        print("corgi bau. I have \(legs) legs")
    }
}

class Poodle: Dog {
    override func speak() {
        print("poodle bau. I have \(legs) legs")
    }
}

class Persian: Cat {

    init() {
        super.init(isTame: true)
    }

    override func speak() {
        print("persian miao. I have \(legs) legs")
    }
}

class Lion: Cat {

    init() {
        super.init(isTame: false)
    }

    override func speak() {
        print("groar. I have \(legs) legs")
    }
}

let dog1 = Corgi()
let dog2 = Poodle()
let cat1 = Persian()
let cat2 = Lion()

dog1.speak()
dog2.speak()
cat1.speak()
cat2.speak()

2      

Solution for Checkpoint 8 of Day 13.

I should have played more with House and Office, and add some properties and methods. Without doing that, the two structs are too similar and it feels like there's no point in creating different structures.

import Cocoa

protocol Building {
    var rooms: Int { get }
    var cost: Int { get set }
    var agent: String { get set }

    func summary(for summary: String )
}

struct House: Building {
    let rooms: Int
    var cost: Int
    var agent: String

    init( rooms: Int, cost: Int, agent: String) {
        self.rooms = rooms
        self.cost = cost
        self.agent = agent
    }

    func summary(for summary: String) {
        print("House: \(summary)")
        print("number of rooms: \(rooms)")
        print("cost in dollars: $\(cost)")
        print("estate agent: \(agent)")
    }
}

struct Office: Building {
    let rooms: Int
    var cost: Int
    var agent: String

    init( rooms: Int, cost: Int, agent: String) {
        self.rooms = rooms
        self.cost = cost
        self.agent = agent
    }

    func summary(for summary: String) {
        print("Office: \(summary)")
        print("number of rooms: \(rooms)")
        print("cost in dollars: $\(cost)")
        print("estate agent: \(agent)")
    }
}

var myHome = House(rooms: 3, cost: 1000000, agent: "Nicolas")
var myOffice = Office(rooms: 20, cost: 10000000, agent: "Frank")

myHome.summary(for: "my sweet home")
myOffice.summary(for: "AI")

myHome.cost = 200000
myHome.summary(for: "After renovation")

2      

Day 14. Checkpoint 9 solution.

import Foundation

func randomChoice(sequence: [Int]?) -> Int {
    sequence?.randomElement() ?? Int.random(in: 1...100)
}

print(randomChoice(sequence: nil))
print(randomChoice(sequence: [1,2,3,4,5,6,7,8,9,10]))

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.