Friday, 19 October 2012

Function in Python

def get_length(dna):
    ''' (str) -> int

    Return the length of the DNA sequence dna.

    >>> get_length('ATCGAT')
    6
    >>> get_length('ATCG')
    4
    '''
    count=0;
    for char in dna:
        count = count + 1;
    return count;

res1 = get_length('ATCGAT')
print(res1)
res2 = get_length('ATCG')
print(res2)

def is_longer(dna1, dna2):
    ''' (str, str) -> bool

    Return True if and only if DNA sequence dna1 is longer than DNA sequence
    dna2.

    >>> is_longer('ATCG', 'AT')
    True
    >>> is_longer('ATCG', 'ATCGGA')
    False
    '''
    l1=get_length(dna1);
    l2=get_length(dna2);
    if(l1>l2):
        return True;
    else:
        return False;

res1=is_longer('ATCG','ATCGGC');
print(res1)
res2=is_longer('ATCG', 'AT')
print(res2)




def count_nucleotides(dna, nucleotide):
    ''' (str, str) -> int

    Return the number of occurrences of nucleotide in the DNA sequence dna.

    >>> count_nucleotides('ATCGGC', 'G')
    2
    >>> count_nucleotides('ATCTA', 'G')
    0
    '''
    count=0;
    for char in dna:
        if(char==nucleotide):
            count=count+1;
    return count;

count1=count_nucleotides('ATCGGC', 'G');
print(count1)
count2=count_nucleotides('ATCTA', 'G');
print(count2)


def contains_sequence(dna1, dna2):
    ''' (str, str) -> bool

    Return True if and only if DNA sequence dna2 occurs in the DNA sequence
    dna1.

    >>> contains_sequence('ATCGGC', 'GG')
    True
    >>> contains_sequence('ATCGGC', 'GT')
    False
   
    '''

    if(dna2 in dna1):
        return True;
    else:
        return False;

res1=contains_sequence('ATCGGC', 'GG');
print(res1)
res2=contains_sequence('ATCGGC', 'GT');
print(res1)



0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel