Nested cycles don't stop (password brute-force simulation)

hi there. Im learning Swift via Apple Education Books with tasks. It is brute force simulation for 4-symbol passwords. Nested FOR-in cycles asking string letters for right combination.
I wrote fine algorithm for it, but have one problem. It's not stopping, when find right combination.
I mean, its print it for example on 500k iteration , but still looping to 2-4 millions. (smth about 3-5 min).
I tried all combinations I've known. guards, ifs with breaks, return without anything and return with func change. its final try with commented lines with Outerloop with I can't basically understand, but its still looping.
Any ideas? I haven't .
I ADDED SCREENSHOT TO THE POST, BECAUSE FORMATTING LOOKS AWFUL


import Foundation

let digits = "0123456789"
let punctuation = "!@#$%^&*(),.<>;'`~{}\|/?_-+= "
let lowercaseAlphas = "abcdefghijklmnopqrstuvwxyz"
let uppercaseAlphas = lowercaseAlphas.uppercased()

func v2passwordIsCorrect(_ password: String) -> Bool {
return password == "hZ79"
}

func guessPasswordOf4Char(containing characters: String) {
var password: String = ""
// var flag = false

outerLoop: for a in characters {
for b in characters {
for c in characters {
for d in characters {
password = String(a) + String(b) + String(c) + String(d)

                   if v2passwordIsCorrect(password)  {
                       print("Found password: \(password)")
                       
                       // The return statement below means that the function exits
                       // early when the password is guessed, rather than executing
                       // all loops to completion.
                       
                      // flag = true
                      // guard flag == false else { return   }
                       break outerLoop
                       // return
                   }
               //if flag == true   { break }
            }
            //if flag == true   { break }
        }
       // if flag == true   { break }
    }
   // if flag == true   { break }
}

}

guessPasswordOf4Char(containing: lowercaseAlphas + uppercaseAlphas + digits)

Your formatting looks awful because you need to use Markdown code formatting. You have several choices. Either indent each line of your code by four extra spaces or you can use lines of three backticks to bookend a code block.

```
let digits = "0123456789"

```

will appear as

let digits = "0123456789" 

Or you can highlight a block and use the preformatted text button with the </> label (or it's keyboard shortcut e).

1 Like

@bumpagram You mean it's printing it in the console below at 500k iteration and the number on the right that says x times is still running?

yes. look at the screenshot. it has 1 768 655 times (1.76 million iterations). I can change password to find and it can be up to 4 million iterations total. And then cycle will stop.
but problem is - I need to stop this at the moment, when It finally find right password.
ridiculous .
print. - executed
other things like break - no executed

You can label the first loop statement and then break out of it.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#Labeled-Statements

outerLoop: for a in characters {
   for b in characters {
      …
      break outerLoop
   }
}

Do you see the same issue in release optimized builds? I wonder if it’s an ARC issue in debug builds.

I think this is a bug with Xcode playgrounds. Try quitting and reopening Xcode. Also, you can long-click the run/stop button and change it from "Automatically Run" to "Manually Run", and I have found that to be more reliable.

I tried outerloop and nothing happens. u can see in not in screenshot, but in code in this post. ( ya, bad format, sorry). I added. nothing.
I added Bool flag. nothing happens.
tried guard with flag, IF/Else and etc.
tried reboot Mac and Xcode. nothing.

I don't understand question about builds, but I just use Xcode from AppStore.
yesterday it goes new update for Xcode. I downloaded it and , well, nothing happens. :smiley:

looks like im too stupid what's going on , haha

ty for reply, but I tried all of this before creating the post. not working

There's no issue with the code regardless if using loop labels, break flags, direct return, continues or any other method to break early from loops.
Given how your array of characters is set up, the inner if check will execute 1868060 times before being true regardless if you run this in a playground or a normal app.
What happens is that the console gives you the result of the program from a compiled wrapper app and the playground UI doesn't keep up and shows you for academic purposes how those loops iterate. Essentially the UI is lagging behind because it artificially inserts some code in your program to show you information about state and that inserted code slows the whole program down. It's exactly like you would add a print just before your if - it will slow down just to print to console

You will NOT find out from a playground how fast any code executes because of that. You should do a Mac console app and compile it for release for that

But anyway, to test this out add a var increment = 0 just before your for loops, then modify the if as follows:

increment += 1
if v2passwordIsCorrect(password) {
    print(increment)
    print("Found password: \(password)")

    return
}

run this in playground and you'll see a seemingly early console print

1868060
Found password: hZ79

while the playground UI is still iterating until it reaches 1868060 times

3 Likes