1

The question ask to find a “hidden message” in the replication origin.

Input: A string Text (representing the replication origin of a genome).

Output: A hidden message in Text.

Translate to computational language,

Input: Strings Text and Pattern.

Output: Count(Text, Pattern).

For example,

Count(ACAACTATGCATACTATCGGGAACTATCCT, ACTAT) = 3. 

In theory, we should account for overlapping occurrences of Pattern in Text right? So one way to do it is to screen down from first element to the length of text-length of the pattern we are looking for?

Here's the pseudo code i come up with,

def PatternCount(Text, Pattern):
    count = 0
    for i = 0 to len(Text)-len(Pattern):
        if Text(i, len(Pattern)) = Pattern:
            count = count + 1
    return count

Any suggestion? I'm new to python. Thanks in advance.

ads27
  • 71
  • 3
  • 12

3 Answers3

1

This is what I came up with:

def pattern_count(text, pattern):
    count = 0
    for i in range(0, len(text) - len(pattern) + 1):
        if text[i : len(pattern) + i] == pattern:
            count += 1
    return count

We're using string slicing (text[i : len(pattern) + i]) to check if the sub-string matches the pattern.

Input: text = "abc123!@#654abcabc" and pattern = "abc" Output: 3

the_dude
  • 1,004
  • 1
  • 11
  • 21
0
import re
print len(re.findall("abc", "abc123!@#654abcabc"))
Stylize
  • 1,058
  • 5
  • 16
  • 32
0

I think a more "pythonic" solution would be to use list comprehensions.

def pattern_count(text, pattern):
    return len([x for x in range(len(text) - len(pattern)+1) if pattern in text[x:len(pattern)+x]])
Alexsh
  • 67
  • 7