Added functions to create a string from a vector

This commit is contained in:
2021-06-30 14:02:57 -04:00
parent 16b2a6f792
commit cf8fd4d65e
2 changed files with 92 additions and 26 deletions

View File

@@ -27,6 +27,7 @@
#include <cinttypes> #include <cinttypes>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <sstream>
#include <cmath> #include <cmath>
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
@@ -619,6 +620,21 @@ std::string toBin(T num){
return trimmedString; return trimmedString;
} }
//Print a vector
template <class T>
std::string printVector(std::vector<T>& ary){
std::stringstream str;
str << "[";
for(int cnt = 0;cnt < ary.size();++cnt){
str << ary[cnt];
if(cnt < ary.size() - 1){
str << ", ";
}
}
str << "]";
return str.str();
}
} }

View File

@@ -56,15 +56,18 @@ bool testFindNumOccurrence();
bool testFactorial(); bool testFactorial();
bool testIsPalindrome(); bool testIsPalindrome();
bool testToBin(); bool testToBin();
bool testPrintVector();
int main(){ int main(){
mee::Stopwatch timer; mee::Stopwatch timer;
bool passedTest = false; bool passedTest = false;
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations, std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin}; testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin,
testPrintVector};
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations", std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin"}; "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin",
"testPrintVector"};
//Start doing tests and print out the results of each //Start doing tests and print out the results of each
for(int cnt = 0;cnt < functions.size();++cnt){ for(int cnt = 0;cnt < functions.size();++cnt){
@@ -580,73 +583,116 @@ bool testToBin(){
return true; return true;
} }
bool testPrintVector(){
//Test 1
std::vector<int> nums;
std::string correctAnswer = "[]";
std::string answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 2
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 3
nums = {-3, -2, -1, 0, 1, 2, 3};
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 4
std::vector<std::string> strings = {"A", "B", "C"};
correctAnswer = "[A, B, C]";
answer = mee::printVector(strings);
if(correctAnswer != answer){
return false;
}
//Test 5
strings = {"abc", "def", "ghi"};
correctAnswer = "[abc, def, ghi]";
answer = mee::printVector(strings);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
/* Results: /* Results:
Function getPrimes() passed the test Function getPrimes() passed the test
The test took 14.100 microseconds The test took 9.400 microseconds
Function getNumPrimes() passed the test Function getNumPrimes() passed the test
The test took 15.100 microseconds The test took 6.400 microseconds
Function isPrime() passed the test Function isPrime() passed the test
The test took 300.000 nanoseconds The test took 200.000 nanoseconds
Function getFactors() passed the test Function getFactors() passed the test
The test took 10.200 microseconds The test took 5.400 microseconds
Function getDivisors() passed the test Function getDivisors() passed the test
The test took 5.800 microseconds
Function getSum() passed the test
The test took 9.300 microseconds
Function getProduct() passed the test
The test took 2.000 microseconds The test took 2.000 microseconds
Function getSum() passed the test
The test took 8.500 microseconds
Function getProduct() passed the test
The test took 1.200 microseconds
Function isFound() passed the test Function isFound() passed the test
The test took 23.500 microseconds The test took 7.100 microseconds
Function getPermutations() passed the test Function getPermutations() passed the test
The test took 30.100 microseconds The test took 8.800 microseconds
Function getFib() passed the test Function getFib() passed the test
The test took 228.800 microseconds The test took 221.400 microseconds
Function getAllFib() passed the test Function getAllFib() passed the test
The test took 9.000 microseconds The test took 4.100 microseconds
Function bubbleSort() passed the test Function bubbleSort() passed the test
The test took 995.215 milliseconds The test took 1.186 seconds
Function quickSort() passed the test Function quickSort() passed the test
The test took 3.559 milliseconds The test took 3.580 milliseconds
Function search() passed the test Function search() passed the test
The test took 2.800 microseconds The test took 1.300 microseconds
Function findMin() passed the test Function findMin() passed the test
The test took 2.500 microseconds The test took 1.700 microseconds
Function findMax() passed the test Function findMax() passed the test
The test took 2.500 microseconds The test took 1.600 microseconds
Function findNumOccurrence() passed the test Function findNumOccurrence() passed the test
The test took 1.100 microseconds The test took 1.400 microseconds
Function factorial() passed the test Function factorial() passed the test
@@ -654,9 +700,13 @@ The test took 200.000 nanoseconds
Function isPalindrome() passed the test Function isPalindrome() passed the test
The test took 1.500 microseconds The test took 1.400 microseconds
Function toBin() passed the test Function toBin() passed the test
The test took 2.100 microseconds The test took 9.300 microseconds
Function testPrintVector() passed the test
The test took 9.900 microseconds
*/ */