Skip to content

Commit

Permalink
Merge pull request #120 from FabioDiBa/python-queue
Browse files Browse the repository at this point in the history
add python queue
  • Loading branch information
ravivarshney01 authored Oct 4, 2018
2 parents e697048 + 7bbbef7 commit 402fe5c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
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()

0 comments on commit 402fe5c

Please sign in to comment.