mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
Added functions to check for palindromes and convert nums to bin strings
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
--luaClasses/Algorithms.lua
|
--luaClasses/Algorithms.lua
|
||||||
--Matthew Ellison
|
--Matthew Ellison
|
||||||
-- Created: 02-04-19
|
-- 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
|
--This is a file of algorithms that I have found it useful to keep around at all times
|
||||||
--[[
|
--[[
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -361,3 +361,33 @@ function factorial(num)
|
|||||||
end
|
end
|
||||||
return fact;
|
return fact;
|
||||||
end
|
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
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
--lua/luaClasses/testAlgorithms.lua
|
--lua/luaClasses/testAlgorithms.lua
|
||||||
--Mathew Ellison
|
--Mathew Ellison
|
||||||
-- Created: 03-28-19
|
-- Created: 03-28-19
|
||||||
--Modified: 06-01-21
|
--Modified: 06-29-21
|
||||||
--This script is used to test the Algorithms script
|
--This script is used to test the Algorithms script
|
||||||
--[[
|
--[[
|
||||||
Copyright (C) 2021 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
@@ -349,6 +349,7 @@ local function testFactorial()
|
|||||||
local answer = factorial(num);
|
local answer = factorial(num);
|
||||||
if(answer ~= correctAnswer) then
|
if(answer ~= correctAnswer) then
|
||||||
io.write("factorial failed the first test\n");
|
io.write("factorial failed the first test\n");
|
||||||
|
failed = true;
|
||||||
end
|
end
|
||||||
--Test 2
|
--Test 2
|
||||||
local num = 10;
|
local num = 10;
|
||||||
@@ -356,6 +357,7 @@ local function testFactorial()
|
|||||||
local answer = factorial(num);
|
local answer = factorial(num);
|
||||||
if(answer ~= correctAnswer) then
|
if(answer ~= correctAnswer) then
|
||||||
io.write("factorial failed the second test\n");
|
io.write("factorial failed the second test\n");
|
||||||
|
failed = true;
|
||||||
end
|
end
|
||||||
--Test 3
|
--Test 3
|
||||||
local num = -5;
|
local num = -5;
|
||||||
@@ -363,6 +365,7 @@ local function testFactorial()
|
|||||||
local answer = factorial(num);
|
local answer = factorial(num);
|
||||||
if(answer ~= correctAnswer) then
|
if(answer ~= correctAnswer) then
|
||||||
io.write("factorial failed the third test\n");
|
io.write("factorial failed the third test\n");
|
||||||
|
failed = true;
|
||||||
end
|
end
|
||||||
|
|
||||||
--Print a message if all of the tests passed
|
--Print a message if all of the tests passed
|
||||||
@@ -371,6 +374,74 @@ local function testFactorial()
|
|||||||
end
|
end
|
||||||
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
|
--Create the timer to time each test
|
||||||
local timer = Stopwatch:create();
|
local timer = Stopwatch:create();
|
||||||
@@ -446,3 +517,15 @@ timer:start();
|
|||||||
testFactorial();
|
testFactorial();
|
||||||
timer:stop();
|
timer:stop();
|
||||||
io.write("It took " .. timer:getString() .. " to run this test\n");
|
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");
|
||||||
|
|||||||
Reference in New Issue
Block a user