vlatkovski

All possible string combinations

Apr 18th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.71 KB | None | 0 0
  1. ----------------------------------------------------------------------------------
  2. --BlueTaslem's version
  3.  
  4.  
  5. -- Returns a list of strings (each of length k)
  6. -- (Works inductively)
  7. function kstrings(chars, k)
  8.     if (k == 0) then -- There is only one length 0 string:
  9.         return {""};
  10.     end
  11.  
  12.     local function vfind(t,v)
  13.         for i,v1 in next, t do if v1 == v then return v1; end; end;
  14.     end;
  15.  
  16.     local kminus1s = kstrings(chars, k-1); -- all (k-1)-strings
  17.     local result = {};
  18.     for i = 1, #chars do
  19.         -- chars[i] followed by each (k-1)-string
  20.         for _,km1 in pairs(kminus1s) do
  21.             if (not vfind(result,chars:sub(i, i)..km1)) then
  22.                 table.insert(result,chars:sub(i, i)..km1);
  23.             end;
  24.         end;
  25.     end;
  26.     return result;
  27. end;
Advertisement
Add Comment
Please, Sign In to add comment