How can I translate this python function to c++?

2024/10/9 6:24:34

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:


# 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 variablesstring s;int len_suf, jump;// We read the input and store it to 3 different variablescin >> s;cin >> len_suf;cin >> jump;// We print the resultvector<string> ans = window(s, len_suf, jump);for(auto& x: ans){cout << x << endl;}return 0;
}

Thanks!

Answer

It does the job, but as I'm not very into C++ Alexandros Palacios approach might be better anyways. This is just a blunt translation of your Python code.

#include <strings.h>
#include <iostream>
#include <vector>std::string window(std::string S, int fragment_size, int jump) {for (int i = 0; i <= S.length(); jump) {std::string word = S.substr(i, i+fragment_size);if (word.length() < fragment_size) {return "";}else {return word + " " + window(S.substr(jump, S.length()-1), fragment_size, jump);}}
}int main(int argc, char const *argv[])
{std::string S;std::cout << "Enter DNA sequence: ";std::cin >> S;int fragment_size;std::cout << "Enter fragment size: ";std::cin >> fragment_size;int jump;std::cout << "Enter jump size: ";std::cin >> jump;std::vector<std::string> data;std::string result;if (S.length() > 0 && fragment_size > 0 && jump > 0) {result = window(S, fragment_size, jump);} else {return 1;}size_t pos;std::string delimiter = " ";while ((pos = result.find(delimiter)) != std::string::npos) {data.push_back(result.substr(0, pos));result.erase(0, pos + delimiter.length());}for (const auto &str : data) {std::cout << str << " ";}std::cout << std::endl;return 0;
}
https://en.xdnf.cn/q/118619.html

Related Q&A

Reverse PDF imposition

I have an imposed document: there are 4 n A4 pages on the n sheets. I put them into a roller image scanner and receive one 2 n paged PDF document (A3).If, say, n = 3, then Ive got the following seque…

Python: How to run flask mysqldb on Windows machine?

Ive installed the flask-mysqldb module with pip package management system on my Windows machine and I dont know how to run it.I have tried to add the path to the MySQLdb in System properties and still …

Match a pattern and save to variable using python

I have an output file containing thousands of lines of information. Every so often I find in the output file information of the following formInput Orientation: ... content ... Distance matrix (angstro…

Sharing a Queue instance between different modules

I am new to Python and I would like to create what is a global static variable, my thread-safe and process-safe queue, between threads/processes created in different modules. I read from the doc that t…

Square a number with functions in python [duplicate]

This question already has answers here:What does it mean when the parentheses are omitted from a function or method call?(6 answers)Closed last year.This is an extremely easy question for Python. Its…

Changing the cell name

I have a file that contains the following:NameABCD0145ABCD1445ABCD0998And Im trying to write a cod that read every row and change the name to the following format:NameABCD_145ABCD_1445ABCD_998keeping i…

Procfile Heroku

I tried to deploy my first Telegram chatbot (done with Chatterbot library) on Heroku. The files of my chatbot are: requirements (txt file) Procfile (worker: python magghybot.py) botusers (csv file) Mag…

How do i loop a code until a certain number is created?

This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is:Determining the difference between the strength attributes. The differ…

Finding the longest list in given list that contains only positive numbers in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed l…

How to create multiple VideoCapture Objects

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.for example: I have path for three videos that I wanted to be read using Video Captur…