-1

I am trying to translate a python function to c++ without success. Can someone help me?

The python function receives as input a string S and 2 integers (fragment_size and jump). The aim of this function is to slice the string S in a number of fragments of length equal to the first integer given by the input (fragment_size) and traverse the whole string S with a step equal to the second integer given by the input (jump).

import sys
# First we read the input and asign it to 3 different variables
S = sys.stdin.readline().strip()
fragment_size = sys.stdin.readline().strip()
jump = sys.stdin.readline().strip()

def window(S, fragment_size, jump):
    word = S[:fragment_size]
    if len(word)< fragment_size:
        return []
    else:
        return [word] + window(S[jump:], fragment_size, jump)

# We check that S is not an empty string and that fragment_size and jump are bigger than 0. 
if len(S) > 0 and int(fragment_size) > 0 and int(jump) > 0:
    # We print the results 
    for i in window(S, int(fragment_size), int(jump)):
        print(i)

For example: Input

ACGGTAGACCT
3
1

Output

ACG
CGG
GGT
GTA
TAG
AGA
GAC
ACC
CCT

Example 2: Input

ACGGTAGACCT
3
3

Output

ACG
GTA
GAC

I know how to solve this in c++ returning a string in the window function. But I really need to return a list, like the one I am returning in the python program.

Right now, I have this C++ code (that does not compile):

# include <iostream>
# include <vector>
# include <string>
using namespace std;

vector<string> window_list(string word, vector<vector<string>>& outp_list){
    outp_list.push_back(word);
}

vector<string> window(string s, int len_suf, int jump){
    string word;
    vector<vector<string>> outp_list; 

    word = s.substr(0, len_suf);
    if(word.length() < len_suf){
        return vector<string> window_list();
    }
    else {
        window_list(word, outp_list);
        return window(s.substr(jump), len_suf, jump);
    } 
}

int main(){
    // We define the variables
    string s;
    int len_suf, jump;
    // We read the input and store it to 3 different variables
    cin >> s;
    cin >> len_suf;
    cin >> jump;
    // We print the result
    vector<string> ans = window(s, len_suf, jump);
    for(auto& x: ans){
        cout << x << endl;
    }

    return 0;
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Lous
  • 35
  • 6
  • `vector window_list()` is a function definition. Your probably want to simply `return vector();` – UnholySheep Oct 28 '21 at 10:36
  • In the `else` branch you call `window_list` correctly, with correct arguments, even if you ignore the returned vector. But in the other branch your call is invalid, with a type between `return` and the call, and you don't pass any arguments to the call. Are you really supposed to call the `window_list` function in that case, or return an empty vector? – Some programmer dude Oct 28 '21 at 10:38
  • @Some programmer dude , I am just trying to return an empty vector, like in the python program. But I have just started using C++ and I don't really know how to do it. – Lous Oct 28 '21 at 10:51
  • @ UnholySheep, Okay, I will try to do that! Thanks – Lous Oct 28 '21 at 10:52
  • This programs seems much too complicated for someone "just starting with C++". Perhaps you should take a couple of steps back, invest in [some good books](https://stackoverflow.com/a/388282/440558) and learn a little more of the basics first. – Some programmer dude Oct 28 '21 at 10:56
  • Also a note about "translating" a code: **Don't!** Direct translations between languages (programming, spoken or written) almost never turn out good. Instead reimplement the language-independent algorithm. – Some programmer dude Oct 28 '21 at 10:57
  • Does this answer your question? [How can I translate this python function to c++?](https://stackoverflow.com/questions/69736694/how-can-i-translate-this-python-function-to-c) – Jason Oct 29 '21 at 16:46

1 Answers1

0

You can simplify your program as shown in the below example:


#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> stringFragmenter(const std::string &inputString, int len, int stepSize)
{
    std::string::const_iterator currentBegin = inputString.begin();
    
    std::string::const_iterator end = inputString.end();
    std::string::const_iterator currentPosition = currentBegin;
    
    std::vector<std::string> output;
    std::string pushedString;
    while(currentPosition + len <= end)
    {
        currentBegin = currentPosition;
        std::string::const_iterator currentEnd = currentBegin + len;
        
       
        
        
        while(currentBegin != currentEnd)
        {
            pushedString+= *currentBegin;
            ++currentBegin;
        }
        output.push_back(pushedString);
        
        currentPosition = currentPosition + stepSize;
        
        //clear the pushedString for next iteration
        pushedString.clear();
        
    }
    return output;
}
int main()
{
    std::string inputString;
    std::cin >> inputString;
    
    int length;//this denotes the length of string that has to be displayed(stored) at each time
    std::cin >> length;
    
    int stepSize;
    std::cin >>stepSize;//this denotes the jump size
    
    
    
    std::vector<std::string> fragmentedString = stringFragmenter(inputString, length, stepSize);
    
    //print out the elements of the returned vector so that we can confirm if it contains the correct elements
    for(const std::string &elem: fragmentedString)
    {
        std::cout<<elem<<std::endl;
    }

    
    return 0;
}


The output of the above program matches with both of your input cases as can be seen here.

In the above program, to enter your input case 1 you should first enter the string ACGGTAGACCT and then the length 3 and then finally the stepSize(or jump size) 1 and the input/output would look like:

ACGGTAGACCT
3
1
ACG
CGG
GGT
GTA
TAG
AGA
GAC
ACC
CCT

which matches with your desired output. Similarly for your input case 2. Check out the output here.

Jason
  • 36,170
  • 5
  • 26
  • 60