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
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
--lua/luaClasses/testAlgorithms.lua
|
||||
--Mathew Ellison
|
||||
-- Created: 03-28-19
|
||||
--Modified: 06-30-21
|
||||
--Modified: 10-11-21
|
||||
--This script is used to test the Algorithms script
|
||||
--[[
|
||||
Copyright (C) 2021 Matthew Ellison
|
||||
@@ -497,6 +497,7 @@ local function testPrintTable()
|
||||
local answer = printTable(nums);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("printTable int failed the first test\n");
|
||||
failed = true;
|
||||
end
|
||||
--Test 2
|
||||
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
@@ -504,6 +505,7 @@ local function testPrintTable()
|
||||
answer = printTable(nums);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("printTable int failed the second test\n");
|
||||
failed = true;
|
||||
end
|
||||
--Test 3
|
||||
nums = {-3, -2, -1, 0, 1, 2, 3};
|
||||
@@ -511,6 +513,7 @@ local function testPrintTable()
|
||||
answer = printTable(nums);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("printTable int failed the third test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 4
|
||||
@@ -519,6 +522,7 @@ local function testPrintTable()
|
||||
answer = printTable(strings);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("printTable string failed the first test\n");
|
||||
failed = true;
|
||||
end
|
||||
--Test 5
|
||||
strings = {"abc", "def", "ghi"};
|
||||
@@ -526,6 +530,7 @@ local function testPrintTable()
|
||||
answer = printTable(strings);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("printTable string failed the second test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Print a message if all of the tests passed
|
||||
@@ -534,6 +539,70 @@ local function testPrintTable()
|
||||
end
|
||||
end
|
||||
|
||||
local function testIsPandigital()
|
||||
local failed = false; --Signals whether a test was failed
|
||||
|
||||
--Test 1
|
||||
local num = "123456789";
|
||||
local correctAnswer = true;
|
||||
local answer = isPandigital(num);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the first test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 2
|
||||
num = "123";
|
||||
correctAnswer = true;
|
||||
answer = isPandigitalFull(num, '1', '3');
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the second test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 3
|
||||
num = "123";
|
||||
correctAnswer = false;
|
||||
answer = isPandigital(num);
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the third test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 4
|
||||
num = "123";
|
||||
correctAnswer = false;
|
||||
answer = isPandigitalFull(num, '3', '1');
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the fourth test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 5
|
||||
num = "1";
|
||||
correctAnswer = true;
|
||||
answer = isPandigitalFull(num, '1', '1');
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the fifth test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
--Test 6
|
||||
num = "112";
|
||||
correctAnswer = false;
|
||||
answer = isPandigitalFull(num, '1', '3');
|
||||
if(answer ~= correctAnswer) then
|
||||
io.write("isPandigital failed the sixth test\n");
|
||||
failed = true;
|
||||
end
|
||||
|
||||
|
||||
--Print a message if all of the tests passed
|
||||
if(not failed) then
|
||||
io.write("isPandigital passed all tests\n");
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--Create the timer to time each test
|
||||
local timer = Stopwatch:create();
|
||||
@@ -633,3 +702,9 @@ timer:start();
|
||||
testPrintTable();
|
||||
timer:stop();
|
||||
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||
|
||||
--Test isPalindrome
|
||||
timer:start();
|
||||
testIsPandigital();
|
||||
timer:stop();
|
||||
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||
|
||||
Reference in New Issue
Block a user