➜ ~

Playing Hacks and Stuffs!


Project maintained by h4ckyou Hosted on GitHub Pages — Theme by mattgraham

Repetition

image

Description:

You are given a DNA sequence: a string consisting of characters A, C, G, and T.
Your task is to find the longest repetition in the sequence.
This is a maximum-length substring containing only one type of character.

So what this is about is:

For example if they give:

The answer should be 4 because letter D is repeated 4 times

To solve this I’ll do this:

Here’s my solve script

def findLongestRepetition(sequence):
    maxRepetition = 0 
    currentRepetition = 1
    previousChar = sequence[0]

    for char in sequence[1:]:
        if char == previousChar:
            currentRepetition += 1
        else:
            maxRepetition = max(maxRepetition, currentRepetition)
            currentRepetition = 1
            previousChar = char

    return max(maxRepetition, currentRepetition)

sequence = list(input())
r = findLongestRepetition(sequence)

print(r)

It works

image