BLACK FRIDAY: Save 50% on all books and bundles! >>

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())

}

   

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.

   

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

   

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
    }
}

   

@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 😃

   

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

   

Save 50% in my Black Friday sale.

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

Not logged in

Log in
 

Link copied to your pasteboard.