Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subsequence function
#1
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
Reply
#2
Done.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Moved to homework, since it's clearly homework.
Reply
#4
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
Reply
#5
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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.
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020