NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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.

   

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
}

   

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

   

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)

   

Hacking with Swift is sponsored by Emerge

SPONSORED Emerge helps iOS devs write better, smaller apps by profiling binary size on each pull request and surfacing insights and suggestions. Companies using Emerge have reduced the size of their apps by up to 50% in just the first day. Built by a team with years of experience reducing app size at Airbnb.

Set up a demo!

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

Reply to this topic…

You need to create an account or log in to reply.

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.