You can't do this with just for loops (unless you know specifically how long it will always be -- e.g., 5)
The most straightforward way to do this is recursion.
Let's call a n-string a string made of the alphabet that is n characters long.
Then we know all of the 1-strings -- they are just all of the letters in chars.
What are all of the 2-strings? Well, they're any two letters in chars. But we can say something a little nicer:
A 2-string is any one character from chars followed by any of the 1-strings.
We can use this definition in general:
A k-string is any one character from chars followed by a k-1-string
The code to get all of them, then, would look something like this:
chars = "abcd"
function kstrings( k )
if k == 0 then
return {""}
else
local kminus1s = kstrings( k - 1 )
local result = {}
for i = 1, #chars do
for _, km1 in pairs(kminus1s) do
table.insert(result, chars:sub(i, i) .. km1 )
end
end
return result;
end
end
print( kstrings(3) )
Warning: This will be very, very slow. The result will be #chars^k strings. If you're using uppercase and lowercase and you're just inputting 5, that's 380 million. Each will be 5 chars long, so this will take at least 1.9 GB to store[^1].
Because of the massive memory requirement, you probably won't be able to generate a list of all of them.
A better approach? (edited)
Instead, we can make a "successor" of a string by counting up repeatedly in base-#chars.
function successor(list, base, at)
at = at or #list;
list[at] = list[at] + 1
if list[at] > base then
list[at] = 1
if at > 1 then
return successor(list, base, at - 1);
end
else
return true;
end
end
function numsToString(list, char)
local r = ""
for i = 1, #list do
r = r .. char:sub(list[i], list[i])
end
return r
end
local chars = "abc"
local k = {1, 1, 1, 1}
repeat
print(numsToString(k, chars));
until not successor(k, #chars);
This will still have to consider a very large number of strings, but it does it in much less memory, so this will actually be able to be usable (just extremely slow)
[^1]:
Actually, it's might not be that bad, as long as you're really using strings. But it will still be really bad (and the less memory use will come at a high performance cost)
5 comments
Do you mean combination or permutation? Combinations are where order doesn't matter, so {1, 2} is the same as {2, 1}. Permutations are where order does matter so {1, 2} is distinctly different from {2, 1}.
Combination. [As I said, I didn't get really close :( ]
Not sure what you're trying to do and it's probably above me, but I would like to point out that all those semicolons are not needed in Lua and make it kind of annoying to read.
@Perci1 I know they're not needed, I just got use to them because of other coding/scripting languages. Anyway, I removed some extra stuff so it's easier to read.
Good luck. I used the combinations formula to try and do what you want, but the amount of characters you have is way too large. It caused number overflows and froze because it will require billions of looping.