mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added functions to create a string from a vector
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <cinttypes>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <cmath>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
@@ -619,6 +620,21 @@ std::string toBin(T num){
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,15 +56,18 @@ bool testFindNumOccurrence();
|
||||
bool testFactorial();
|
||||
bool testIsPalindrome();
|
||||
bool testToBin();
|
||||
bool testPrintVector();
|
||||
|
||||
|
||||
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, 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",
|
||||
"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
|
||||
for(int cnt = 0;cnt < functions.size();++cnt){
|
||||
@@ -580,73 +583,116 @@ bool testToBin(){
|
||||
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:
|
||||
Function getPrimes() passed the test
|
||||
The test took 14.100 microseconds
|
||||
The test took 9.400 microseconds
|
||||
|
||||
|
||||
Function getNumPrimes() passed the test
|
||||
The test took 15.100 microseconds
|
||||
The test took 6.400 microseconds
|
||||
|
||||
|
||||
Function isPrime() passed the test
|
||||
The test took 300.000 nanoseconds
|
||||
The test took 200.000 nanoseconds
|
||||
|
||||
|
||||
Function getFactors() passed the test
|
||||
The test took 10.200 microseconds
|
||||
The test took 5.400 microseconds
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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
|
||||
The test took 23.500 microseconds
|
||||
The test took 7.100 microseconds
|
||||
|
||||
|
||||
Function getPermutations() passed the test
|
||||
The test took 30.100 microseconds
|
||||
The test took 8.800 microseconds
|
||||
|
||||
|
||||
Function getFib() passed the test
|
||||
The test took 228.800 microseconds
|
||||
The test took 221.400 microseconds
|
||||
|
||||
|
||||
Function getAllFib() passed the test
|
||||
The test took 9.000 microseconds
|
||||
The test took 4.100 microseconds
|
||||
|
||||
|
||||
Function bubbleSort() passed the test
|
||||
The test took 995.215 milliseconds
|
||||
The test took 1.186 seconds
|
||||
|
||||
|
||||
Function quickSort() passed the test
|
||||
The test took 3.559 milliseconds
|
||||
The test took 3.580 milliseconds
|
||||
|
||||
|
||||
Function search() passed the test
|
||||
The test took 2.800 microseconds
|
||||
The test took 1.300 microseconds
|
||||
|
||||
|
||||
Function findMin() passed the test
|
||||
The test took 2.500 microseconds
|
||||
The test took 1.700 microseconds
|
||||
|
||||
|
||||
Function findMax() passed the test
|
||||
The test took 2.500 microseconds
|
||||
The test took 1.600 microseconds
|
||||
|
||||
|
||||
Function findNumOccurrence() passed the test
|
||||
The test took 1.100 microseconds
|
||||
The test took 1.400 microseconds
|
||||
|
||||
|
||||
Function factorial() passed the test
|
||||
@@ -654,9 +700,13 @@ The test took 200.000 nanoseconds
|
||||
|
||||
|
||||
Function isPalindrome() passed the test
|
||||
The test took 1.500 microseconds
|
||||
The test took 1.400 microseconds
|
||||
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user