-1

I have a piece of python code I need to use in c++. The algorithm is a recursion that uses yield.

Here is the python function:

def getSubSequences(self, s, minLength=1):
    if len(s) >= minLength:
        for i in range(minLength, len(s) + 1):
            for p in self.getSubSequences(s[i:], 1 if i > 1 else 2):
                yield [s[:i]] + p
    elif not s:
        yield []

and here is my attempt so far

vector< vector<string> > getSubSequences(string number, unsigned int minLength=1) {
    if (number.length() >= minLength) {
        for (unsigned int i=minLength; i<=number.length()+1; i++) {
            string sub = "";
            if (i <= number.length())
                sub = number.substr(i);
            vector< vector<string> > res = getSubSequences(sub, (i > 1 ? 1 : 2));
            vector< vector<string> > container;
            vector<string> tmp;
            tmp.push_back(number.substr(0, i));
            container.push_back(tmp);
            for (unsigned int j=0; j<res.size(); j++) {
                container.push_back(res.at(j));
                return container;
            }
        }
    } else if (number.length() == 0)
        return vector< vector<string> >();
}

Unfortunately I get a segmentation fault when executing it. Is this even the right attempt or is there an easier way to do this? The data structures are not fixed I just need the same result as I get in the python code!

Barry
  • 286,269
  • 29
  • 621
  • 977
wasp256
  • 5,943
  • 12
  • 72
  • 119
  • `if (i <= number.length()) sub = number.substr(i);` What happens here if `i == number.length()`? Also, this loop condition: `i<=number.length()+1` smells of a memory overread or overwrite. – PaulMcKenzie May 25 '15 at 18:17
  • Also, please provide the parameters you used for the function that reproduce the error. – PaulMcKenzie May 25 '15 at 18:20
  • The most natural way to implement it might be as an iterator returned from a function. Accessing the iterator advances the "generator", and the return value is checked to see if the loop should terminate. – Ignacio Vazquez-Abrams May 25 '15 at 18:24
  • The parameter was "1234", for both python n c++ – wasp256 May 25 '15 at 18:27
  • check the answer to this question: http://stackoverflow.com/questions/22358153/recursive-generator-in-c – João Abrantes May 25 '15 at 18:32
  • Have you tried stepping through it with a debugger? And seeing what line the problem occurs on? Then checking the state of variables before the problem occurs, and seeing if anything is wrong? – Yakk - Adam Nevraumont May 25 '15 at 18:58
  • You have a `return` statement inside of a `for` loop. That is clearly wrong, regardless of what is causing the segfault. – Barry May 25 '15 at 19:01
  • Doesn't the yield work as a return as well? – wasp256 May 25 '15 at 19:12

1 Answers1

1

The loops in your above code snippets are not equivalent.

The Python code has

for i in range(minLength, len(s) + 1):

The C++ code has

for (unsigned int i=minLength; i<=number.length()+1; i++) {

So the Python loop terminates one iteration sooner than the C++ one.


The question has really nothing to do with yield. I think you should print stuff out from implementations, in these cases, and study them. In this case, it would have shown that the two algorithms diverge.

Ami Tavory
  • 74,578
  • 11
  • 141
  • 185