Reto
Código
Stack con nodos
from typing import Optional, Union
from typing import Iterable
from node import Node
class Stack:
"""A simple stack implementation."""
_size: int
_top: Optional[Node]
def __init__(self, *items):
"""Initializes the stack."""
self._top = None
self._size = 0
if len(items) > 0 and isinstance(items[0], Iterable):
items = items[0]
for item in items:
self.push(item)
def is_empty(self):
"""Checks if the stack is empty.
Returns:
bool: True if the stack is empty, False otherwise.
"""
return self._size <= 0
def clear(self):
"""Clears the stack."""
self._top = None
self._size = 0
def copy(self):
"""Returns a copy of the stack.
Caution: This method returns a shallow copy.
Returns:
Stack: A copy of the stack.
"""
new_stack = Stack()
new_stack._top = self._top
return new_stack
def depthcopy(self):
"""Returns a deep copy of the stack.
Returns:
Stack: A deep copy of the stack.
"""
new_stack = Stack()
new_stack._top = self._top.depthcopy()
new_stack._size = self._size
return new_stack
def extend(self, other: 'Stack'):
"""Extends the current stack with the items of the other stack.
Time complexity: O(n) where n is the length of the other stack.
Args:
other: The other stack to extend the current stack with.
"""
if other._top is None:
return None
bottom_node = other._top
while bottom_node.next is not None:
bottom_node = bottom_node.next
bottom_node.next = self._top
self._top = other._top
self._size += other._size
return None
def peek(self, raw: bool = False) -> Union[None, Node, object]:
"""Returns the top item of the stack.
Raises IndexError if the stack is empty.
Args:
raw: If True returns the Node, otherwise it will return the node value.
Returns:
object: The top item of the stack.
"""
if not self._top:
raise IndexError('The stack is empty.')
if raw:
return self._top
return self._top.value
def push(self, value: object):
"""Pushes an item onto the stack.
Time complexity: O(1)
Args:
item: The item to be pushed onto the stack.
"""
self._top = Node(value, self._top)
self._size += 1
def pop(self, raw: bool = False):
"""Pops an item off the stack.
Raises IndexError if the stack is empty.
Time complexity: O(1)
Args:
raw: If True returns the Node, otherwise it will return the node value.
Returns:
object: The item that was popped off the stack.
"""
if self._top is None:
raise IndexError('The stack is empty.')
temp = self._top
self._top = temp.next
self._size -= 1
if raw:
return temp
return temp.value
def __len__(self):
"""Returns the length of the stack.
Time complexity: O(1)
Returns:
int: The length of the stack.
"""
return self._size
def __str__(self) -> str:
"""Returns a string representation of the stack.
Time complexity: O(n)
Space complexity: O(n)
Returns:
str: A string representation of the stack.
"""
items = list(self)
return str(items)
def __contains__(self, item: object) -> bool:
"""Checks if the stack contains an item.
Args:
item: The item to check if the stack contains.
Returns:
bool: True if the stack contains the item, False otherwise.
"""
pointer = self._top
while pointer is not None:
if pointer.value == item:
return True
pointer = pointer.next
return False
def itervalues(self) -> Iterable:
"""Returns an iterator over the values of the stack.
Returns:
iterator: An iterator over the values of the stack.
"""
pointer = self._top
while pointer is not None:
yield pointer
pointer = pointer.next
def __iter__(self) -> Iterable:
"""Returns an iterator over the stack.
Returns:
iterator: An iterator over the stack.
"""
return self.itervalues()
def __add__(self, other):
"""Returns a new stack containing the items of both stacks.
Args:
other: The other stack to add to the current stack.
Returns:
Stack: A new stack containing the items of both stacks.
"""
new_stack = self.depthcopy()
new_stack.extend(other.depthcopy())
return new_stack
Stack con listas
from typing import Optional, Union
from typing import Iterable
class Stack:
"""A simple stack implementation with list."""
_items: list
def __init__(self, *items):
self._items = list(*items)
def is_empty(self) -> bool:
return len(self._items) == 0
def clear(self) -> None:
self._items.clear()
def copy(self) -> 'Stack':
stack = Stack()
for item in self._items:
stack.push(item)
return stack
def extend(self, other: Iterable) -> None:
for item in reversed(other):
self.push(item)
def peek(self) -> Optional[object]:
if self.is_empty():
raise ValueError('Stack is empty.')
return self._items[-1]
def pop(self) -> Optional[object]:
return self._items.pop()
def push(self, item: object) -> None:
self._items.append(item)
def itervalues(self) -> Iterable:
return reversed(self._items)
def __len__(self) -> int:
return len(self._items)
def __str__(self) -> str:
return str(self._items)
def __contains__(self, item: object) -> bool:
return item in self._items
def __iter__(self) -> Iterable:
return self.itervalues()
def __repr__(self) -> str:
return f'Stack({self._items})'
def __reversed__(self) -> Iterable:
return self._items
def __add__(self, other: Union['Stack', Iterable]) -> 'Stack':
stack = self.copy()
if isinstance(other, Stack):
stack.extend(other)
else:
stack.extend(other)
return stack
Pruebas unitarias
Stack con nodos
# Unitest
import unittest
# Utils
from stack_with_node import Stack
class StackWithNodeTestCase(unittest.TestCase):
"""Stack test cases."""
def test_empty_instantiation(self):
"""Test instantiation of an empty stack."""
stack = Stack()
self.assertEqual(len(stack), 0)
def test_instantiation_with_items(self):
"""Test instantiation of a stack with items."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(len(stack), 3)
def test_is_empty(self):
"""Test is_empty method."""
stack = Stack()
self.assertTrue(stack.is_empty())
def test_is_not_empty(self):
"""Test is_empty method."""
stack = Stack(['a', 'b', 'c'])
self.assertFalse(stack.is_empty())
def test_push(self):
"""Test push method."""
stack = Stack()
stack.push('a')
self.assertEqual(len(stack), 1)
def test_peek(self):
"""Test peek method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(stack.peek(), 'c')
def test_pop(self):
"""Test pop method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(stack.pop(), 'c')
self.assertEqual(len(stack), 2)
for a, b in zip(stack, ['b', 'a']):
self.assertEqual(a, b)
def test_pop_empty(self):
"""Test pop method."""
stack = Stack()
self.assertRaises(IndexError, stack.pop)
def test_copy(self):
"""Test copy method."""
stack = Stack(['a', 'b', 'c'])
stack_copy = stack.copy()
self.assertIsNot(stack_copy, stack)
def test_depthcopy(self):
"""test depthcopy method."""
stack_1 = Stack([1, 2, 3])
stack_2 = stack_1.depthcopy()
self.assertEqual(len(stack_2), len(stack_1))
for a, b in zip(stack_1, stack_2):
self.assertIsNot(a, b)
def test_iter(self):
"""Test iter method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(list(stack), ['c', 'b', 'a'])
def test_extend(self):
"""Test extends method."""
stack_1 = Stack([1, 2, 3])
stack_2 = Stack([4, 5, 6])
stack_1.extend(stack_2)
self.assertEqual(len(stack_1), 6)
for a, b in zip(stack_1, [6, 5, 4, 3, 2, 1]):
self.assertEqual(a, b)
def test_add_operator(self):
"""Test add operator."""
stack_1 = Stack([1, 2, 3])
stack_2 = Stack([4, 5, 6])
stack_3 = stack_1 + stack_2
self.assertEqual(len(stack_3), 6)
for a, b in zip(stack_3, [6, 5, 4, 3, 2, 1]):
self.assertEqual(a, b)
Stack con listas
# Unitest
import unittest
# Utils
from stack_with_array import Stack
class StackWithNodeTestCase(unittest.TestCase):
"""Stack test cases."""
def test_empty_instantiation(self):
"""Test instantiation of an empty stack."""
stack = Stack()
self.assertEqual(len(stack), 0)
def test_instantiation_with_items(self):
"""Test instantiation of a stack with items."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(len(stack), 3)
def test_is_empty(self):
"""Test is_empty method."""
stack = Stack()
self.assertTrue(stack.is_empty())
def test_is_not_empty(self):
"""Test is_empty method."""
stack = Stack(['a', 'b', 'c'])
self.assertFalse(stack.is_empty())
def test_push(self):
"""Test push method."""
stack = Stack()
stack.push('a')
self.assertEqual(len(stack), 1)
def test_push_multiple(self):
"""Test push method."""
stack = Stack()
stack.push('a')
stack.push('b')
stack.push('c')
self.assertEqual(len(stack), 3)
for a, b in zip(stack, ['c', 'b', 'a']):
self.assertEqual(a, b)
def test_peek(self):
"""Test peek method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(stack.peek(), 'c')
def test_pop(self):
"""Test pop method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(stack.pop(), 'c')
self.assertEqual(len(stack), 2)
for a, b in zip(stack, ['b', 'a']):
self.assertEqual(a, b)
def test_pop_empty(self):
"""Test pop method."""
stack = Stack()
self.assertRaises(IndexError, stack.pop)
def test_copy(self):
"""Test copy method."""
stack = Stack(['a', 'b', 'c'])
stack_copy = stack.copy()
self.assertIsNot(stack_copy, stack)
def test_iter(self):
"""Test iter method."""
stack = Stack(['a', 'b', 'c'])
self.assertEqual(list(stack), ['c', 'b', 'a'])
def test_extend(self):
"""Test extends method."""
stack_1 = Stack([1, 2, 3])
stack_2 = Stack([4, 5, 6])
stack_1.extend(stack_2)
self.assertEqual(len(stack_1), 6)
for a, b in zip(stack_1, [6, 5, 4, 3, 2, 1]):
self.assertEqual(a, b)
def test_add_operator(self):
"""Test add operator."""
stack_1 = Stack([1, 2, 3])
stack_2 = Stack([4, 5, 6])
stack_3 = stack_1 + stack_2
self.assertEqual(len(stack_3), 6)
for a, b in zip(stack_3, [6, 5, 4, 3, 2, 1]):
self.assertEqual(a, b)