-1

I have set of patterns

ATGCG GCATG CATGC AGGCA GGCAT

which I need to find the overlap between them. I'm using this function and it works correctly:

def get_overlap(patterns):
    n = len(patterns[0])-1
    return [(left,right) for left,right in product(patterns,patterns) if left != right and left.endswith(right[:n])]

My question is: why it doesn't loop through all the patterns when I am using the ordinary for loop form?

for left, right in product(patterns, patterns):
    if left != right and left.endswith(right[:n]):
        return [left, right]

it prints the last item only

EdChum
  • 376,765
  • 198
  • 813
  • 562
marjamil
  • 3
  • 1

1 Answers1

3

The first solution basically says "return all (left, right) pairs that match the following criteria"

The second solution says "search for a pair, that matches the following criteria. As soon as one is found, return it". Therefore only one is returned. I haven't tested this, but I it should be the first one encountered, not the last one.

To fix it: instead of returning a pair which is found in the second solution, add it to an array of solutions, and return that array after the for loop.

Misch
  • 10,350
  • 4
  • 35
  • 49