[英]How can I translate this python function to c++?
I am trying to translate a python function to c++ without success.我正在尝试将 python 函数转换为 C++,但没有成功。 Can someone help me?
有人能帮我吗?
The python function receives as input a string S and 2 integers (fragment_size and jump). python 函数接收一个字符串 S 和 2 个整数(fragment_size 和 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).
此函数的目的是将字符串 S 切成长度等于输入给定的第一个整数 (fragment_size) 的多个片段,并以等于输入给定的第二个整数的步长遍历整个字符串 S(跳转)。
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例如:输入ACGGTAGACCT 3 1
Output ACG CGG GGT GTA TAG AGA GAC ACC CCT输出ACG CGG GGT GTA TAG AGA GAC ACC CCT
Example 2: Input ACGGTAGACCT 3 3示例 2:输入ACGGTAGACCT 3 3
Output ACG GTA GAC输出ACG GTA GAC
I know how to solve this in c++ returning a string in the window function.我知道如何在 C++ 中解决这个问题,在窗口函数中返回一个字符串。 But I really need to return a list, like the one I am returning in the python program.
但我真的需要返回一个列表,就像我在 python 程序中返回的列表一样。
Right now, I have this C++ code:现在,我有这个 C++ 代码:
# 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;
}
Thanks!谢谢!
Using the string
data type you can achieve it without so much problems.使用
string
数据类型,您可以毫无问题地实现它。 Instead of the python [:]
operator, you can use the substr
method:您可以使用
substr
方法代替 python [:]
运算符:
#include <iostream>
#include <string.h>
using namespace std;
string S = "";
int fragment_size = 0;
int jump = 0;
string window(string s) {
if (s.length() < fragment_size)
return "";
else
return s.substr(0, fragment_size) + " " + window(s.substr(jump));
}
int main(int argc, char *argv[]) {
cin >> S;
cin >> fragment_size;
cin >> jump;
if (S.length() && fragment_size > 0 && jump > 0) {
cout << endl
<< window(S);
}
return 0;
}
Also, in your window
function, you have an extra for loop that isn't needed since you return a value after the first iteration.此外,在您的
window
函数中,您有一个不需要的额外 for 循环,因为您在第一次迭代后返回一个值。
It does the job, but as I'm not very into C++ Alexandros Palacios approach might be better anyways.它可以完成这项工作,但由于我不是很喜欢 C++ Alexandros Palacios 方法可能会更好。 This is just a blunt translation of your Python code.
这只是对 Python 代码的粗略翻译。
#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;
}
You can use the program 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:在上面的程序中,要输入输入 case 1,您应该首先输入字符串
ACGGTAGACCT
,然后输入长度 3,最后输入 stepSize(或跳转大小)1,输入/输出将如下所示:
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 .
与您的输入案例 2 类似。在此处查看输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.