Python Forum

Full Version: Subsequence function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Write a python function to determine whether or not string x is a subsequence of y. Unlike substrings, subsequences do not have to be consecutive. For example:
is_subseq('13','123')
True

is_subseq('india','indonesia')
True

my code here
Done.
Moved to homework, since it's clearly homework.
Also, here ya go :)

>>> is_subseq = lambda x, y: set(x) == set(x) & set(y)
>>> is_subseq('13', '123')
True
>>> is_subseq('india', 'indonesia')
True
>>> is_subseq('13', '1245')
False
>>> is_subseq('123', '13')
False
Yes, but I think:

>>> is_subseq('123', '321')
True
should be False. I took it to mean that consecutive didn't matter, but order did. Also I would think duplicates would matter:

>>> is_subseq('loose', 'lose')
True
It's a shame this was homework.  The monkey wrenches you threw my way made for a fun little trist into the joys of str.find() and it's second parameter, the start_index.
I just did a loop consuming the search string. But that's me. If I can do it with loops and conditionals I don't think about fancy methods and data types.