Set operations

 # Set manipulation


"""

The second year of engineering offers the following subjects in two departments. 

CSE = ["PQT", "ADC", "SE", "DPSD", "DS", "OOP"]

IT = ["OS", "PQT", "DS", "SE", "OOP", "SS"]


Write a program to create set of subjects for the two departments.  

Use set operations to find 

1. Number and names of subjects in CSE

2. Number and names of subjects in IT

3. Names of subjects in CSE only

4. Names of subjects in IT only

5. Number and names of subjects in both CSE and IT

6. Number and names of subjects for either CSE or IT, but not in both

7. Number and names of all subjects.

"""


# Converting Given List to set

CSE = set(["PQT", "ADC", "SE", "DPSD", "DS", "OOP"])

IT = set(["OS", "PQT", "DS", "SE", "OOP", "SS"])


print("CSE =",CSE)

print("IT =",IT)



# 1. Number and names of subjects in CSE

print("Number of subjects in CSE = ", len(CSE))

print("Subjects in CSE : ")

for sub in CSE:

print(sub, end=", ")

print()


# 2. Number and names of subjects in IT

print("Number of subjects in IT = ", len(IT))

print("Subjects in IT : ")

print(", ".join(IT))


# 3. Names of subjects in CSE only

print("Names of subjects in CSE only : ", CSE.difference(IT))


# 4. Names of subjects in IT only

print("Names of subjects in IT only : ", IT.difference(CSE))


# 5. Number and names of subjects in both CSE and IT

CommonSubjects = CSE.intersection(IT)

print("Number of common subjects in CSE and IT =",len(CommonSubjects))

print("Names of common subjects in CSE and IT :",CommonSubjects)


# 6. Number and names of subjects for either CSE or IT, but not in both

UniqueSubjects = CSE.symmetric_difference(IT)

print("Number of subjects for either CSE or IT, but not in both = ", len(UniqueSubjects))

print("Names of subjects for either CSE or IT, but not in both = ", UniqueSubjects)


# 7. Number and names of all subjects.

Allsub=CSE.union(IT)

print("Number of subjects : ", len(Allsub))

print("Names of all subjects : ", Allsub)


"""

Sampleoutput

>python Set.py

CSE = {'PQT', 'SE', 'DPSD', 'ADC', 'DS', 'OOP'}

IT = {'PQT', 'SS', 'SE', 'OS', 'DS', 'OOP'}

Number of subjects in CSE =  6

Subjects in CSE :

PQT, SE, DPSD, ADC, DS, OOP,

Number of subjects in IT =  6

Subjects in IT :

PQT, SS, SE, OS, DS, OOP

Names of subjects in CSE only :  {'ADC', 'DPSD'}

Names of subjects in IT only :  {'SS', 'OS'}

Number of common subjects in CSE and IT = 4

Names of common subjects in CSE and IT : {'OOP', 'PQT', 'DS', 'SE'}

Number of subjects for either CSE or IT, but not in both =  4

Names of subjects for either CSE or IT, but not in both =  {'SS', 'ADC', 'DPSD', 'OS'}

Number of subjects :  8

Names of all subjects :  {'PQT', 'SS', 'SE', 'DPSD', 'OS', 'ADC', 'DS', 'OOP'}

"""

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu