mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Added isPandigital function
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user