简体   繁体   中英

What did I do wrong with this function?

I don't know what I did - it's wrong . Can someone help me?

def insert_sequence(dna1, dna2, number):

    '''(str, str, int) -> str
    Return the DNA sequence obtained by inserting the second DNA sequence
    at the given index. (You can assume that the index is valid.)  

    >>> insert_sequence('CCGG', 'AT', 2)
    'CCATGG'
    >>> insert_sequence('TTGC', 'GG', 2)
    'TTGGGC'
    '''
    index = 0
    result = '';
    for string in dna1:
        if index == number:
            result = result + dna2

            result = result + string
            index += 1

            print(result)

Here's a solution:

def insert_sequence(dna1, dna2, number):

    '''(str, str, int) -> str
    Return the DNA sequence obtained by inserting the second DNA sequence
    at the given index. (You can assume that the index is valid.)  

    >>> insert_sequence('CCGG', 'AT', 2)
    'CCATGG'
    >>> insert_sequence('TTGC', 'GG', 2)
    'TTGGGC'
    '''

    return dna1[:number] + dna2 + dna1[number:]

you needed an if-else loop here :

def insert_sequence(dna1, dna2, number):


    result = '';

    #you can use enumerate() to keep track of index you're on

    for ind,x in enumerate(dna1): 
        if ind == number:            #if index is equal to number then do this
            result = result + dna2 +x
        else:                        #otherwise do this   
            result = result + x 


    print(result)

insert_sequence('CCGG', 'AT', 2)
insert_sequence('TTGC', 'GG', 2)

output:

CCATGG
TTGGGC

There are already right working functions in other answers (specially the comment from Rakesh Pandit and the answer from JeffS), but your actual question is "why my original function doesn't work".

I copied a working version of your function, comments below:

def insert_sequence(dna1, dna2, number):

    index = 0
    result = ''

    for character in dna1:
        if index == number:
            result = result + dna2
        result = result + character
        index += 1
    print(result)

Python considers indentation, so you should print only at the end of things, outside loops and ifs. When you "increase" your result, you do this only inside the "if" on your function, when actually you should increase "for every character in dna1", and only when/"if index == number" you should put the middle string inside.

I believe you are very new to Python or to programming in general, being probably from a biological background, but you really shouldn't iterate to get this type of string operation done, as others have shown.

Hope this helps!

You're never splitting the string apart, so you'll always prepend dna2 to dna1.

You probably want to return dna1[:number] + dna2 + dna1[number:]

You do nothing if the index is not at the insertion point, including incrementing the index. Your code needs an else and you are also printing prematurely:

def insert_sequence(dna1, dna2, number):
    index = 0
    result = '';
    for char in dna1:
        if index == number:
            result = result + dna2
            result = result + char
            index += len(dna2) + 1
        else:
            result = result + char
            index += 1
    print(result)

mistakes made: a) parameter index is initialised to 0. b) "for sting in dia1:" should have been "for dia1_position in range(len(dia1)):" c) print result indentation is wrong and function isn't just supposed to print. It should return result. d) index need not be incremented now.

Answers are already there. Above briefly lists the mistakes made. I guess you didn't see any error because you never called the function. First error should be "number" not defined (not any more as question has been updated and parameter has number defined).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM