UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Swift Coding Challenges

Forums > Books

Solution from the book:

func challenge2b(input: String) -> Bool {
let lowercased = input.lowercased()
return String(lowercased.reversed()) == lowercased
}

which can be further optimised by eliminating extra constant declaration. Original string will be left unaffected anyways

func challenge2b(input:String) -> Bool{

    return input.lowercased() == String(input.lowercased().reversed())

}

3      

I would say that is not really an optimization. The line is too long and more importantly you now need to do the lowercase conversion twice instead of single time.

3      

@nemecek-filip may be... I am coding in C mostly with further assembly optimizations for speed. OOP and Swift are very new to me but I`m trying to change the way I use to think :)

Any feedback is very appriciated!

Coding challenges Book is very good so far...solutions are awesome too.

3      

Challenge 4 Solution from the book

extension String {
func fuzzyContains(_ string: String) -> Bool {
return range(of: string, options: .caseInsensitive) != nil
} }

LocalizedStandardRange is doing same thing i.e. case insensitiveness without explicitly asking for it via options and this time line is shorter @nemecek-filip.

localizedStandardRange(of:) Finds and returns the range of the first occurrence of a given string within the string by performing a case and diacritic insensitive, locale-aware search.

extension String{
    func fuzzyContains(_ input:String) -> Bool {
        localizedStandardRange(of: input) != nil
    }
}

3      

@twostraws  Site AdminHWS+

@ioprojecton As I say in the intro to the book, Swift Coding Challenges tries to simultaneously optimize for a variety of things, which is tricky. So, the book isn't always trying to show you the most performant solution, because I'm also trying to give folks solutions that can easily be remembered when you're sitting in an interview – and that's not easy. That doesn't mean I present slow solutions – in fact, often I present multiple with increasingly optimized performance characteristics – but it does mean there's always a trade off between "how fast is this" and "can I remember this when standing in front of a whiteboard"!

I hope that helps 😃

3      

Got it! this book is amazing! explanations of solutions are also sometimes mindblowing.

3      

I liked the multiple solutions provided for challenge 5. However, my solution wasn't one of them:

func challenge5(input: String, char: Character) -> Int {
    let parts = input.components(separatedBy: String(char))
    return parts.count - 1
}

3      

This is what I came up with for challenge 10:

func challenge10(input: String) -> (vowels: Int, consonants: Int) {

    let lowered = input.lowercased()
    let vowels = lowered.filter { "aeiou".contains($0) }.count
    let consonants = lowered.filter { "bcdfghjklmnpqrstvwxyz".contains($0) }.count

    return (vowels, consonants)
}

3      

Your answer for challenge 23 gave me a clue for solving challenge 24. Using CharacterSet, it is quite easy to get the numbers (as strings) out of the mixed string:

let numberStrings = input.components(separatedBy: CharacterSet.decimalDigits.inverted)

3      

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 April 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.