Subsequence function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Subsequence function (/thread-4108.html) |
Subsequence function - tomhuang - Jul-24-2017 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 RE: Subsequence function - ichabod801 - Jul-24-2017 Done. RE: Subsequence function - nilamo - Jul-25-2017 Moved to homework, since it's clearly homework. RE: Subsequence function - nilamo - Jul-25-2017 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 RE: Subsequence function - ichabod801 - Jul-25-2017 Yes, but I think: >>> is_subseq('123', '321') Trueshould 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 RE: Subsequence function - nilamo - Jul-25-2017 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.
RE: Subsequence function - ichabod801 - Jul-25-2017 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. |