combinations for password

hello guys and girls , i need a help for making a an single app for listing all combiniations
i know the algortihm but i don't know how to code it




it's simple


just counting



the chars list is = " 0 to 9 and a to z and A to Z "
and lenght is 25
and it will start from 0000000000000000000000000 and it will finish by ZZZZZZZZZZZZZZZZZZZZZZZZZ

thank you for you help

Answered by DTS Engineer in 276157022

You can generate arbitrarily long sequences like this lazily using the code shown below. For example:

for s in digitCombinations(ofLength: 25).prefix(5) {
    print(s)
}

prints:

0000000000000000000000000
0000000000000000000000001
0000000000000000000000002
0000000000000000000000003
0000000000000000000000004

But, yeah, if you try to enumerate all such sequences you will definitely be waiting around until the end of time!

ps I wasted way too much time playing around with this yesterday )-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
func combinations<Elements, Element>(
    ofLength length: Int,
    elements: Elements
) -> AnySequence<[Element]> where Elements : Collection, Elements.Element == Element {
    if length == 0 {
        return AnySequence(CollectionOfOne([]))
    }
    let prefixes = combinations(ofLength: length - 1, elements: elements)
    return AnySequence(prefixes.lazy.flatMap { (prefix) in
        elements.lazy.map { (element) in
            // Uncomment this to check that everything is happening lazily.
            // print("tick")
            return prefix + [element]
        }
    })
}

func digitCombinations(ofLength length: Int) -> AnySequence<String> {
    func combine(_ digits: [UnicodeScalar]) -> String {
        return String(digits.map{ Character($0) })
    }
    let digits = [UnicodeScalar]("0123456789".unicodeScalars)
    return AnySequence(combinations(ofLength: length, elements: digits).lazy.map(combine))
}

We do not see the algorithm, so it is dificult to help.


But I guess you want to build all the patterns of 25 chars .


Did you see that the number of combinations will be stronomical :

if you have 25 characters based on 62 characters (chars list is = " 0 to 9 and a to z and A to Z ") based on your char list.


That means 62 ^^25 (which is rougly 6.4 * 10^^44 (1 followed by 44 zeros, absolutely huge).


What do you want to do with it ? How do you want to display ? How do you expect to handle ?

If you just need counting, this calculation gives you the result: 62 ^^25 (which is rougly 6.4 * 10^^44)


To illustrate how huge it is, imagine you want to print all combinations on paper, one per line of 1 mm. If my computation is correct, that would mean a paper length of 6 10^^25 light years, much more than the size of the universe which is a mere 15 Billions light years (1.5 10^^10 light years). So it is 4 Million billions times the size of universe for the length of paper role for printing all permutations on 1mm lines !

And the time to test all combinations would be more than the age of universe too !


And the algorithm for creating all the combinations would never end (even with the fastet computer, be prepared to wait for trillions years).


So, you need to clarify your intention : how do you plan to handle those combinations.

Accepted Answer

You can generate arbitrarily long sequences like this lazily using the code shown below. For example:

for s in digitCombinations(ofLength: 25).prefix(5) {
    print(s)
}

prints:

0000000000000000000000000
0000000000000000000000001
0000000000000000000000002
0000000000000000000000003
0000000000000000000000004

But, yeah, if you try to enumerate all such sequences you will definitely be waiting around until the end of time!

ps I wasted way too much time playing around with this yesterday )-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
func combinations<Elements, Element>(
    ofLength length: Int,
    elements: Elements
) -> AnySequence<[Element]> where Elements : Collection, Elements.Element == Element {
    if length == 0 {
        return AnySequence(CollectionOfOne([]))
    }
    let prefixes = combinations(ofLength: length - 1, elements: elements)
    return AnySequence(prefixes.lazy.flatMap { (prefix) in
        elements.lazy.map { (element) in
            // Uncomment this to check that everything is happening lazily.
            // print("tick")
            return prefix + [element]
        }
    })
}

func digitCombinations(ofLength length: Int) -> AnySequence<String> {
    func combine(_ digits: [UnicodeScalar]) -> String {
        return String(digits.map{ Character($0) })
    }
    let digits = [UnicodeScalar]("0123456789".unicodeScalars)
    return AnySequence(combinations(ofLength: length, elements: digits).lazy.map(combine))
}

tell me how to create this app ? it's so important to me
i wanna make this app
tell me how it is
thanks a lot
give me a code , not a talk
and i am not going to print it and i need some of it

You got the code, from Quinn. (very nice code, worth studying)


However, I do believe the "talk" will be useful because, whatever you intend to do with it, any code to generate all combinations will lead you nowhere.


You'll have probably to experiment by yourself, but don't be surprised when it "does not work", which means func never completes.

Interesting recursive code.


How is it that printing starts immediately, not waiting till all combinations are computed ? Is it thanks to lazy ?

Otherwise, even a short sequence ofLength = 4 takes 5 seconds to compute, ofLength = 5 takes 56 seconds to compute, and increase exponentially by a factor of 10 (logically) when ofLength increase by 1.


So how is exactly lazy working here ? I didn't know this syntax prefixes.lazy.flatMap

Interesting recursive code.

Yeah, like I said, I spent way too much time on this (-:

How is it that printing starts immediately, not waiting till all combinations are computed ? Is it thanks to lazy ?

Yes. Originally I missed one of the

lazy
s (the one on line 23) and the whole thing ran eagerly. When dealing with
lazy
it’s always a good idea to actually verify that the stuff you think is running lazily is actually running lazily. Hence my tick test (line 12).

I didn't know this syntax prefixes.lazy.flatMap

In this context

flatMap
is running the closure, each invocation of which generates a sequence, and then creates a single sequence but expanding out those sequences. If you were working with arrays it would convert
[[1,2], [3,4]]
to
[1, 2, 3, 4]
. The
lazy
means that this work is done lazily, so elements of the final sequence are only generated as the sequence is iterated.

btw I very much doubt that this is the most efficient way to do this. I built an efficient version of this using

sequence(state:next:)
and I strongly suspect it would be radically faster. I did not, however, sit down to profile it, so my gut reaction might be completely wrong. The ability of the Swift compiler to inline stuff from the standard library can make stuff that’s ‘obviously’ slow run unexpectedly quickly.

However, I do believe the "talk" will be useful because, whatever you intend to do with it, any code to generate all combinations will lead you nowhere.

I totally agree with you here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

thank you for all of your helps

combinations for password
 
 
Q