-2

If I want to know whether some characters are found in a string, how? String will be returned as Boolean. The body is as below :

Return True if the letters 'A', 'T', 'C' or 'G' are found in the string. Return False if it is not.

def is_valid_sequence(dna):

>>> is_valid_sequence('ATCG')
True

>>> is_valid_sequence('AtcGEQ')
False

How to write the code? Thank you.

vaultah
  • 44,105
  • 12
  • 114
  • 143
MingJian
  • 29
  • 2
  • 3

3 Answers3

2

As suggested in this answer - How to check a string for specific characters?

def is_valid(dna)
    if 'A' or 'T' or 'G' or 'C' in dna:
    return True
else:
    return False
Community
  • 1
  • 1
Nike
  • 72
  • 6
0

Your question uses 'A', 'T', 'C' or 'G' but your output suggests it should be 'A', 'T', 'C' and 'G' so you can use all to check that every char in l is in your string:

l = [ 'A', 'T', 'C' ,'G']
s = 'AtcGEQ'

print all(x in s for x in l)
False

s='ATCG'
print all(x in s for x in l)
True

If you want to check if 'A' or 'T' or 'C' or 'G' is in s:

l = [ 'A', 'T', 'C' ,'G']
s= 'AtcGEQ'

print any(x in s for x in l)
True

But that will not match what you have in your question:

>>> is_valid_sequence('AtcGEQ')
False
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Is it possible for the code to work : i = 0 while i < len(dna): dna[i] == 'A' or 'T' or 'C' or 'G' It seems that it works if it is true, but it return T when it's not true. Anything wrong? – MingJian Jun 26 '14 at 10:13
  • you need to use something like `if dna[i] == 'A' or dna[i] == 'T' or dna[i] == 'C' or dna[i] == 'G'` Do you want any to match or do all have to match. You should also add what you tried to your question. – Padraic Cunningham Jun 26 '14 at 10:18
0

A string contains a valid DNA sequence if only the letters ATCG appear in the string (though not necessarily all of them - you could have a gene sequence that doesn't have a C anywhere, although that's of course unlikely).

With that in mind, the function should be

def is_valid_sequence(seq):
    return set(seq).issubset({"A", "T", "C", "G"})

>>> is_valid_sequence("AAAATAATG")
True
>>> is_valid_sequence("AAAATAATGX")
False
>>> is_valid_sequence("AAAATACTAATG")
True
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561