Skip to content

Commit 2635bb5

Browse files
committed
Part1
1 parent 045b6d7 commit 2635bb5

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

reverse/reverse.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ def contains(self, value):
4747

4848
def reverse_list(self, node, prev):
4949
# You must use recursion for this solution
50-
pass
50+
if node is None:
51+
return None
52+
if node.next_node is None:
53+
self.head = node
54+
self.head.next_node = prev
55+
return None
56+
else:
57+
self.reverse_list(node.next_node, node)
58+
node.next_node = prev

ring_buffer/ring_buffer.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,40 @@ def __init__(self, capacity):
88
self.storage = DoublyLinkedList()
99

1010
def append(self, item):
11-
pass
11+
if len(self.storage) < self.capacity:
12+
self.storage.add_to_tail(item)
13+
self.current = self.storage.tail
14+
if len(self.storage) == self.capacity:
15+
self.current.value = item
16+
if self.current is self.storage.tail:
17+
self.current = self.storage.head
18+
else:
19+
self.current = self.current.next
1220

1321
def get(self):
1422
# Note: This is the only [] allowed
1523
list_buffer_contents = []
1624

1725
# TODO: Your code here
18-
26+
current = self.storage.head
27+
while current:
28+
list_buffer_contents.append(current.value)
29+
current = current.next
1930
return list_buffer_contents
2031

2132
# ----------------Stretch Goal-------------------
2233

2334

2435
class ArrayRingBuffer:
2536
def __init__(self, capacity):
26-
pass
37+
self.capacity = capacity
38+
self.current = 0
39+
self.storage = [None] * capacity
2740

2841
def append(self, item):
29-
pass
42+
self.current = self.current % self.capacity
43+
self.storage[self.current] = item
44+
self.current += 1
3045

3146
def get(self):
32-
pass
47+
return [i for i in self.storage if i != None]

0 commit comments

Comments
 (0)