Python Tuples, Sets, Shallow Copy, Deep Copy
Exercise on Sets
The following employee ids where collected during three days of training
Day 1
12336,12338,12339,12467,12789,12987,12888
Day 2
12888,12339,12467,12789,12987
Day 3
12336,12338,12619,12467,12789,12555,12888
Display the employee ids of employees who have attended all three days of training
Display the total number of employees who attended the training
Display the employee ids who attended Day3 training by not Day2
Output
Solution
day1={12336,12338,12339,12467,12789,12987,12888}
day2={12888,12339,12467,12789,12987}
day3={12336,12338,12619,12467,12789,12555,12888}
#members who attended all days of training
print(day1.intersection(day2).intersection(day3))
#total number of participants in three days
print(len(day1.union(day2).union(day3)))
#people who attended day 3 but not day2
print(day3.difference(day2))
Output
Exercise 2 on Sets
Read names for n players from two teams for two different clubs(club1 and club2) There can be players who play for both the clubs.
Identity the following
List the players who play for both the clubs
List the players who play for only one club
List the players who play only for club1 and not for club2
Display the count of players who play for only one club
Display “No common players�? message if there are no common players in both the clubs
If all the players of one club(club2) play for another club(club1) then display club1 contains club2
Output
Solution
m={1,3,4,5}
k={5,4,8}
print(m.difference(k).union(k.difference(m)))
m={1,3,4,5}
k={5,4,8}
print(len(m.difference(k).union(k.difference(m))))
m={1,3,4,5}
k={6,7,8,5}
if(m.intersection(k)):
print("They have players")
else:
print("They dont have any players")
club1={1,3,4,5}
club2={5,4}
if(club2.issubset(club1)):
print("Club1 contains club2")
else:print("not true")
Output