def stack_of_plates():
    plates = [23, 67, 323, 976, 213, 6, 78, 85]

    stack = Stack()
    for x in plates:
        stack.push(x)

    print(stack)
    print(stack.length())

    threshold = 2

    set_of_stacks = SetOfStacks(threshold)
    for x in plates:
        set_of_stacks.push(x)

    print(set_of_stacks)

    set_of_stacks.push(100)
    print(set_of_stacks)

    set_of_stacks.push(200)
    print(set_of_stacks)

    set_of_stacks.pop()
    print(set_of_stacks)

    set_of_stacks.pop_at(0)
    print(set_of_stacks)
Exemple #2
0
 def test_pushMultiple_ComeOutFILO(self):
     stack = Stack()
     stack.push(2)
     stack.push(1)
     out1 = stack.pop()
     out2 = stack.pop()
     self.assertEqual(1, out1)
     self.assertEqual(2, out2)
    def push(self, data):
        if len(self.__stack_list__) == 0:
            self.__stack_list__.append(Stack())
        else:
            last = self.__stack_list__[-1]
            if last.length() >= self.__threshold__:
                self.__stack_list__.append(Stack())

        self.__stack_list__[-1].push(data)
Exemple #4
0
 def push(self, item):
     # Keep stacks and sizes lists of the same length
     if len(self.sizes) == 0 or self.sizes[-1] == self.capacity:
         self.stacks.append(Stack())
         self.sizes.append(1)
     else:
         self.sizes[-1] += 1
     (self.stacks[-1]).push(item)
Exemple #5
0
class QueueViaStack(object):
    '''

    Time :
    Space:
    Note :
    '''
    def __init__(self):
        self.input = Stack()
        self.output = Stack()

    def push(self, data):
        self.input.push(data)

    def pop(self):
        if self.input.empty() and self.output.empty():
            return None
        
        curr = self.input.pop()
        while curr != None:
            self.output.push(curr.data)
            curr = self.input.pop()
        
        return self.output.pop()
Exemple #6
0
def sort_stack(s):
    assert not s.is_empty()
    ss = Stack()  # This will be the auxiliary stack

    num_elements = 0
    while not s.is_empty():
        ss.push(s.pop())
        num_elements += 1

    num_sorted = 0
    while num_sorted < num_elements:
        # `num_sorted` is how much `s` is filled.
        # `ss` has the `num_elements-num_sorted` elements.
        min_item = np.float('inf')

        # Search through all non-sorted items to find minimum.
        for _ in range(num_elements - num_sorted):
            item = ss.pop()
            if item < min_item:
                min_item = item
            s.push(item)

        # Put all unsorted items other than the one with min back
        # into ss, if we get the min hold it out and put it in s.
        got_min = False  # Handle duplicates

        for _ in range(num_elements - num_sorted):
            item = s.pop()
            if item == min_item and not got_min:
                got_min = True
            else:
                ss.push(item)
        s.push(min_item)
        num_sorted += 1

    return s
 def __init__(self):
     self.s1 = Stack()
     self.s2 = Stack()
class MyQueue:
    """ For Question 5. """

    def __init__(self):
        self.s1 = Stack()
        self.s2 = Stack()

    def enqueue(self, item):
        """ Add to end. """
        self.s1.push(item)

    def dequeue(self):
        """ Remove from front. """
        if self.s1.is_empty():
            return None
        while (not self.s1.is_empty()):
            self.s2.push( self.s1.pop() )
        result = self.s2.pop()
        while (not self.s2.is_empty()):
            self.s1.push( self.s2.pop() )       
        return result
Exemple #9
0
 def test_canPushAndPop(self):
     stack = Stack()
     stack.push(1)
     out = stack.pop()
     self.assertEqual(1, out)
Exemple #10
0
 def __init__(self):
     self.s1 = Stack()  # Use for dequeues
     self.s2 = Stack()  # Use for reverse ordering
Exemple #11
0
class MyQueue:
    def __init__(self):
        self.s1 = Stack()  # Use for dequeues
        self.s2 = Stack()  # Use for reverse ordering

    def enqueue(self, item):
        #while not self.s1.is_empty():
        while self.s1.peek() is not None:
            self.s2.push(self.s1.pop())
        self.s2.push(item)
        #while not self.s2.is_empty():
        while self.s2.peek() is not None:
            self.s1.push(self.s2.pop())

    def dequeue(self):
        return self.s1.pop()

    def __str__(self):
        return "s1: {}\ns2: {}".format(self.s1.__str__(), self.s2.__str__())
Exemple #12
0
 def __init__(self):
     self.input = Stack()
     self.output = Stack()
Exemple #13
0
        for _ in range(num_elements - num_sorted):
            item = ss.pop()
            if item < min_item:
                min_item = item
            s.push(item)

        # Put all unsorted items other than the one with min back
        # into ss, if we get the min hold it out and put it in s.
        got_min = False  # Handle duplicates

        for _ in range(num_elements - num_sorted):
            item = s.pop()
            if item == min_item and not got_min:
                got_min = True
            else:
                ss.push(item)
        s.push(min_item)
        num_sorted += 1

    return s


if __name__ == "__main__":
    lists = [[1, 2, 3], [3, 2, 1], [2, 5, 4, 6, 7, 3, 4, 9, 1, 1, 0, -1, 44]]
    for l in lists:
        s = Stack()
        for item in l:
            s.push(item)
        print("\nSorting: {}".format(s))
        print("Result:  {}".format(sort_stack(s)))