mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2026-02-04 03:12:27 -05:00
Added some comments and a function for permutations
This commit is contained in:
@@ -10,20 +10,29 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace mee{
|
namespace mee{
|
||||||
|
|
||||||
|
|
||||||
//A list of functions in the file
|
//A list of functions in the file
|
||||||
//Also works as a declaration
|
//Also works as a declaration
|
||||||
|
//This is a function that returns all the primes <= goalNumber and returns a vector with those prime numbers
|
||||||
template<class T>
|
template<class T>
|
||||||
std::vector<T> getPrimes(T goalNumber);
|
std::vector<T> getPrimes(T goalNumber);
|
||||||
|
//This is a function that gets all the divisors of num and returns a vector containing the divisors
|
||||||
template<class T>
|
template<class T>
|
||||||
std::vector<T> getDivisors(T num);
|
std::vector<T> getDivisors(T num);
|
||||||
|
//This is a function that gets the sum of all elements in a vector and returns the number
|
||||||
template <class T>
|
template <class T>
|
||||||
T getSum(std::vector<T> numbers);
|
T getSum(std::vector<T> numbers);
|
||||||
|
//This is a function that searches a vecter for an element. Returns true if num is found in list
|
||||||
template<class T>
|
template<class T>
|
||||||
bool isFound(T num, std::vector<T> list);
|
bool isFound(T num, std::vector<T> list);
|
||||||
|
//This is a function that creates all permutations of a string and returns a vector of those permutations.
|
||||||
|
//It is meant to have only the string passed into it from the calling function. num is used for recursion purposes
|
||||||
|
//It can however be used with num if you want the first num characters to be stationary
|
||||||
|
std::vector<std::string> getPermutations(std::string master, int num = 0);
|
||||||
|
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
@@ -103,6 +112,37 @@ bool isFound(T num, std::vector<T> list){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> getPermutations(std::string master, int num){
|
||||||
|
std::vector<std::string> perms;
|
||||||
|
//Check if the number is out of bounds
|
||||||
|
if((num >= master.size()) || (num < 0)){
|
||||||
|
return perms;
|
||||||
|
}
|
||||||
|
//If this is the last possible recurse just return the current string
|
||||||
|
else if(num == (master.size() - 1)){
|
||||||
|
perms.push_back(master);
|
||||||
|
return perms;
|
||||||
|
}
|
||||||
|
//If there are more possible recurses, recurse with the current permutation
|
||||||
|
std::vector<std::string> temp;
|
||||||
|
temp = getPermutations(master, num + 1);
|
||||||
|
perms.insert(perms.end(), temp.begin(), temp.end());
|
||||||
|
//You need to swap the current letter with every possible letter after it
|
||||||
|
//The ones needed to swap before will happen automatically when the function recurses
|
||||||
|
for(int cnt = 1;(num + cnt) < master.size();++cnt){
|
||||||
|
std::swap(master[num], master[num + cnt]);
|
||||||
|
temp = getPermutations(master, num + 1);
|
||||||
|
perms.insert(perms.end(), temp.begin(), temp.end());
|
||||||
|
std::swap(master[num], master[num + cnt]);
|
||||||
|
}
|
||||||
|
|
||||||
|
//The array is not necessarily in alpha-numeric order. So if this is the full array sort it before returning
|
||||||
|
if(num == 0){
|
||||||
|
std::sort(perms.begin(), perms.end());
|
||||||
|
}
|
||||||
|
return perms;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user