0

I have this following string:

array = "TQTDTQD"

For each D in array, is there anyway I could know how many consecutive T's or D's are before and after each D that is the same as the letter right before each D. So for the 1st D, the letter right before it is T, then I want to find how many consecutive T's are before and after 1st D. For the 2nd D, the letter right before it is Q, so i want to find # of consecutive Q's before and after 2nd D.

For instance, for the 1st D, there is 1 T before D and 1 T after showing up until there is a Q. (e.g: for the 1st D, its like "TDT")

for the 2nd D, there is one consecutive Q before or no Q after

I did start with the following Python codes to find the positions of each T,Q,D:

get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
T_position = get_indexes("T",array)
Q_position = get_indexes("Q",array)
D_position = get_indexes("D",array) 

and then I tried this to find out T's before each D.

for each in range(0,len(D_position)):
  RightBeforeD = array[D_position[each]-1]
  numberofT = []
  for eachofallbefore in reversed(range(0,D_position[each])):
    if array[eachofallbefore] ==RightBeforeD:
      numberofT.append(array[eachofallbefore])
      print (numberofT)

But the results for these two D's are like 1st D:["T","T"], 2nd D: ["Q","Q"], seems like it prints out all T or Q's before each D,

  1. I am just wondering if I could add something like a check point to stop my for loop once the consecutive T/Q's end.
  2. Also I just wonder if I could use the same method for finding consecutive T/Q's AFTER each D.

many thx!

Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
ssftw
  • 25
  • 1
  • 2
    Why did you tag this python 2.7 and python 3.x? Which are you using? Are you seeking a solution that's compatible in both? – Two-Bit Alchemist Apr 19 '16 at 20:37
  • Also, can you give more example input and expected output? I'm having a hard time following your specified description. For each letter in the string, you want to find the number of consecutive appearances of the previous letter before and after the letter you're looking at? – Two-Bit Alchemist Apr 19 '16 at 20:39
  • @Two-BitAlchemist sorry about my poor description! for each "D" in the string, i wan to find the # of consecutive appearances of the previous letter before and after. – ssftw Apr 19 '16 at 21:02
  • @Two-BitAlchemist for instance, for the array ="QDQQTDT", for the 1st D, the output would be 1 (Q before) and 2 (Qs after), for the 2nd D, the output would be 1 (T) before and 1(T) after. – ssftw Apr 19 '16 at 21:04
  • ^ I suggest taking a look here because this is a notoriously hard problem and if you can solve the "longest repeated subsequence" problem that's 95% of it. The last 5% would be localizing it to only look before and after the 'D' character. You could even run your solution over the return value of `re.split('D', your_input_string)` – Two-Bit Alchemist Apr 19 '16 at 22:11

1 Answers1

0

I think this works:

import re
def sequences(s):
    xdx = re.findall(r"['Q']+['D']['Q']*|['T']+['D']['T']*", s)

    for i, d_group in enumerate(xdx):
        before, after = d_group.split('D')
        print("For D{0} there were {1}({2})s before and {3}({2})s after".format(i+1, len(before), before[0], len(after)))


if __name__ == "__main__":
     sequences('QQQDQQTQDQQTDTDQQ')
MTset
  • 90
  • 5