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

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.