mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added functions to check palindromes and convert to bin
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <bitset>
|
||||
|
||||
namespace mee{
|
||||
|
||||
@@ -93,6 +94,11 @@ template <class T>
|
||||
T findMax(const std::vector<T>& 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 <class T>
|
||||
std::string toBin(T num);
|
||||
|
||||
|
||||
template <class T>
|
||||
@@ -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 <class T>
|
||||
std::string toBin(T num){
|
||||
//Convert the number to a binary string
|
||||
std::string fullString = std::bitset<sizeof(T)>(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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<boolFn> 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<std::string> 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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user