recursion - Creating recursive function for nested loop in python -
i had posted question : non overlapping pattern matching gap constraint in python ; 2 months back. got 1 response. solution quite long, , each word in pattern, 1 nested loop formed. there way of forming following function recursively ?
i=0 while < len(pt_dic[pt_split[0]]): match=false ii = pt_dic[pt_split[0]][i] #print "ii=" + str(ii) # start loop @ next index after ii j = next(x[0] x in enumerate(pt_dic[pt_split[1]]) if x[1] > ii) while j < len(pt_dic[pt_split[1]]) , not match: jj = pt_dic[pt_split[1]][j] #print "jj=" + str(jj) if jj > ii , jj <= ii + 2: # start loop @ next index after ii k = next(x[0] x in enumerate(pt_dic[pt_split[2]]) if x[1] > jj) while k < len(pt_dic[pt_split[2]]) , not match: kk = pt_dic[pt_split[2]][k] #print "kk=" + str(kk) if kk > jj , kk <= jj + 2: # start loop @ next index after kk l = next(x[0] x in enumerate(pt_dic[pt_split[3]]) if x[1] > kk) while l < len(pt_dic[pt_split[2]]) , not match: ll = pt_dic[pt_split[3]][l] #print "ll=" + str(ll) if ll > kk , ll <= kk + 2: print "match: (" + str(ii) + "," + str(jj) + "," + str(kk) + "," + str(ll) + ")" # we've found match, skip indices within match. = next(x[0] x in enumerate(pt_dic[pt_split[0]]) if x[1] > ll) -= 1 match=true l += 1 k += 1 j += 1 += 1
edit : for don't context :
i want find total no. of non-overlapping matches of pattern appearing in sequence, gap constraint 2.
eg. a b c
pattern found using algorithm. have find total # of pattern appearing in sequence such a b b c d e b c …
, max gap constraint 2.
max. gap isn't seen across sequence, seen between 2 words belonging pattern substring in sequence. e.g. pat: b c
, seq: b d e c b b b c d e
.
in case, a b d e c ...
match max 2 gaps allowed between a,b , b, c. next find a b b c
match. interestingly. there 2 matches, (2 chars b/w a, b , 2 chars b/w b,c) . however, count one, it's overlapping match. a b x x x c
isn't valid.
i have read original question briefly. i'm not sure if i've got gap counting part right. think have l sorted sequences of unique indices , code searches lists l elements, nth element nth sequence , 2 adjacent items satisfy condition prev < next < prev + gap + 1
anyway question nested loops.
the basic idea of code below pass list of sequences recursive function. function takes first sequence , iterates on it. remaining sequences passed other instances of same function each instance same, i.e. iterates on first sequence , passes rest until no sequences iterate on left.
during process partial solution being built step step. recursion continues if partial solution satisfies condition. when sequences exhausted, partial solution becomes final solution.
list_of_seqs= [ [0, 1, 7, 11, 22, 29], [2, 3, 8, 14, 25, 33], [4, 9, 15, 16, 27, 34], ] def found_match(m): print(m) gap = 2 def recloop(part, ls): if not ls: found_match(part) return seq, *ls = ls # python3 syntax last = part[-1] if part else none # not optimized: in seq: if last none or last < <= last + gap + 1: recloop(part + [i], ls) recloop([], list_of_seqs)
for python2 replace marked line seq, ls = ls[0], ls[1:]
Comments
Post a Comment