Beispiel #1
0
    def __init__(self):
        '''pre: None
           post: Construct the empty ordered Set
           
        '''

        self.set = DLList()
    def __init__(self):
        '''pre: None
           post: Construct the empty ordered Set
           
        '''

        self.set = DLList()
Beispiel #3
0
class OrderedSet(object):
    '''The OrderedSet is an ordered collection of unique elements. with following operations
       Construction/Insertion/str/len/in/getset/remove/removeFirst
   '''

    #---------------------------------------------------------------------------------
    #---------------------------------------------------------------------------------
    def __init__(self):
        '''pre: None
           post: Construct the empty ordered Set
           
        '''

        self.set = DLList()
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def insert(self, x):
        '''pre: X is of the same type as the objects in OrderedSet
           post: X is inserted into OrderedSet (to the right position)
           
           
        '''

        #if x is already in the set
        #remove the existing one
        #insert x to the right position
        if x in self:
            self.remove(x)
            self.insert(x)
            return

        #if x not in the set
        #insert x to the right position
        index = len(self.set)

        for i in range(len(self.set) - 1, -1, -1):

            if x <= self.set[i]:
                index = i
        if index == len(self.set):
            self.set.insert(index, x)
            return
        if self.set[index] == x:
            self.set.insert(index + 1, x)
            return

        self.set.insert(index, x)

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def __str__(self):
        '''
        pre:NOne
        Post: return a string representation of the orderedset
        '''

        #retur [] if the set is empty
        if len(self.set) == 0:
            return '[]'

        #generate the correct string representation for non-empty set
        s = '['
        s = s + str(self.set[0]) + '*'

        for i in range(1, len(self.set)):

            s = s + ', ' + str(self.set[i])
        s = s + ']'
        return s
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def __contains__(self, x):
        '''pre: None
           post: 1. return True if x is inside the orderedset
                 2. return False otherwise
        '''

        #Check whether i
        for i in self.set:
            if i is x:
                return True
        return False

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def remove(self, x):
        '''pre:element x is in the list
           post:elemnt x is removed from the list
           exception: IndexError if x is not in the list in the frist place
        '''
        #go into the set to check whether x is in the set
        #if it is, remove item x
        for i in self.set:
            if i is x:
                print(i)
                self.set.remove(i)
                return
        #raise IndexError if not found
        raise IndexError

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def removeFirst(self):
        '''
        pre:NOne
        post:1.removes and return the first element from the ordered set
             2. return None if the ordered set is empty
        '''

        #return None if the set is empty
        print(len(self))
        if len(self) == 0:
            return None
        #pop the first item if not empty
        return self.set.pop(0)

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------

    def __len__(self):
        '''
        pre: None
        post:return the length of the ordered set
        '''

        return len(self.set)
Beispiel #4
0
 def __init__(self):
     SLLQueue.__init__(self)
     self.deque = DLList()
Beispiel #5
0
class MaxQueue(SLLQueue):
    def __init__(self):
        SLLQueue.__init__(self)
        self.deque = DLList()

    def add(self, x: np.object):
        super().add(x)
        if self.deque.size() == 0:
            self.deque.add(0, x)
        else:
            while self.deque.size() != 0 and self.deque.dummy.next.x < x:
                self.deque.remove(self.deque.n - 1)
            self.deque.add(self.deque.n, x)

    def remove(self) -> np.object:
        if self.n == 0: return None
        if self.head.x == self.deque.dummy.prev.x:
            self.deque.remove(0)
        return super().remove()

    def max(self) -> np.object:
        if self.deque.size() == 0:
            return None
        return self.deque.dummy.prev.x
class OrderedSet(object):
    '''The OrderedSet is an ordered collection of unique elements. with following operations
       Construction/Insertion/str/len/in/getset/remove/removeFirst
   '''
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def __init__(self):
        '''pre: None
           post: Construct the empty ordered Set
           
        '''

        self.set = DLList()
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def insert(self,x):
        '''pre: X is of the same type as the objects in OrderedSet
           post: X is inserted into OrderedSet (to the right position)
           
           
        '''
        
       

        #if x is already in the set
        #remove the existing one
        #insert x to the right position
        if x in self:
            self.remove(x)
            self.insert(x)
            return
        
            
        #if x not in the set
        #insert x to the right position
        index = len(self.set)
        
        for i in range(len(self.set)-1,-1,-1):
            
                
            if x <= self.set[i]:
                index = i
        if index == len(self.set):
            self.set.insert(index,x)
            return
        if self.set[index] == x:
            self.set.insert(index+1,x)
            return
             
            
        self.set.insert(index,x)

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------    
    def __str__(self):
        '''
        pre:NOne
        Post: return a string representation of the orderedset
        '''

        #retur [] if the set is empty
        if len(self.set) == 0:
            return '[]'

        #generate the correct string representation for non-empty set
        s = '['
        s = s + str(self.set[0])+'*'
        
        for i in range (1,len(self.set)):

            s = s + ', ' + str(self.set[i])
        s = s + ']'
        return s
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def __contains__(self,x):
        '''pre: None
           post: 1. return True if x is inside the orderedset
                 2. return False otherwise
        '''

        #Check whether i 
        for i in self.set:
            if i is x:
                return True
        return False

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def remove(self,x):
        '''pre:element x is in the list
           post:elemnt x is removed from the list
           exception: IndexError if x is not in the list in the frist place
        '''
        #go into the set to check whether x is in the set
        #if it is, remove item x
        for i in self.set:
            if i is x:
                print(i)
                self.set.remove(i)
                return
        #raise IndexError if not found
        raise IndexError

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def removeFirst(self):
        '''
        pre:NOne
        post:1.removes and return the first element from the ordered set
             2. return None if the ordered set is empty
        '''

        #return None if the set is empty
        print(len(self))
        if len(self)==0:
            return None
        #pop the first item if not empty
        return self.set.pop(0)

#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
    def __len__(self):
        '''
        pre: None
        post:return the length of the ordered set
        '''

        return len(self.set)