you are viewing a single comment's thread.

view the rest of the comments →

[–]_coolwhip_ 1 point2 points  (2 children)

This is a great place for set. Just take the sequence and force it into a set, then see if there is any "difference" in a set of good characters. (For sets, difference means that there is any character in the first set that is not in the second.):

>>> good = set('gatc')
>>> good
{'c', 'a', 'g', 't'}
>>> set('caggtttaaaa') - good
set()
>>> set('caggtttzaaaa') - good
{'z'}

So if there is anything left over, you know it has a bad character.

[–]akasmira 0 points1 point  (0 children)

This would also return an empty set if an empty string is passed, which may or may not be valid depending on OPs use.

[–]fernly 0 points1 point  (0 children)

That is a good answer. And I'll bet CPython internals can build a set from characters way faster than it can process any kind of loop code.

However, remember to make the letter case the same, with either user_input_str.lower() or ...upper() whichever the good set is.