Recursive Function

Mar 24, 2013 at 6:08am
I'm trying to write a recursive function that takes in a vector of strings that contains

"1 forward", "2 forward", "rotate left", "2 backwards" ... etc

how can I write recursive function that creates a vector of every unique combination of commands?
Mar 24, 2013 at 6:27am
Please define what you mean by "unique combination". Is "1 forward" is an unique combination? Or should it be several (How many?) commands in a row?
Mar 24, 2013 at 4:04pm
By unique combination I mean a vector of the same length. So, just "1 forward" is not a unique combination.
Mar 24, 2013 at 4:10pm
Mar 24, 2013 at 6:52pm
This is what I want but is there a way to do it recursively?
Mar 24, 2013 at 7:07pm
vector of every unique combination of commands

You mean it should be vector of vector of strings?
Mar 24, 2013 at 8:19pm
yes
Mar 24, 2013 at 8:36pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <algorithm>

std::vector<std::vector<std::string> > recursive_permutation(std::vector<std::string> data)
{
    if( std::next_permutation(data.begin(), data.end()) ) {
        std::vector<std::vector<std::string>> temp  = recursive_permutation(data);
        temp.push_back(data);
        return temp;
    } else {
        std::vector<std::vector<std::string>> result = {data};
        return result;
    }
}

std::vector<std::vector<std::string>> all_unique_combinations(std::vector<std::string> source)
{
    std::sort(source.begin(), source.end());
    return recursive_permutation(source);
}

Yay for excessive copy
Last edited on Mar 24, 2013 at 8:37pm
Mar 26, 2013 at 2:28am
Thank you
Topic archived. No new replies allowed.