File tree Expand file tree Collapse file tree 1 file changed +31
-8
lines changed Expand file tree Collapse file tree 1 file changed +31
-8
lines changed Original file line number Diff line number Diff line change 1+ # buffer does not grow
2+ # last element is replaced by new element
3+
4+
15class 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
You can’t perform that action at this time.
0 commit comments