mercredi 27 octobre 2021

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

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!

Aucun commentaire:

Enregistrer un commentaire