Des. e imp. de sistemas de SW

Adapter Pattern


During this activity:

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.

Activity Description

This activity must be developed individually.

  1. Create a directory called adapter containing three files:,, and

    Add to the Python source file a comment at the start containing title, date, and the author’s personal information. For example:

    # Adapter Pattern
    # Date: 23-May-2022
    # Authors:
    #          A01777771 Stephen Strange
  2. The content of the file is as follows:

    # File:
    # IMPORTANT: Do not modify the following class in any way!
    class SimpleQueue:
        def __init__(self):
            self._info = []
        def insert(self, x):
            return self
        def remove(self):
            if self.is_empty():
                raise RuntimeError("Can't remove if queue is empty")
                return self._info.pop(0)
        def is_empty(self):
            return self.size() == 0
        def size(self):
            return len(self._info)
        def __repr__(self):
            return repr(self._info)

    The SimpleQueue class contains a First-In First-Out (FIFO) data structure that implements the following methods:

    Method Signature Description
    insert(self, x) Inserts x at the back of this queue. Returns this queue.
    remove(self) Removes and returns the element at the front of this queue. Raises an exception if this queue happens to be empty.
    is_empty(self) Returns True if this queue is empty, otherwise returns False.
    size(self) Returns the number of elements currently stored in this queue.

    Write an adapter class called QueueToStackAdapter (place it in the file called that makes a SimpleQueue instance (as implemented above) behave like a Last-In First-Out (LIFO) stack with the following interface:

    Method Signature Description
    __init__(self, queue) Initializes a new stack, using queue as the adaptee.
    push(self, x) Inserts x at the top of this stack. Returns this stack.
    pop(self) Returns None if this stack is empty, otherwise removes and returns its top element.
    peek(self) Returns None if this stack is empty, otherwise returns its top element without removing it.
    is_empty(self) Returns True if this stack is empty, otherwise returns False.
    size(self) Returns the number of elements currently stored in this stack.

  3. The following unit test verifies the correct behavior of the QueueToStackAdapter class. Place the test class in the source file.
    # File:
    from unittest import TestCase, main
    from simple_queue import SimpleQueue
    from queue_to_stack_adapter import QueueToStackAdapter
    class TestQueueToStackAdapter(TestCase):
        def test_queue_to_stack_adapter(self):
            q = SimpleQueue()
            qsa = QueueToStackAdapter(q)
            self.assertIs(qsa, qsa.push('Foo'))
            self.assertEqual('Foo', qsa.peek())
            self.assertIs(qsa, qsa.push('Bar'))
            self.assertEqual('Bar', qsa.peek())
            self.assertEqual(qsa, qsa.push('Baz').push('Quux'))
            self.assertEqual(4, q.size())
            self.assertEqual(4, qsa.size())
            self.assertEqual('Quux', qsa.peek())
            self.assertEqual('Quux', qsa.pop())
            self.assertEqual('Baz', qsa.peek())
            self.assertEqual('Baz', qsa.pop())
            self.assertEqual('Bar', qsa.peek())
            self.assertEqual('Bar', qsa.pop())
            self.assertEqual('Foo', qsa.peek())
            self.assertIs(qsa, qsa.push('Goo'))
            self.assertEqual('Goo', qsa.peek())
            self.assertEqual('Goo', qsa.pop())
            self.assertEqual('Foo', qsa.peek())
            self.assertEqual(1, qsa.size())
            self.assertEqual('Foo', qsa.pop())
            self.assertEqual(0, q.size())
            self.assertEqual(0, qsa.size())
    if __name__ == '__main__':

    To run the tests, at the terminal move to the adapter directory:

    cd adapter

    Now, run the script with the tests and make sure you pass them all:



Instrucciones para subir archivo

Para entregar el archivo, ingresa los siguientes datos:

Solicitar NIP

Due date is Tuesday, May 24.