Example of SET ADT in python
Two students are taking different course . How a set data structure will help to apply some operations to obtain information.
Two students are taking different course . How a set data structure will help to apply some operations to obtain information.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set of two students courses names | |
# student 1 | |
smith = set() | |
smith.add( "CSCI-112" ) | |
smith.add( "MATH-121" ) | |
smith.add( "HIS-340" ) | |
smith.add( "ECON-101" ) | |
# stuendt 2 | |
roberts = set() | |
roberts.add( "POL-101" ) | |
roberts.add( "ANTH-230" ) | |
roberts.add( "CSCI-112" ) | |
roberts.add( "ECON-101" ) | |
# students have taken exact same or not | |
# if not then find same course | |
if smith == roberts: | |
print("Smith and Roberts are taking the same courses. ") | |
else: | |
sameCourses = smith.intersection(roberts) | |
# print(sameCourses) | |
if len(sameCourses) == 0: | |
print("Smith and Roberts are not taking any of the same courses. ") | |
else: | |
print("Smith and Roberts are taking some of the same courses. ") | |
# printing same courses | |
for course in sameCourses: | |
print(course) | |
# Courses only smith is taking not roberts | |
print("Courses only smith is taking not roberts") | |
uniqueCourses = smith.difference(roberts) | |
for course in uniqueCourses: | |
print(course) |
SET ADT Using Students courses example
Reviewed by Leaf Code
on
September 15, 2020
Rating:

No comments: