[Solved] Is there any way I can generate the following series? [closed]


Here is one possible solution for your sequence generator.

#include <iostream>
#include <string>

// generate inverted term s -> !s
std::string invert(const std::string& s) {
    const char a="1";
    const char b = '2';
    std::string out;
    for (int i = 0; i < s.size(); i++) {
        // here one can check for invalid input as well, eg 0
        out += s[i] == a ? b : a;
    }
    return out;
}

// sk = s(k - 1)(!s(k - 1))(!s(k - 1))s(k - 1)
std::string nextSeq(const std::string& s) {
    std::string s_inv = invert(s);
    return s + s_inv + s_inv + s;
}

// generate nth term of the sequence.
std::string nthSeq(const std::string& s, const int n) {
    std::string out = s;
    for (int i = 1; i < n; i++) {
        out = nextSeq(out);
    }
    return out;
}

int main() {
    std::string s1 = "1221";
    // this will 2nd term 1221211221121221
    std::cout << nthSeq(s1, 2) << std::endl;
    return 0;
}

0

solved Is there any way I can generate the following series? [closed]