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.
This activity must be developed individually.
Create a directory called adapter
containing three files: simple_queue.py
, queue_to_stack_adapter.py
, and adapter_test.py
.
Add to the queue_to_stack_adapter.py
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
The content of the file simple_queue.py
is as follows:
# File: simple_queue.py # IMPORTANT: Do not modify the following class in any way! class SimpleQueue: def __init__(self): self._info = [] def insert(self, x): self._info.append(x) return self def remove(self): if self.is_empty(): raise RuntimeError("Can't remove if queue is empty") else: 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 queue_to_stack_adapter.py
) 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. |
QueueToStackAdapter
class. Place the test class in the adapter_test.py
source file.
# File: adapter_test.py 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.assertTrue(q.is_empty()) self.assertTrue(qsa.is_empty()) self.assertIsNone(qsa.pop()) self.assertIs(qsa, qsa.push('Foo')) self.assertEqual('Foo', qsa.peek()) self.assertFalse(q.is_empty()) self.assertFalse(qsa.is_empty()) 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.assertIsNone(qsa.peek()) self.assertIsNone(qsa.pop()) self.assertTrue(q.is_empty()) self.assertTrue(qsa.is_empty()) self.assertEqual(0, q.size()) self.assertEqual(0, qsa.size()) if __name__ == '__main__': 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:
python3 adapter_test.py
Para entregar el archivo queue_to_stack_adapter.py
, ingresa los siguientes datos:
Due date is Tuesday, May 24.