WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 28, Project 5: isPossible() does not fail when reusing letters

Forums > 100 Days of Swift

The following function, as presented in the course, should return false when a letter is reused. Did I miss something?

func isPossible(word: String) -> Bool {
        guard var tempWord = title?.lowercased() else { return false }

        for letter in word {
            if let position = tempWord.firstIndex(of: letter) {
                tempWord.remove(at: position)
            } else {
                return false
            }
        }
        return true
    }

1      

Not really sure what you mean.

Say the word you are given is "deflects". Your first suggestion is "elect". Let's walk through it...

func isPossible(word: String) -> Bool { //1
    guard var tempWord = title?.lowercased() else { return false } //2

    for letter in word { //3
        if let position = tempWord.firstIndex(of: letter) { //4a
            tempWord.remove(at: position) //5
            //6
        } else {
            return false //4b
        }
    }

    return true //7
}
  1. word is set to "elect"
  2. tempWord is set to "deflects"
  3. we loop through every letter in "elect" 4a. if letter is found in word...
  4. remove letter from tempWord
  5. and keep looping 4b. if letter is not found, return false from isPossible
  6. if we make it through the loop, then isPossible is true

So, using "deflects" and "elects":

  • is e found in "deflects"? yes, so remove it, leaving tempWord as "dflects"
  • is l found in "deflects"? yes, so remove it, leaving tempWord as "dfects"
  • is e found in "deflects"? yes, so remove it, leaving tempWord as "dfcts"
  • is c found in "deflects"? yes, so remove it, leaving tempWord as "dfts"
  • is t found in "deflects"? yes, so remove it, leaving tempWord as "dfs"
  • we're done looping through "elect" so return true

We can see that even though the e is reused in "elect", our function still works as expected.

If a letter is reused in the player's word but it doesn't appear more than once in the given word, then isPossible will return false because at some point tempWord will no longer contain that letter.

So if the user tries "selects", this happens:

letter  tempWord
s       deflect
e       dflect
l       dfect
e       dfct
c       dft
t       df
s       **fail**

1      

Ok, I'm an idiot. I misunderstood what was meant to happen there. I was thinking that after I used a letter it would be unavailable for subsequent answers, but obviously 'tempWord' is going to be destroyed and reused each time the function in run.

Thanks for the help.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

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.