0% found this document useful (0 votes)
6 views3 pages

Excercises On Sets

sets set 2 problems

Uploaded by

Sambhav Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Excercises On Sets

sets set 2 problems

Uploaded by

Sambhav Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practice Problems based on Set data type

1. Write a program to check if a given set contains a frozenset as an element.


Ex: Input: {1, 2, 3, frozenset({4, 5}), 6} Output: Yes
2. Write a program to remove all elements from a set that are multiples of 5.
Ex: Input: {10, 22, 15, 35, 40, 42} Output: {22, 42}
3. Write a program to remove empty sets from a list of sets and count how
many were removed.
Ex: Input: [{1, 2}, set(), {3, 4}, { }, {7, 8}] Output: [{1, 2}, {3, 4}, {7, 8}],
Removed: 2
4. Write a program to find the total number of sets in a mixed list.
Ex: Input: [1, {2, 3}, 4, {5, 6}, 7] Output: 2
5. Write a program to flatten a list of sets into a sorted list.
Ex: Input: [{3, 1}, {4, 6, 5}, {9, 7}] Output: [1, 3, 4, 5, 6, 7, 9]

Hints for Set data type-based questions:


1. Check if a given set contains a frozenset as an element.
 Hint:
o Use isinstance() to check if any element is a frozenset. Loop
through the set and use a flag to track if a frozenset is found.
o Key function: isinstance()
o Example: if isinstance(i, frozenset):

2. Remove all elements from a set that are multiples of 5.


 Hint:
o Use set comprehension to iterate through the set. Use the modulus
operator (%) to filter out multiples of 5.
o Key operator: %
o Key structure: Set comprehension
o Example: {x for x in s if x % 5 != 0}

3. Remove empty sets from a list of sets and count how many were removed.
 Hint:
o Use a while loop to iterate through the list. Check the length of
each set using len() and remove empty sets using del. Keep a
counter to track how many sets are removed.
o Key functions: len(), del
o Example: if len(L1[i]) == 0: del L1[i]

4. Find the total number of sets in a mixed list.


 Hint:
o Use isinstance() to check if an element is a set. Loop through the
list and increment the counter for each set found.
o Key function: isinstance()
o Example: if isinstance(i, set):

5. Flatten a list of sets into a sorted list.


 Hint:
o Use set().union() to merge all the sets together. Then, use sorted()
to sort the elements in ascending order.
o Key functions: set().union(), sorted()
o Example: sorted(set().union(*L1))

PROGRAMS
1.
s = {1, 2, 3, frozenset({4, 5}), 6}
a=1
for i in s:
if isinstance(i, frozenset):
a=0
break
if a == 0:
print('Yes')
else:
print('No')

2.

s = {10, 22, 15, 35, 40, 42}


s = {x for x in s if x % 5 != 0}
print(s)
3.

L1 = [{1, 2}, set(), {3, 4}, set(), {7, 8}]


count = 0
i=0
while i < len(L1):
if len(L1[i]) == 0:
del L1[i]
count += 1
i -= 1
i += 1
print(L1, "Removed:", count)

4.
L1 = [1, {2, 3}, 4, {5, 6}, 7]
a=0
for i in L1:
if isinstance(i, set):
a += 1
print("Number of sets:", a)

5.
L1 = [{3, 1}, {4, 6, 5}, {9, 7}]
L2 = sorted(set().union(*L1))
print(L2)

You might also like