-
Notifications
You must be signed in to change notification settings - Fork 0
/
adts.py
131 lines (104 loc) · 3.8 KB
/
adts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
One implementation each of Stack and Queue.
This code is provided solely for the personal and private use of students
taking the CSC148 course at the University of Toronto. Copying for purposes
other than this use is expressly prohibited. All forms of distribution of
this code, whether as given or with any changes, are expressly prohibited.
This file is:
Copyright (c) 2021 Diane Horton, Jonathan Calver, Sophia Huynh, Maryam Majedi,
and Jaisie Sin.
===== Module Description =====
This module contains list-based implementations of the Stack and Queue
ADTs. You are NOT required to use either of these in this assignment,
but we include them for those who wish to do so.
"""
from typing import List, Optional, Any
###############################################################################
# Stacks
###############################################################################
class Stack:
"""A last-in-first-out (LIFO) stack of items.
Stores data in a last-in, first-out order. When removing an item from the
stack, the most recently-added item is the one that is removed.
"""
# === Private Attributes ===
# _items:
# The items stored in this stack. The end of the list represents
# the top of the stack.
_items: List
def __init__(self) -> None:
"""Initialize a new empty stack."""
self._items = []
def is_empty(self) -> bool:
"""Return whether this stack contains no items.
>>> s = Stack()
>>> s.is_empty()
True
>>> s.push('hello')
>>> s.is_empty()
False
"""
return self._items == []
def push(self, item: Any) -> None:
"""Add a new element to the top of this stack."""
self._items.append(item)
def pop(self) -> Any:
"""Remove and return the element at the top of this stack.
Raise an EmptyStackError if this stack is empty.
>>> s = Stack()
>>> s.push('hello')
>>> s.push('goodbye')
>>> s.pop()
'goodbye'
"""
if self.is_empty():
raise EmptyStackError
else:
return self._items.pop()
class EmptyStackError(Exception):
"""Exception raised when calling pop on an empty stack."""
def __str__(self) -> str:
"""Return a string representation of this error."""
return 'You called pop on an empty stack.'
###############################################################################
# Queues
###############################################################################
class Queue:
"""A first-in-first-out (FIFO) queue of items.
Stores data in a first-in, first-out order. When removing an item from the
queue, the most recently-added item is the one that is removed.
"""
# === Private attributes ===
# _items: a list of the items in this queue
_items: List
def __init__(self) -> None:
"""Initialize a new empty queue."""
self._items = []
def is_empty(self) -> bool:
"""Return whether this queue contains no items.
>>> q = Queue()
>>> q.is_empty()
True
>>> q.enqueue('hello')
>>> q.is_empty()
False
"""
return self._items == []
def enqueue(self, item: Any) -> None:
"""Add <item> to the back of this queue.
"""
self._items.append(item)
def dequeue(self) -> Optional[Any]:
"""Remove and return the item at the front of this queue.
Return None if this Queue is empty.
(We illustrate a different mechanism for handling an erroneous case.)
>>> q = Queue()
>>> q.enqueue('hello')
>>> q.enqueue('goodbye')
>>> q.dequeue()
'hello'
"""
if self.is_empty():
return None
else:
return self._items.pop(0)