0

My code for the function is really messy and I cannot find why it returns a list of 1's. A solution would obviously be great, but with advice to make the code just better, i'd be happy

def cont_cons_repeats(ADN, STR, pos):
slong = 0
# Find start of sequence
for i in range(len(ADN[pos:])):
    if ADN[pos + i:i + len(STR)] == STR:
        slong = 1
        pos = i + pos
        break

if slong == 0:
    return 0

# First run
for i in range(len(ADN[pos:])):
    i += len(STR) - 1
    if ADN[pos + i + 1:pos + i + len(STR)] == STR:
        slong += 1
    else:
        pos = i + pos
        break
    
# Every other run
while True:
    pslong = cont_cons_repets(ADN, STR, pos)
    if pslong > slong:
        slong = pslong
    if pslong == 0:
        break

return slong

(slong stands for size of longest sequence, pslong for potential slong, and pos for position)

5
  • 2
    This would be easier to answer if you provided sample input data and your expected output. Commented Feb 18, 2021 at 6:16
  • Do you have some sample data that others can run through your function. Have you tried regular expressions? regex101.com ? Commented Feb 18, 2021 at 6:16
  • What is the point of passing in pos? Do you only want to find the longest run of consecutive repeats of STR in ADN at or after pos? Commented Feb 18, 2021 at 6:19
  • @Grismar I thought it would be necessary for the function to be recursive Commented Feb 18, 2021 at 16:05
  • @Nick ADN is a big chunck of DNA (a .txt with one long line), the STRs are sequences of 4 to 8 nucleotides (i. e. AGATC, AATG, etc). The expected output would be in one case ['2', '8', '3'] (this is all part of a problem from cs50, should've said that in the post) Commented Feb 18, 2021 at 16:10

1 Answer 1

1

Assuming you pass in pos because you want to ignore the start of the string you're searching up to pos:

def longest_run(text, part, pos):
    m = 0
    n = 0
    while pos < len(text):
        if text[pos:pos+len(part)] == part:
            n += 1
            pos += len(part)
        else:
            m = max(n, m)
            n = 0
            pos += 1
    return m

You say your function returns a list of 1s, but that doesn't seem to match what your code is doing. Your provided code has some syntax errors, including a misspelled call to your function cont_cons_repets, so it's impossible to say why you're getting that result.

You mentioned in the comments that you thought a recursive solution was required. You could definitely make it work as a recursive function, but in many cases where a recursive function works, you should consider a non-recursive function to save on resources. Recursive functions can be very elegant and easy to read, but remember that any recursive function can also be written as a non-recursive function. It's never required, often more resource-intensive, but sometimes just a very clean and easy to maintain solution.

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the name of the function for the post (comments and names were in spanish), and mispelled it the second time haha. Your code worked, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.