mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Added a function to return all permutations of a
string
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
--luaClasses/Algorithms.lua
|
||||
--Matthew Ellison
|
||||
-- Created: 2-4-19
|
||||
--Modified: 2-7-19
|
||||
-- Created: 02-04-19
|
||||
--Modified: 03-24-19
|
||||
--This is a file of algorithms that I have found it useful to keep around at all times
|
||||
|
||||
|
||||
@@ -191,3 +191,76 @@ function getProd(ary)
|
||||
--Return the product of all elements
|
||||
return prod;
|
||||
end
|
||||
|
||||
function getPermutations(master, num)
|
||||
--If no num was given make it 0
|
||||
local num = num or 1
|
||||
local perms = {};
|
||||
--Check if the number is out of bounds
|
||||
if((num > string.len(master)) or (num < 0)) then
|
||||
--Do nothing and return an empty list
|
||||
perms = {};
|
||||
--If this is the last possible recurse just return the current string
|
||||
elseif(num == string.len(master)) then
|
||||
perms[#perms + 1] = master;
|
||||
--If there are more possible recurses, recurse with the current permutations
|
||||
else
|
||||
local temp = getPermutations(master, num + 1);
|
||||
--Add the elements of temp to perms
|
||||
for loc = 1, #temp do
|
||||
perms[#perms + 1] = temp[loc]
|
||||
end
|
||||
--You need to swap the current letter with every possible letter after it
|
||||
--The ones needed to swap before will happen automatically when the function recurses
|
||||
for cnt = 1, (string.len(master) - num) do
|
||||
|
||||
--Swap two elements
|
||||
--Turn it into byte array so you can change values
|
||||
local tempStr = {};
|
||||
for loc = 1, #master do
|
||||
tempStr[loc] = string.byte(master:sub(loc, loc));
|
||||
end
|
||||
|
||||
--Swap the two elements
|
||||
local tempChar = tempStr[num];
|
||||
tempStr[num] = tempStr[num + cnt];
|
||||
tempStr[num + cnt] = tempChar;
|
||||
--Change it back to a string
|
||||
master = "";
|
||||
for loc = 1, #tempStr do
|
||||
master = master .. string.char(tempStr[loc]);
|
||||
end
|
||||
|
||||
--Get the permutations after swapping two elements
|
||||
temp = getPermutations(master, num + 1);
|
||||
--Add the elements of temp to perms
|
||||
for loc = 1, #temp do
|
||||
perms[#perms + 1] = temp[loc]
|
||||
end
|
||||
|
||||
--Swap the elements back
|
||||
--Turn it into byte array so you can change values
|
||||
tempStr = {};
|
||||
for loc = 1, #master do
|
||||
tempStr[loc] = string.byte(master:sub(loc, loc));
|
||||
end
|
||||
--Swap the two elements
|
||||
tempChar = tempStr[num];
|
||||
tempStr[num] = tempStr[num + cnt];
|
||||
tempStr[num + cnt] = tempChar;
|
||||
--Change it back to a string
|
||||
master = "";
|
||||
for loc = 1, #tempStr do
|
||||
master = master .. string.char(tempStr[loc]);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
|
||||
if(num == 1) then
|
||||
table.sort(perms);
|
||||
end
|
||||
|
||||
--Return the list
|
||||
return perms;
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user