|
Cheat Engine The Official Site of Cheat Engine
|
View previous topic :: View next topic |
Author |
Message |
HexaG0n Advanced Cheater Reputation: 0
Joined: 29 Mar 2021 Posts: 64
|
Posted: Fri Sep 17, 2021 9:04 am Post subject: find all possible concatenations of characters in a table |
|
|
How can I find all possible concatenations of characters in a table [ in a table ] and display them as strings with new lines?
Example, I have a array of tables with characters:
Code: | c={
{'1','2','3'},
{'a','b','c'},
{'A','B','C'}
}
|
I want to find all possible concatenations like
Quote: | 1aA
1aB
1aC
-- second table second character
1bA
1bB
1bC
-- second table third character
1cA
1cB
1cC
-- first table second character
2aA
2aB
2aC
-- and so on...
|
There could be many tables with however much characters aswell so... really appreciate the help!
edit:
I think you can call it:
Get all permutations of array of arrays containing characters?
|
|
Back to top |
|
|
panraven Grandmaster Cheater Reputation: 55
Joined: 01 Oct 2008 Posts: 941
|
Posted: Fri Sep 17, 2021 11:49 am Post subject: |
|
|
May try this
Code: |
--
local function reduce(t, init, fn)
for i=1,#t do init=fn(init, t[i], i)end
return init
end
local function map(t, fn)
local r = {map=map,reduce=reduce}
for i=1,#t do r[i]=fn(t[i], i)end
return r
end
local function perm(choices, cb)
local orders = map(choices, function(a) return #a end)
local max = reduce(orders, 1, function(a,b)return a*b end)
for i=1,max do
local n, cidx = i-1
local p = map(orders, function(ord, oidx)
cidx, n = n % ord, math.floor(n / ord)
return choices[oidx][1+cidx]
end)
local ret = table.pack(cb(table.unpack(p)))
if #ret>0 then return table.unpack(ret,1,ret.n)end
end
end
-- test
perm({{'x','y'},{1,2,3},{'Y','X'}},print) -->print all possible choices
print()
print(perm({{'x','y'},{1,2,3},{'Y','X'}},function(...)-- return something to break the loop
local p1,p2,p3 = ...
if p1:lower()==p3:lower() and p2==2 then return ... end -- break
print('looping:',...) -- continue loop
end))
|
_________________
- Retarded. |
|
Back to top |
|
|
ParkourPenguin I post too much Reputation: 138
Joined: 06 Jul 2014 Posts: 4275
|
Posted: Fri Sep 17, 2021 12:07 pm Post subject: |
|
|
Code: | function permuteConcat(t)
local index = {}
for i = 1, #t-1 do
index[i] = 1
end
index[#t] = 0
return function()
for i = #index, 1, -1 do
if index[i] >= #t[i] then
if i == 1 then return end
index[i] = 1
else
index[i] = index[i] + 1
break
end
end
local str = {}
for i,v in ipairs(index) do
str[#str+1] = t[i][v]
end
return table.concat(str,'')
end
end
-- example usage:
for str in permuteConcat{ {'1','2','3'}, {'a','b','c'}, {'A','B','C'} } do
print(str)
end
|
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
|
HexaG0n Advanced Cheater Reputation: 0
Joined: 29 Mar 2021 Posts: 64
|
Posted: Fri Sep 17, 2021 12:39 pm Post subject: |
|
|
ParkourPenguin wrote: | Code: | function permuteConcat(t)
local index = {}
for i = 1, #t-1 do
index[i] = 1
end
index[#t] = 0
return function()
for i = #index, 1, -1 do
if index[i] >= #t[i] then
if i == 1 then return end
index[i] = 1
else
index[i] = index[i] + 1
break
end
end
local str = {}
for i,v in ipairs(index) do
str[#str+1] = t[i][v]
end
return table.concat(str,'')
end
end
-- example usage:
for str in permuteConcat{ {'1','2','3'}, {'a','b','c'}, {'A','B','C'} } do
print(str)
end
|
|
Thank you so much, this works perfectly!
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|