Python Sets Exercises With Solutions





Exercices 200 || Solution

Write a program in python as a function that determines the intersection of two sets without using any predefined functions.
Example if

E = {"Java", "Python", "Javascript", "C ++", "C #"} 
and 
F = {"VB.ET", "Java", "Kotlin", "Python"}

, the function returns the set:

{"Java", "Python"}

Exercise 201 || Solution

Write a python program as a function that determines the minimum and maximum
values of given a set withowt using any predefined function.
Example if

E = {13 , 2 , 47 , 23 , 71 , 17}

, the function returns (2,71)


Exercise 202 || Solution

Write a program in python as a function which examines whether two sets are disjoint or not.
If the two sets are disjoint, the function must return True and False if not.





Exercise 203 || Solution

Write a python program as a function that determines the union of two sets
without using any predefined functions. Example if

E = {"Lenovo", "Hp", "del" , "Accent" , "Asus"}

and

F = {"Acer", "Thomson"}

, the function returns the set:

{"Lenovo", "Hp", "del" , "Accent" , "Asus" , "Acer", "Thomson"}

Exercise 204 || Solution

Write a Python program as function which takes as parameters two sets E and F,
such that E is included in F and returns the complement of F in E.
Example if

E = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}

and

F = { 'c', 'd', 'e',  'h'}

The function returns:

{'a', 'b', 'f', 'g'}

Exercise 205 || Solution

Given the following set of numerical tuples:

E = {(12, 3 , 7 , 11) , (2, 9 , 5 , 13) , (1, 4 ,8)}

Write a Python program that creates a set F whose elements are (Tuple , average of Tuple). Exemple for the set E cited above,
the output must be:

F =  {((2, 9, 5, 13), 7.25), ((12, 3, 7, 11), 8.25), ((1, 4, 8), 4.333333333333333)}

Exercise206 || Solution

Given two sets A = {'a' , 'b' , 'c' , 'd'} and B = {'c' , 'e' , 'd' , 'h'}. Write a Python program that returns their symmetric difference without using the python symmetric_difference() method.


Exercise 207 || Solution

Given a set A = { 'a', 'b', 'c', 'd' }. Write an algorithm in Python that adds an 'x' element to A without using the add() method.


Exercise 208 || Solution

Write a program in Python that remove an element from a given set A without using the discard() or remove() methods.


Exercise 209 || Solution

Write a program in Python language that returns the intersection and the union of the following three sets:

A = {11, 21, 5, 7, 43, 32, 13, 9}
B = {2, 19, 11, 33, 7, 25, 5, 4}
C = {45,27,11,5,7,22,14,1}





Exercise 210 || Solution

Resume the previous exercise (Exercise 209) without using the intersection() and union() methods.


Exercise 211 || Solution

Write a Python program that returns the set of perfect square integers less than or equal to 100.


Exercise 212 || Solution

Create a python program that determines the set of odd integers less than or equal to 100 that are multiple of 3.

Exercise 213 || Solution

Write a python program that determines the set of prime numbers from 1 to 100.

 

Younes Derfoufi
CRMEF OUJDA