Skip to content

Instantly share code, notes, and snippets.

@lucpet
Last active December 22, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucpet/6545959 to your computer and use it in GitHub Desktop.
Save lucpet/6545959 to your computer and use it in GitHub Desktop.
My answers to Assignment 2
def get_length(dna):
""" (str) -> int
Return the length of the DNA sequence dna.
>>> get_length('ATCGAT')
6
>>> get_length('ATCG')
4
"""
return len(dna)
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
"""
return len(dna1) > len(dna2)
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
"""
str1 = dna
str2 = nucleotide
return str1.count(str2)
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('GGATCC', 'GG')
True
>>> contains_sequence('ATCCGG', 'GG')
True
>>> contains_sequence('ATCGGC', 'GT')
False
"""
if dna2 in dna1:
return True
if dna2 not in dna1:
return False
def is_valid_sequence(dna):
''' (str) -> bool
Return True if and only if the DNA sequence is valid
(that is, it contains no characters other than 'A', 'T', 'C' and 'G').
>>> is_valid_sequence('ATCG')
True
>>> is_valid_sequence('ATCg')
False
>>> is_valid_sequence('atcg')
False
>>> is_valid_sequence('')
False
'''
val_seq = True
for char in dna:
if char not in 'ATCG':
val_seq = False
return val_seq
def insert_sequence(dna1, dna2, int1):
''' (str, str, int) -> str
Return the DNA sequence obtained by inserting the second DNA
sequence into the first DNA sequence at the given index.
(You can assume that the index is valid.)
>>> insert_sequence('CCGG', 'AT', 2)
'CCATGG'
'''
return dna1[:int1] + dna2 + dna1[int1:]
def get_complement(dna):
''' (str) -> str
Return the complimentry nucleotide for
a given nucleotide
>>> get_complement('A')
'T'
>>> get_complement('G')
'C'
>>> get_complement('T')
'A'
>>> get_complement('C')
'G'
'''
if dna == 'A':
return 'T'
elif dna == 'G':
return 'C'
if dna == 'T':
return 'A'
elif dna == 'C':
return 'G'
def get_complementary_sequence(dna):
''' (str) -> str
Return the DNA sequence that is
complementary to the given DNA sequence.
>>> get_complementary_sequence('AT')
'TA'
>>> get_complementary_sequence('ACGTACG')
'TGCATGC'
>>> get_complementary_sequence('ACGTACGACGTACG')
'TGCATGCTGCATGC'
'''
r = ""
for i in dna:
r = r + get_complement(i)
return r
@kryptykphysh
Copy link

get_complementary_sequence isn't calling get_complement on the 'i' variable to get the complementary nucleotide.

@lucpet
Copy link
Author

lucpet commented Sep 13, 2013

Where does it say I have to use it

@kryptykphysh
Copy link

It doesn't, but: Return the DNA sequence that is
complementary to the given DNA sequence.

This requires that each character be replaced with its complement, which is what get_complement does (for a single character).

r = ""
for i in comp:
r = i + r
return r

will just reverse the i string.

It should be r = r + get_complement(i)

@lucpet
Copy link
Author

lucpet commented Sep 13, 2013

ooooooooooh ok thanks

@lucpet
Copy link
Author

lucpet commented Sep 13, 2013

Ok fixed up the get_compliment as well, posted the old version earlier.

@drummer777
Copy link

Hey buddy! Have you done assignment 3! please help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment