Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python queue #120

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions data structures/queue/python/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Queue:

def __init__(self):
self.list = []

def enqueue(self, element):
self.list.append(element)

def dequeue(self):
assert len(self.list) > 0; "Queue is empty"
return self.list.pop(0)

def isEmpty(self):
return len(self.list) == 0

32 changes: 32 additions & 0 deletions data structures/queue/python/queue_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
import queue

class QueueTest(unittest.TestCase):

def setUp(self):
self.queue = queue.Queue()

def test_enqueue(self):
self.queue.enqueue(23)
self.assertFalse(self.queue.isEmpty())

def test_empty(self):
self.assertTrue(self.queue.isEmpty())
self.queue.enqueue(42)
self.assertFalse(self.queue.isEmpty())

def test_error_when_queue_empty(self):
with self.assertRaises(AssertionError):
self.queue.dequeue()

def test_multiple_enqueue_dequeue(self):
elements = ['hi', 1, 'hu']

for element in elements:
self.queue.enqueue(element)

for element in elements:
self.assertEqual(self.queue.dequeue(), element)

if __name__ == "__main__":
unittest.main()