Added isPandigital function

This commit is contained in:
2021-10-11 12:58:11 -04:00
parent 4c426c5424
commit a6daca6619
2 changed files with 104 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
--luaClasses/Algorithms.lua
--Matthew Ellison
-- Created: 02-04-19
--Modified: 06-30-21
--Modified: 10-11-21
--This is a file of algorithms that I have found it useful to keep around at all times
--[[
Copyright (C) 2021 Matthew Ellison
@@ -419,3 +419,30 @@ function printTable(ary)
tableString = tableString .. "]";
return tableString;
end
--Returns true if the string passed to it is a pandigital
function isPandigitalFull(str, bottom, top)
--Return false if top < bottom
if(top < bottom) then
return false;
end
--Return false if the wrong number of characters are in the string
if(#str ~= (top - bottom + 1)) then
return false;
end
--Make sure that all of the needed characters are in the string exactly one time
for cnt = tonumber(bottom), tonumber(top) do
local _, count = string.gsub(str, cnt, cnt);
if(count ~= 1) then
return false;
end
end
--If the function has reached this part it has passed all of the falsifying tests
return true;
end
function isPandigital(str)
return isPandigitalFull(str, '1', '9');
end