Skip to content

Instantly share code, notes, and snippets.

@edwintcloud
Last active May 7, 2019 17:54
Show Gist options
  • Save edwintcloud/c63e2dedb6d192b37b671460a1fea86b to your computer and use it in GitHub Desktop.
Save edwintcloud/c63e2dedb6d192b37b671460a1fea86b to your computer and use it in GitHub Desktop.
Circular Buffer in Python Part 2
def size(self):
"""Return the size of the CircularBuffer
Runtime: O(1) Space: O(1)"""
if self.tail >= self.head:
return self.tail - self.head
return self.max_size - self.head - self.tail
def is_empty(self):
"""Return True if the head of the CircularBuffer is equal to the tail,
otherwise return False
Runtime: O(1) Space: O(1)"""
return self.tail == self.head
def is_full(self):
"""Return True if the tail of the CircularBuffer is one before the head,
otherwise return False
Runtime: O(1) Space: O(1)"""
return self.tail == (self.head-1) % self.max_size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment