CS50 DNA works for small.csv but not for large

后端 未结 2 1983
挽巷
挽巷 2021-01-27 00:46

I am having problems with CS50 pset6 DNA. It is getting all the right values and gives correct answers when I use the small.csv file but not when I use the large on

2条回答
  •  迷失自我
    2021-01-27 01:31

    Problem is in this while loop. Look at this code carefully.

    while i < len(samples):
        hold = samples[i:(i + size)]
        if hold == keys:
            run += 1
            #ensure the code does not go outside len of samples
            if ((i + size) < len(samples)):
                i = i + size
            continue
        if run > longest:
            longest = run
            run = 0
        i += 1
    

    You have a missing logic here. You are supposed to check the longest consecutive DNA sequence. So when you have a repetition of dna sequence back to back, you need to find how many times it is repeated. When it is no longer repeated, only then, you need to check if this is the longest sequence.

    Solution

    You need to add else statement after if hold==keys: statement. This would be the right fix;

    while i < len(samples):
        hold = samples[i:(i + size)]
        if hold == keys:
            run += 1
            #ensure the code does not go outside len of samples
            if ((i + size) < len(samples)):
                i = i + size
            continue
        else: #only if there is no longer sequence match, check this.
            if run > longest:
                longest = run
                run = 0
            else: #if the number of sequence match is still smaller then longest, then make run zero.
                run = 0
        i += 1
    

提交回复
热议问题