Skip to content

Commit 3d2ae39

Browse files
committed
finished ring_buffer
1 parent b925124 commit 3d2ae39

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

ring_buffer/ring_buffer.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
# buffer does not grow
2+
# last element is replaced by new element
3+
4+
15
class RingBuffer:
2-
def __init__(self, capacity):
3-
self.capacity = capacity
4-
self.current = 0
5-
self.storage = [None]*capacity
6+
def __init__(self, capacity):
7+
self.capacity = capacity
8+
self.current = 0
9+
self.storage = [None]*capacity
10+
11+
# adds a new element and deletes the last element
12+
def append(self, item):
13+
# print(self.current)
14+
# print(self.storage)
15+
# replaces the item for the "current" index
16+
self.storage[self.current] = item
17+
# check that our current position is not beyond the lists length
18+
if self.current < self.capacity - 1:
19+
# increment our position
20+
self.current += 1
21+
# if we are past the length of the array set to index[0]
22+
else:
23+
self.current = 0
624

7-
def append(self, item):
8-
pass
25+
# returns all of the elements in the buffer
26+
# does not return None values
927

10-
def get(self):
11-
pass
28+
def get(self):
29+
returnArr = []
30+
for i in self.storage:
31+
if i is not None:
32+
returnArr.append(i)
33+
# print(returnArr)
34+
return returnArr

0 commit comments

Comments
 (0)