Added functions to check for palindromes and convert nums to bin strings

This commit is contained in:
2021-06-29 14:14:35 -04:00
parent a9de121a88
commit 75f4441f18
2 changed files with 115 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
--luaClasses/Algorithms.lua
--Matthew Ellison
-- Created: 02-04-19
--Modified: 06-01-21
--Modified: 06-29-21
--This is a file of algorithms that I have found it useful to keep around at all times
--[[
Copyright (C) 2021 Matthew Ellison
@@ -361,3 +361,33 @@ function factorial(num)
end
return fact;
end
--Returns true if the string passed in is a palindrome
function isPalindrome(str)
local rev = string.reverse(str);
if(str == rev) then
return true;
else
return false;
end
end
--Converts a number to its binary equivalent
function toBin(num)
--Convert the number to a binary string
local binNum = "";
while(num > 0) do
local rest = math.fmod(num, 2);
if(rest == 1) then
binNum = binNum .. "1";
else
binNum = binNum .. "0";
end
num = (num - rest) / 2;
end
binNum = string.reverse(binNum);
if(binNum == "") then
binNum = "0";
end
return binNum;
end

View File

@@ -1,7 +1,7 @@
--lua/luaClasses/testAlgorithms.lua
--Mathew Ellison
-- Created: 03-28-19
--Modified: 06-01-21
--Modified: 06-29-21
--This script is used to test the Algorithms script
--[[
Copyright (C) 2021 Matthew Ellison
@@ -349,6 +349,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the first test\n");
failed = true;
end
--Test 2
local num = 10;
@@ -356,6 +357,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the second test\n");
failed = true;
end
--Test 3
local num = -5;
@@ -363,6 +365,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
@@ -371,6 +374,74 @@ local function testFactorial()
end
end
local function testIsPalindrome()
local failed = false; --Signals whether a test was failed
--Test 1
local str = "101";
local correctAnswer = true;
local answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the first test\n");
failed = true;
end
--Test 2
str = "100";
correctAnswer = false;
answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the second test\n");
failed = true;
end
--Test 3
str = "";
correctAnswer = true;
answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("isPalindrome passed all tests\n");
end
end
local function testToBin()
local failed = false; --Signals whether a test was failed
--Test 1
local num = 7;
local correctAnswer = "111";
local answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the first test\n");
failed = true;
end
--Test 2
num = 0;
correctAnswer = "0";
answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the second test\n");
failed = true;
end
--Test 3
num = 1000000;
correctAnswer = "11110100001001000000";
answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("toBin passed all tests\n");
end
end
--Create the timer to time each test
local timer = Stopwatch:create();
@@ -446,3 +517,15 @@ timer:start();
testFactorial();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test isPalindrome
timer:start();
testIsPalindrome();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test toBin
timer:start();
testToBin();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");