Hello again, it is me with another homework hurdle. I am currently tasked with creating a Set class. We are not permitted to use Python's set class. I have looked at Python's set class for hints, but I had difficulty understanding it. We are not allowed to add methods not listed in the assignment to the class.

I am using Windows Vista Business, and Python 2.3.

The assignment requires me to create a class named Set with the following methods:

Set(elements) Create a set
addElement(x) Adds x to the set
deleteElement(x) Removes x from the set, if present. If x is not in the set, the set is left unchanged
member(x) Returns true is x is in the set and false otherwise
intersection(set2) Returns a new set containing just those elements that are common to this set and set 2
union(set2) Returns a new set containing all of elements that are in this set, set2, or both
subtract(set2) Returns a new set containing all of the elements of this set that are not in set2
__str__() to construct strings in normal mathematical notation
clear() to remove all items from a set
equal() to text if 2 sets are equal
count() to report how many items are in a set
isSubset() Reports if a set is a subset of another set.

I have already made a bit of headway on the assignment.

class Set:
    def __init__(self, elements):
        self.elements = {}
        for elem in elements:
            self.elements[elem] = None

    def __str__(self):
        return str(self.elements.keys())

    def addElement(self, x):
        #adds the element to the set
        self.elements[x] = None

    def deleteElement(self, x):
        #remove the element from the set
        del self.elements[x]
        
    def member(self, x):
        #checks to see if the element is in the set
        if self.elements.has_key(x) == True: return True
        else: return False
        
    def intersection(self, set2):
        #show the numbers that are in both sets
        set3 = Set([])
        for i in self.elements:
            if set2.member(i) == True:
                set3.addElement(i)
        return set3

    def union(self, set2):
        #put the numbers from the first set that are not in the second set into the second set
        set3 = Set([])
        for i in set2.elements:
            if i not in self.elements:
                set3.addElement(i)
        for i in self.elements:
            if set2.member(i) == True:
                set3.addElement(i)
            if i not in set2.elements:
                set3.addElement(i)
        return set3

    def subtract(self, set2):
        #remove the numbers in the second set from the first set
        set3 = Set([])
        for i in self.elements:
            if set2.member(i) == False:
                set3.addElement(i)
        return set3
        
    def clear(self):
        #empty the set
        set3 = Set([])
        set3.clear()

    """def equal(self, set2):
        #tests to see if the first set has the exact same elements as the second set                
        
    def count(self):
        #count the number of items in the set
        setList = len(self.elements.keys())
        return setList

    def isSubset(self, set2):
        #tests to see if the second set is a subset of the first set"""

I am currently stuck on making clear() work for either my professor's test code or any Set. Once I get clear() working, I can tackle equal(), count(), and isSubset().

So, if anyone has any suggestions, please post them. And, thank you for your time.

Recommended Answers

All 5 Replies

This should do it.

def clear(self):
	self.elements = {}

Thank you, that really did help me out.

I ran out of time and I was not able to complete the entire assignment. Only the final module gave me trouble. So, I will post what I was able to complete and hopefully it will help someone else out in the future:

class Set:
    def __init__(self, elements):
        self.elements = {}
        for elem in elements:
            self.elements[elem] = None

    def __str__(self):
        return str(self.elements.keys())

    def addElement(self, x):
        #adds the element to the set
        self.elements[x] = None

    def deleteElement(self, x):
        #remove the element from the set
        del self.elements[x]
        
    def member(self, x):
        #checks to see if the element is in the set
        if self.elements.has_key(x) == True: return True
        else: return False
        
    def intersection(self, set2):
        #show the numbers that are in both sets
        set3 = Set([])
        for i in self.elements:
            if set2.member(i) == True:
                set3.addElement(i)
        return set3

    def union(self, set2):
        #put the numbers from the first set that are not in the second set into the second set
        set3 = Set([])
        for i in set2.elements:
            if i not in self.elements:
                set3.addElement(i)
        for i in self.elements:
            if set2.member(i) == True:
                set3.addElement(i)
            if i not in set2.elements:
                set3.addElement(i)
        return set3
       
    def subtract(self, set2):
        #remove the numbers in the second set from the first set
        set3 = Set([])
        for i in self.elements:
            if set2.member(i) == False:
                set3.addElement(i)
        return set3
        
    def clear(self):
        #empty the set
        self.elements = {}

    def equal(self, set2):
        #tests to see if the first set has the exact same elements as the second set
        if len(self.elements) == len(set2.elements):
            for i in self.elements:
                if set2.member(i) == True:
                    return True
                else:
                    return False
        else:
            return False
    def count(self):
        #count the number of items in the set
        setList = len(self.elements.keys())
        return setList

    def isSubset(self, set2):
        #tests to see if the first set is a subset of the second set
        if len(self.elements) <= len(set2.elements):
            for i in self.elements:
                if i not in set2.elements:
                    return False
                else:
                    return True
        else:
            return False

In my opinion, that assignment is a terrible assignment.

I would rather use magic methods to make such a set class, i.e. __iter__, __setitem__, __eq__, __add__, __getitem__

In my opinion, that assignment is a terrible assignment.

I would rather use magic methods to make such a set class, i.e. __iter__, __setitem__, __eq__, __add__, __getitem__

Also I think using dict for such data type as set is like using lists to implement strings. I can be wrong though.

Can you mark solved or tell what is still unclear/post results?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.