TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

BuzzFizz. The return of FizzBuzz, new and improved?

Forums > 100 Days of SwiftUI

I thought that I would revisit the FizzBuzz challenge from the 100 Days of SwiftUI, following on from a comment from NigelGee on another post. In that post the challenge was extended to also include numbers that contained either a 3 or a 5 or both.

So the crtiteria I set were

  1. Must still use an enum, with a rawValue extraction.
  2. Run in the range of 1 to 1000, to show what happens in the 30s, 50s, 130s, 150s, … 300s, 500s, …
  3. Be as compact as possible, whilst still being reasonable to read and understand - so no one line wonder code that does everything in one line but is really hard to read.

Whether it should be FizzBuzz or BuzzFizz doesn't really matter, but I chose BuzzFizz just to be different to the tutorial. There are blank line to help in the readability

enum BuzzFizz: String {
    case fizz
    case buzz

    var name: String { self.rawValue.capitalized }
}

func multipleOrContains(input: Int, check: Int, result: BuzzFizz) -> String {
    input.isMultiple(of: check) || String(input).contains(String(check)) ? result.name : ""
}

for i in 1...1000 {
    var result = [String]()

    result.append(multipleOrContains(input: i, check: 3, result: .buzz))
    result.append(multipleOrContains(input: i, check: 5, result: .fizz))

    if result.allSatisfy({ $0.description == "" }) { result.append(String(i)) }

    print(Set(result).sorted(by: <).joined())
}

2      

You are doing a lot of unnecessary extra work there. There's no real reason to do all that stuff with an array and then converting the array to a Set and sorting and joining.

You can achieve the same results much more simply like this:

var result: String
for i in 1...1000 {
    //concat the results of our two function calls
    result = multipleOrContains(input: i, check: 3, result: .buzz) +
             multipleOrContains(input: i, check: 5, result: .fizz)
    //so now result will either be "Buzz" or "Fizz" or "BuzzFizz" or ""

    //if result is "", we just plug in the value of i
    if result.isEmpty { result = "\(i)" }

    print(result)
}

I don't think concatenating two String results from the function calls violates your 3rd criterion, but your mileage may vary.

3      

Yes, your solution is neater.

I love messing around with arrays, sets , .map and .filter.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.