Skip to content

Commit ea79b38

Browse files
Add files via upload
1 parent c7179f4 commit ea79b38

File tree

3 files changed

+1205
-0
lines changed

3 files changed

+1205
-0
lines changed

a6_include.py

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Course: CS261 - Data Structures
2+
# Assignment: 6
3+
# Description: 'Helper' data structures
4+
5+
6+
class SLNode:
7+
def __init__(self, key: str, value: object) -> None:
8+
"""
9+
Singly Linked List Node class
10+
DO NOT CHANGE THIS CLASS IN ANY WAY
11+
"""
12+
self.next = None
13+
self.key = key
14+
self.value = value
15+
16+
def __str__(self):
17+
""" Return content of the node in human-readable form """
18+
return '(' + str(self.key) + ': ' + str(self.value) + ')'
19+
20+
21+
class LinkedList:
22+
"""
23+
Class implementing a Singly Linked List
24+
Supported methods are: insert, remove, contains, length, iterator
25+
26+
DO NOT CHANGE THIS CLASS IN ANY WAY
27+
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
28+
"""
29+
30+
def __init__(self) -> None:
31+
""" Init new SLL """
32+
self.head = None
33+
self.size = 0
34+
35+
def __str__(self) -> str:
36+
""" Return content of SLL in human-readable form """
37+
content = ''
38+
if self.head is not None:
39+
content = str(self.head)
40+
cur = self.head.next
41+
while cur is not None:
42+
content += ' -> ' + str(cur)
43+
cur = cur.next
44+
return 'SLL [' + content + ']'
45+
46+
def insert(self, key: str, value: object) -> None:
47+
""" Insert new node at the beginning of the list """
48+
new_node = SLNode(key, value)
49+
new_node.next = self.head
50+
self.head = new_node
51+
self.size = self.size + 1
52+
53+
def remove(self, key: str) -> bool:
54+
"""
55+
Remove first node with matching key
56+
Return True is some node was removed, False otherwise
57+
"""
58+
prev, cur = None, self.head
59+
while cur is not None:
60+
if cur.key == key:
61+
if prev:
62+
prev.next = cur.next
63+
else:
64+
self.head = cur.next
65+
self.size -= 1
66+
return True
67+
prev, cur = cur, cur.next
68+
return False
69+
70+
def contains(self, key: str) -> SLNode:
71+
"""
72+
If node with matching key in the list -> return pointer
73+
to that node (SLNode), otherwise return None
74+
"""
75+
cur = self.head
76+
while cur is not None:
77+
if cur.key == key:
78+
return cur
79+
cur = cur.next
80+
return cur
81+
82+
def length(self) -> int:
83+
""" Return the length of the list """
84+
return self.size
85+
86+
def __iter__(self) -> SLNode:
87+
"""
88+
Provides iterator capability for the SLL class
89+
so it can be used in for ... in ... type of loops.
90+
EXAMPLE:
91+
for node in my_list:
92+
print(node.key, node.value)
93+
"""
94+
cur = self.head
95+
while cur is not None:
96+
yield cur
97+
cur = cur.next
98+
99+
100+
class DynamicArrayException(Exception):
101+
pass
102+
103+
104+
class DynamicArray:
105+
"""
106+
Class implementing a Dynamic Array
107+
Supported methods are:
108+
append, pop, swap, get_at_index, set_at_index, length
109+
110+
DO NOT CHANGE THIS CLASS IN ANY WAY
111+
YOU ARE ALLOWED TO CREATE AND USE OBJECTS OF THIS CLASS IN YOUR SOLUTION
112+
"""
113+
114+
def __init__(self, arr=None):
115+
""" Initialize new dynamic array """
116+
self.data = arr.copy() if arr else []
117+
118+
def __iter__(self):
119+
"""
120+
Disable iterator capability for DynamicArray class
121+
This means loops and aggregate functions like
122+
those shown below won't work:
123+
124+
arr = StaticArray()
125+
for value in arr: # will not work
126+
min(arr) # will not work
127+
max(arr) # will not work
128+
sort(arr) # will not work
129+
"""
130+
return None
131+
132+
def __str__(self) -> str:
133+
""" Return content of dynamic array in human-readable form """
134+
return str(self.data)
135+
136+
def append(self, value: object) -> None:
137+
""" Add new element at the end of the array """
138+
self.data.append(value)
139+
140+
def pop(self) -> object:
141+
""" Removes element from end of the array and return it """
142+
return self.data.pop()
143+
144+
def swap(self, i: int, j: int) -> None:
145+
""" Swaps values of two elements given their indicies """
146+
self.data[i], self.data[j] = self.data[j], self.data[i]
147+
148+
def get_at_index(self, index: int) -> object:
149+
""" Return value of element at a given index """
150+
if index < 0 or index >= self.length():
151+
raise DynamicArrayException
152+
return self.data[index]
153+
154+
def __getitem__(self, index: int) -> object:
155+
""" Return value of element at a given index using [] syntax """
156+
return self.get_at_index(index)
157+
158+
def set_at_index(self, index: int, value: object) -> None:
159+
""" Set value of element at a given index """
160+
if index < 0 or index >= self.length():
161+
raise DynamicArrayException
162+
self.data[index] = value
163+
164+
def __setitem__(self, index: int, value: object) -> None:
165+
""" Set value of element at a given index using [] syntax """
166+
self.set_at_index(index, value)
167+
168+
def length(self) -> int:
169+
""" Return the length of the DA """
170+
return len(self.data)

0 commit comments

Comments
 (0)