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
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
0000000000000000000000004But, 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))
}