Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Solve ring buffer problem
  • Loading branch information
myfineprint committed Oct 18, 2019
commit 0b71a4f4f8129d481f9b17b9f0ab26bb7bd10cd5
32 changes: 24 additions & 8 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
class RingBuffer:
def __init__(self, capacity):
self.capacity = capacity
self.current = 0
self.storage = [None]*capacity
def __init__(self, capacity):
self.capacity = capacity
self.current = 0
self.storage = [None] * capacity

def append(self, item):
pass
def append(self, item):
if len(self.storage) >= self.capacity:
if self.current >= len(self.storage):
self.current = 0
self.storage[self.current] = item
self.current += 1
else:
self.storage[self.current] = item
self.current += 1
else:
self.storage.append(item)
self.current += 1

def get(self):
pass
def get(self):
if self.storage.count(None) == self.capacity:
return self.storage
if self.storage.count(None) == 0:
return self.storage
if self.storage.count(None) < self.capacity:
self.storage.remove(None)
return self.storage