CodeNO-1_implementation

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.4 kB
2
Indexable
Never
class Set:
    def length(self, List):
        index = 0
        while True:
            try:
                ele = List[index]
                index = index + 1
            except:
                break
        return index

    def isPresent(self, List, tarEle):
        for i in range(self.length(List)):
            if List[i] == tarEle:
                return True
        return False

    def genSet(self):
        result = []
        no = int(input("Enter number of students: ", ))
        for i in range(no):
            rollNo = int(input("Enter roll no of student: ", ))
            if self.isPresent(result, rollNo):
                print("Roll no already stored!!")
                rollNo = int(input("Re-enter roll no of student: ", ))
                result.append(rollNo)
            else:
                result.append(rollNo)
        return result

    def Union(self, List1, List2):
        len1 = self.length(List1)
        len2 = self.length(List2)
        result = []
        if len1 != 0 and len2 != 0:
            for i in range(len1):
                ele = List1[i]
                result.append(ele)

            for i in range(len2):
                if self.isPresent(result, List2[i]):
                    continue
                else:
                    result.append(List2[i])
            return result
        elif len1 == 0 and len2 == 0:
            return result
        else:
            if len1 == 0:
                return List2
            else:
                return List1

    def intersection(self, List1, List2):
        len1 = self.length(List1)
        len2 = self.length(List2)
        result = []
        if len1 != 0 and len2 != 0:
            for i in range(len1):
                ele = List1[i]
                if self.isPresent(List2, ele):
                    result.append(ele)
            return result
        else:
            return result

    def difference(self, List1,List2):
        len1 = self.length(List1)
        len2 = self.length(List2)
        result = []
        if len1 == 0 and len2 == 0:
            return result
        elif len1 == 0:
            return result
        else:
            for i in range(len1):
                ele = List1[i]
                if self.isPresent(List2, ele):
                    continue
                else:
                    result.append(ele)
            return result

A1 = Set()
print("Enter details of students in class")
sample = A1.genSet()
print("Enter Mango students")
mango = A1.genSet()
print("Enter grapes students")
grapes = A1.genSet()
print("Enter oranges students")
oranges = A1.genSet()

print()
print("Mango: ", mango)
print("Grapes: ", grapes)
print("Oranges: ", oranges)

print()
print("Students who like both mango and grapes-")
inter = A1.intersection(mango, grapes)
print(inter)

print()
print("Students who like either mango or grapes but not both-")
inter = A1.intersection(mango, grapes)
union = A1.Union(mango, grapes)
diff = A1.difference(union, inter)
print(diff)

print()
print("Students who like neither mango nor grapes-")
diff = A1.difference(sample, union)
print(diff)

print()
print("Number of students who like mango and oranges but not grapes-")
inter = A1.intersection(mango, oranges)
diff = A1.difference(inter, grapes)
print(A1.length(diff))