예제 #1
0
class LinkedQueue:
    def __init__(self):
        self.data = Doubly_Linked_List()

    def __len__(self):
        return len(self.data)

    def is_empty(self):
        return self.data.is_empty()

    def first(self):
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self.data.first()

    def dequeue(self):  # O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self.data.delete_first()

    def enqueue(self, e):  # O(1)
        self.data.insert_last(e)

    def __str__(self):
        return str(self.data)
예제 #2
0
class LinkedQueue():
    def __init__(self, N=10):
        self._data = Doubly_Linked_List()
        self._size = 0

    def __len__(self):
        return self._size

    def is_empty(self):
        return self._size == 0

    def first(self):  #O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        return self._header._next._element

    def dequeue(self):  #O(1)
        if self.is_empty():
            raise Empty("Queue is Empty")
        self._size -= 1
        return self._data.delete_first()  ##???

    def enqueue(self, e):  #O(1)
        self._size += 1
        self._data.insert_last(e)  ##???

    def __str__(self):
        ''' You can simply print self._data '''
        return str(self._data)  #print directly or as string
예제 #3
0
class Integer():
    def __init__(self, num_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(num_str)):
            self._data.insert_last(num_str[i])

    def __add__(self, other):
        carrier = 0
        newInt = 0
        result = Integer()
        currNode = self._data._trailer
        myNode = other._data._trailer
        if self._data.is_empty() or other._data.is_empty():
            raise Exception("invalid input")
        while currNode._prev is not self._data._header:
            currNode = currNode._prev
            if myNode._prev is not self._data._header:
                myNode = myNode._prev
                newInt = int(currNode._element) + int(myNode._element)
            if carrier != 0:
                newInt += 1
                carrier -= 1
            if newInt >= 10:
                newInt -= 10
                carrier += 1
            result._data.insert_first(str(newInt))

        if currNode._prev == self._data._header:
            mybool = False
            while myNode._prev is not other._data._header:
                myNode = myNode._prev
                result._data.insert_first(myNode._element)
        if myNode._prev == other._data._header:
            obool = False
            while currNode._prev is not self._data._header:
                currNode = currNode._prev
                result._data.insert_first(currNode._element)
        return result

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            repstr += currNode._element
            currNode = currNode._next
        return repstr


# n1 = Integer('375')
# n2 = Integer('4029')
# print(n1)
# print(n2)
# n3 = n1 + n2
# print(n3)
예제 #4
0
class CompactString():

    def __init__(self, orig_str):
        self._data = Doubly_Linked_List()
    	for i in range(len(orig_str)):
    		if i > 0 and orig_str[i] !=  orig_str[i-1]:
    			self._data.insert_last((orig_str[i], 1))
    		else:
    			mylast = self._data.last()
    			tup1 = mylast[1]
    			self._data._trailer._prev._element = (orig_str[i], tup1+1)

    def __add__(self, other):
    	if self._trailer._prev._element[0] == other._header._next._element[0]:

    	else: 
    		self._trailer._prev._next = other._header._next
    		other._header._next._prev = self._trailer._prev


        for
    def __lt__(self, other):
    	if self._data._header._next == self._data._trailer and other._data._header._next == other._data._trailer: 
    		return False
    	elif other._data._header._next == self._data._trailer: 
    		return False
    	elif self._data._header._next == self._data._trailer: 

    def __eq__(self, other):
    	if self._data._header._next == self._data._trailer and other._data._header._next == other._data._trailer: 
    		return True

    def __le__(self, other):

    	if self == other: 
    		return True 
        
    def __gt__(self, other):
        
    def __ge__(self, other):

    	if self == other: 
    		return True 

    def __repr__(self):
예제 #5
0
class Integer():
    def __init__(self, num_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(num_str)):
            self._data.insert_last(num_str[i])

    def __add__(self, other):
        carrier = 0
        newInt = 0
        result = Integer()
        currNode = self._data._trailer
        myNode = other._data._trailer
        while currNode._prev is not self._data._header:
            currNode = currNode._prev
            if myNode._prev is not self._data._header:
                myNode = myNode._prev
                newInt = int(currNode._element) + int(myNode._element)
            if carrier != 0:
                newInt += 1
                carrier -= 1
            if newInt >= 10:
                newInt -= 10
                carrier += 1
            result._data.insert_first(str(newInt))

        if currNode._prev == self._data._header:
            mybool = False
            while myNode._prev is not other._data._header:
                myNode = myNode._prev
                result._data.insert_first(myNode._element)
        if myNode._prev == other._data._header:
            obool = False
            while currNode._prev is not self._data._header:
                currNode = currNode._prev
                result._data.insert_first(currNode._element)
        return result

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            repstr += currNode._element
            currNode = currNode._next
        return repstr
