How to find subsequences of a given vector, excluding given positions?

Asked

Viewed 33 times

0

Given the vector v:

[7 , 8 , 30 , -2 , 9]  

And the vector rem, which contains, in ascending order, the positions removed:

[1 , 4]

It is necessary to do a function seq(rem,n) (where n is the number of vector elements) that will return the beginning and end of each of the subsequences that does not contain the removed positions:

[(0,0) , (2,3)]

I tried to start with some special cases:

if(len(r) == 0):
    return [(0,n-1)]
elif(len(r) == 1):
    if(r[0] == 0):
      return [(1,n-1)]
    elif(r[0] == n -1):
      return [(0, n-2)]
    else:
      return [(0, r[0]-1), (r[0]+1, n-1)]

Then the general cases:

sequencias = []
if(r[0] == 0):
    if(r[1] != 1):
      sequencias.append((1, r[1]-1))
    elif(r[1] == 1 and len(r) ==2):
      sequencias.append((r[1]+1 , n-1))
      return sequencias
else:
    if(r[1] - r[0] > 1):
      sequencias.append((r[0]+1 , r[1]-1))

  
for i in range(1,len(r)-1):
    if(r[i+1] - r[i]  > 1):
      sequencias.append((r[i]+1 , r[i+1]-1))


  return sequencias
  • 1

    Hello Matheus, would it be nice for you to show us what you tried

  • @Lucas put some things in the code, but I’m afraid it’s incomplete.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.