BLACK FRIDAY: 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
    }

3      

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

3      

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.

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.