예제 #6
0
			now = merge_sublists(Node1._next, Node2)
			while Node1 is not srt_lnk_lst1._trailer._prev:
				now.insert_last(Node1._element)
			return now
		else: 
			if Node1._element > Node2._element:
					now = merge_sublists(Node1._next, Node2)
					now.insert_last(Node1._element)
			else:
					now = merge_sublists(Node1, Node2._next)
					now.insert_last(Node._element)
			return now
	return merge_sublists(srt_lnk_lst1._header._next, srt_lnk_lst2._header._next)

lst1 = Doubly_Linked_List()
lst1.insert_last(1)
lst1.insert_last(3)
lst1.insert_last(5)
lst1.insert_last(6)
lst1.insert_last(8)
print(lst1)

lst2 = Doubly_Linked_List()
lst2.insert_last(2)
lst2.insert_last(3)
lst2.insert_last(5)
lst2.insert_last(10)
lst2.insert_last(15)
lst2.insert_last(18)
print(lst2)
예제 #7
0
class CompactString():
    def __init__(self, orig_str=''):
        self._data = Doubly_Linked_List()
        for i in range(len(orig_str)):
            if i == 0:
                self._data.insert_last((orig_str[i], 1))
            if i > 0 and orig_str[i] != orig_str[i - 1]:
                self._data.insert_last((orig_str[i], 1))
            else:
                mylast = self._data.last()
                tup1 = mylast[1]
                self._data._trailer._prev._element = (orig_str[i], tup1 + 1)

    def __add__(self, other):
        result = CompactString()
        if self._data._trailer._prev._element[
                0] == other._data._header._next._element[0]:
            newcount = other._data._header._next._element[
                1] + self._data._trailer._prev._element[1]
            other._data._header._next._element = other._data._header._next._element[
                0], newcount
            self._data.delete_last()
        curr = self._data._header._next
        while curr._element != None:
            result._data.insert_last(curr._element)
            curr = curr._next
        mycurr = other._data._header._next
        while mycurr._element != None:
            result._data.insert_last(mycurr._element)
            mycurr = mycurr._next
        return result

    def __lt__(self, other):
        curr = self._data._header._next
        mycurr = other._data._header._next
        while curr != self._data._trailer and mycurr != other._data._trailer:
            if curr._element[0] == mycurr._element[0]:
                if curr._element[1] == mycurr._element[1]:
                    curr = curr._next
                    mycurr = mycurr._next
                elif curr._element[1] < mycurr._element[1]:
                    if curr._next is not self._data._trailer:
                        return curr._next._element[0] < mycurr._element[0]
                    else:
                        return True
                else:
                    if mycurr._next is not other._data._trailer:
                        return curr._element[0] < mycurr._next._element[0]
                    else:
                        return False
            elif curr._element[0] < mycurr._element[0]:
                return True
            else:
                return False

        if mycurr != other._data._trailer and curr == self._data._trailer:
            return True
        return False

    def __eq__(self, other):
        if not self < other:
            if not self > other:
                return True
        return False

    # curr = self._data._header._next
    # mycurr = other._data._header._next
    # while curr != self._data._trailer:
    # 	if mycurr != self._data._trailer and curr._element == mycurr._element:
    # 		curr = curr._next
    # 		mycurr = mycurr._next
    # if curr == self._data._trailer and mycurr == self._data._trailer:
    # 	return True
    # return False

    def __le__(self, other):
        if self < other:
            return True
        else:
            if self == other:
                return True
        return False

    def __gt__(self, other):
        curr = self._data._header._next
        mycurr = other._data._header._next
        while curr != self._data._trailer and mycurr != other._data._trailer:
            if curr._element[0] == mycurr._element[0]:
                if curr._element[1] == mycurr._element[1]:
                    curr = curr._next
                    mycurr = mycurr._next
                elif mycurr._element[1] < curr._element[1]:
                    if mycurr._next is not other._data._trailer:
                        return curr._element[0] > mycurr._next._element[0]
                    else:
                        return True
                else:
                    if curr._next is not self._data._trailer:
                        return curr._next._element[0] > mycurr._element[0]
                    else:
                        return False
            elif curr._element[0] > mycurr._element[0]:
                return True
            else:
                return False
        if mycurr == other._data._trailer and curr != self._data._trailer:
            return True
        return False

    def __ge__(self, other):
        if self > other:
            return True
        else:
            if self == other:
                return True
        return False

    def __repr__(self):
        repstr = ''
        currNode = self._data._header._next
        while currNode is not self._data._trailer:
            for i in range(currNode._element[1]):
                repstr += currNode._element[0]
            currNode = currNode._next
        return repstr

    s1 = CompactString('aaaaabbbaaac')
    s2 = CompactString('aaaaaaacccaaaa')
    print(s1)
    print(s2)
    s3 = s2 + s1
    print(s3)
    print(s1 > s2)