Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


find all possible concatenations of characters in a table

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Fri Sep 17, 2021 9:04 am    Post subject: find all possible concatenations of characters in a table Reply with quote

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
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 941

PostPosted: Fri Sep 17, 2021 11:49 am    Post subject: Reply with quote

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
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 138

Joined: 06 Jul 2014
Posts: 4275

PostPosted: Fri Sep 17, 2021 12:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
HexaG0n
Advanced Cheater
Reputation: 0

Joined: 29 Mar 2021
Posts: 64

PostPosted: Fri Sep 17, 2021 12:39 pm    Post subject: Reply with quote

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! Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites