Question

Implement function subset that takes a set of positive integers and returns asubset of it that...

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

Homework Answers

Answer #1

  

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Python Implement function allEven() that takes a list of integers and returns True if all integers...
Python Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False
1. Let A and B be sets. The set B is of at least the same...
1. Let A and B be sets. The set B is of at least the same size as the set A if and only if (mark all correct answers) there is a bijection from A to B there is a one-to-one function from A to B there is a one-to-one function from B to A there is an onto function from B to A A is a proper subset of B 2. Which of these sets are countable? (mark all...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses...
Python Mutable Sequences Implement a function reverse that takes a list as an argument and reverses the list. You should mutate the original list, without creating any new lists. Do NOT return anything. Do not use any built-in list functions such as reverse(). def reverse(lst):    """Reverses lst in place (i.e. doesn't create new lists).     >>> L = [1, 2, 3, 4]     >>> reverse(L)     >>> L     [4, 3, 2, 1]     """
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a list of integers representing how many integers in each sublist of the original were divisible by 5. Examples: >>> count_div5([[5, 3, 25, 4], [46, 7], [5, 10, 15]]) [2, 0, 3] >>> count_div5([]) [] >>> count_div5([[-20, 10, 2, 4, 5], [], [5], [8, 25, 10], [6]]) [3, 0, 1, 2, 0]
Implement function swap that takes a 2D list (a list of list of integers) and modifies...
Implement function swap that takes a 2D list (a list of list of integers) and modifies it in-place by swapping the first element in each row with the last element in that row. The return value is None. You may not use slicing. You maynot use any list method like append, insert, etc. Modification must be in-place, you may not move the list itself or any of its sublists to a new memory location. Examples: reverse([[1,2,3],[4,5,6]])        ->    [[3,2,1],[6,5,4]] reverse([[1,2,3,4],[5,6,7,8]])    ->    [[4,2,3,1],[8,6,7,5]]...
Python 3 Implement the function invert_and_merge, which takes any number of input dictionaries via a star...
Python 3 Implement the function invert_and_merge, which takes any number of input dictionaries via a star parameter, and inverts and merges them into a single result dictionary. When inverting a dictionary, the keys and values are flipped, so that each value maps to a set containing the corresponding key(s) in the original dictionary. When merging the inverted dictionaries, sets corresponding to the same key are combined. Examples: invert_and_merge({'a': 1, 'b': 2, 'c': 1, 'd': 1, 'e': 2}) should return {1:...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M,...
Write a function intersect(L, M) that consumes two sorted lists of distinct integers L and M, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. Example: intersect([3, 7, 9, 12, 14], [1, 2, 5, 7, 10, 11, 12]) => [7, 12] Hint: As the title hints at, you are...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT