Skip to content

Commit f3df78c

Browse files
RingBuffer passes test
1 parent f927656 commit f3df78c

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

ring_buffer/ring_buffer.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
class RingBuffer:
22
def __init__(self, capacity):
3-
pass
3+
self.capacity = capacity
4+
self.storage = [None] * capacity # place to store the data concat int
5+
self.current = 0 # len is a tracker
6+
7+
# ds cannot be larger than capacity
48

59
def append(self, item):
6-
pass
10+
# update len
11+
if len(self.storage) < self.capacity: # storage has to be < capacity
12+
self.storage.append(item) # add item to storage
13+
else:
14+
self.storage[self.current] = item # will move to next index
15+
# if storage is > than capacity - start at 0(current)
16+
if self.current < len(self.storage) - 1:
17+
self.current += 1
18+
else:
19+
self.current = 0
720

821
def get(self):
9-
pass
22+
new_items = [] # create a new list
23+
for i in self.storage: # loop through the storage
24+
if i is not None:
25+
new_items.append(i)
26+
return new_items

0 commit comments

Comments
 (0)