From 721128a276a334df8edc58695a3fb9e5d5f38fd1 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 29 Jun 2021 10:17:41 -0400 Subject: [PATCH] Added functions to check palindromes and convert to bin --- Algorithms.hpp | 33 +++++++++++++ testAlgorithms.cpp | 112 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 127 insertions(+), 18 deletions(-) diff --git a/Algorithms.hpp b/Algorithms.hpp index 05bccc2..2c91c6f 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -30,6 +30,7 @@ #include #include #include +#include namespace mee{ @@ -93,6 +94,11 @@ template T findMax(const std::vector& ary); //This function returns the number of times the character occurs in the string int findNumOccurrence(std::string str, char ch); +//This function returns true if the string passed in is a palindrome +bool isPalindrome(std::string str); +//This function converts a number to its binary equivalent +template +std::string toBin(T num); template @@ -586,6 +592,33 @@ T factorial(T num){ return fact; } +//This function returns true if the string passed in is a palindrome +bool isPalindrome(std::string str){ + std::string rev = str; + std::reverse(rev.begin(), rev.end()); + if(str == rev){ + return true; + } + else{ + return false; + } +} + +//This function converts a number to its binary equivalent +template +std::string toBin(T num){ + //Convert the number to a binary string + std::string fullString = std::bitset(num).to_string(); + //Remove leading zeros + int loc = 0; + for(loc = 0;(loc < fullString.size()) && (fullString[loc] == '0');++loc); + std::string trimmedString = fullString.substr(loc); + if(trimmedString == ""){ + trimmedString = "0"; + } + return trimmedString; +} + } diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index 75479f1..d84a65f 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -54,15 +54,17 @@ bool testFindMin(); bool testFindMax(); bool testFindNumOccurrence(); bool testFactorial(); +bool testIsPalindrome(); +bool testToBin(); int main(){ mee::Stopwatch timer; bool passedTest = false; std::vector functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations, - testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial}; + testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin}; std::vector names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations", - "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial"}; + "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin"}; //Start doing tests and print out the results of each for(int cnt = 0;cnt < functions.size();++cnt){ @@ -512,67 +514,141 @@ bool testFactorial(){ return true; } +bool testIsPalindrome(){ + //Test 1 + std::string str = "101"; + bool correctAnswer = true; + bool answer = mee::isPalindrome(str); + if(correctAnswer != answer){ + return false; + } + + //Test 2 + str = "100"; + correctAnswer = false; + answer = mee::isPalindrome(str); + if(correctAnswer != answer){ + return false; + } + + //Test 3 + str = ""; + correctAnswer = true; + answer = mee::isPalindrome(str); + if(correctAnswer != answer){ + return false; + } + + //If it hasn't failed a test then return true for passing all the tests + return true; +} + +bool testToBin(){ + //Test 1 + int num = 7; + std::string correctAnswer = "111"; + std::string answer = mee::toBin(num); + if(correctAnswer != answer){ + return false; + } + + //Test 2 + uint64_t num2 = 8; + correctAnswer = "1000"; + answer = mee::toBin(num2); + if(correctAnswer != answer){ + return false; + } + + //Test 3 + num = 0; + correctAnswer = "0"; + answer = mee::toBin(num); + if(correctAnswer != answer){ + return false; + } + + //If it hasn't failed a test then return true for passing all the tests + return true; +} + /* Results: Function getPrimes() passed the test -The test took 0.000 nanoseconds +The test took 14.100 microseconds Function getNumPrimes() passed the test -The test took 0.000 nanoseconds +The test took 15.100 microseconds + + +Function isPrime() passed the test +The test took 300.000 nanoseconds Function getFactors() passed the test -The test took 0.000 nanoseconds +The test took 10.200 microseconds Function getDivisors() passed the test -The test took 0.000 nanoseconds +The test took 5.800 microseconds Function getSum() passed the test -The test took 0.000 nanoseconds +The test took 9.300 microseconds Function getProduct() passed the test -The test took 0.000 nanoseconds +The test took 2.000 microseconds Function isFound() passed the test -The test took 0.000 nanoseconds +The test took 23.500 microseconds Function getPermutations() passed the test -The test took 0.000 nanoseconds +The test took 30.100 microseconds Function getFib() passed the test -The test took 0.000 nanoseconds +The test took 228.800 microseconds Function getAllFib() passed the test -The test took 0.000 nanoseconds +The test took 9.000 microseconds Function bubbleSort() passed the test -The test took 1.031 seconds +The test took 995.215 milliseconds Function quickSort() passed the test -The test took 4.000 milliseconds +The test took 3.559 milliseconds Function search() passed the test -The test took 0.000 nanoseconds +The test took 2.800 microseconds Function findMin() passed the test -The test took 0.000 nanoseconds +The test took 2.500 microseconds Function findMax() passed the test -The test took 0.000 nanoseconds +The test took 2.500 microseconds Function findNumOccurrence() passed the test -The test took 0.000 nanoseconds +The test took 1.100 microseconds + + +Function factorial() passed the test +The test took 200.000 nanoseconds + + +Function isPalindrome() passed the test +The test took 1.500 microseconds + + +Function toBin() passed the test +The test took 2.100 microseconds */