Implement function subset that takes a set of positive integers and returns asubset of it that includes only those numbers that can form a sequence. It's ok if more than one sequences co-exist in the subset. You may not use lists, tuples, strings, dictionaries; only set functions/methods are allowed.
Examples:
subset({0,4,11,5,3,2,7,9}) -> {4,5,3,2}
subset({3,1,6,8,2,12,9}) -> {3,1,2,8,9}
True
False
In Python Please
def subset(s):
s1=set()
for i in s:
if i+1 in s:
s1.add(i)
s1.add(i+1)
return s1
print(subset({0,4,11,5,3,2,7,9}))
this is quite simple
for a sequence we just need to check if the next number is present in the set or not
if the next number is present then add both the numbers in the new set
and because this is set we will not have any repeated value
Get Answers For Free
Most questions answered within 1 hours.