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 @@
--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");