Home > Software engineering >  How can I translate this python function to c ?
How can I translate this python function to c ?

Time:10-27

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):
    for i in range(0, len(S), jump):
        word = S[i:i 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

Thanks!

CodePudding user response:

Using the string data type you can achieve it without so much problems. Instead of the python [:] operator, you can use the substr method:

#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.

CodePudding user response:

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;
}
  • Related