Skip to content

Commit 324b623

Browse files
committed
Made PEP8 compatible
1 parent 9b0adf9 commit 324b623

File tree

1 file changed

+32
-49
lines changed

1 file changed

+32
-49
lines changed

pygorithm/data_structures/queue.py

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33

44
# queue implementation
55
class Queue(object):
6-
'''
7-
8-
size : return the current size of the queue
9-
enqueue : insert an item into the queue
10-
dequeue : remove an item from the queue which was first inserted
11-
isEmpty : check if the queue is empty
12-
13-
'''
14-
156
def __init__(self, limit = 10):
167
'''
178
@param: limit: queue size
@@ -22,16 +13,19 @@ def __init__(self, limit = 10):
2213
self.limit = limit
2314
self.size = 0
2415

25-
# for printing the contents of the queue
2616
def __str__(self):
2717
return ' '.join([str(i) for i in self.queue])
2818

29-
# to check if queue is empty
30-
def isEmpty(self):
19+
def size(self):
20+
''' returns the current size of the queue '''
21+
return self.size
22+
23+
def is_empty(self):
24+
''' checks if the queue is empty '''
3125
return self.size <= 0
3226

33-
# to add an element from the rear end of the queue
3427
def enqueue(self, data):
28+
''' inserts an item into the queue '''
3529
if self.size >= self.limit:
3630
return -1 # queue overflow
3731
else:
@@ -45,9 +39,9 @@ def enqueue(self, data):
4539

4640
self.size += 1
4741

48-
# to pop an element from the front end of the queue
4942
def dequeue(self):
50-
if self.isEmpty():
43+
''' pops an item from the queue which was first inserted '''
44+
if self.is_empty():
5145
return -1 # queue underflow
5246
else:
5347
self.size -= 1
@@ -57,66 +51,55 @@ def dequeue(self):
5751
self.rear = self.size - 1
5852
return self.queue.pop(0)
5953

60-
# return the size of the queue
61-
def size(self):
62-
return self.size
63-
64-
# easily retrieve the source code of the Queue class
6554
def get_code(self):
6655
import inspect
6756
return inspect.getsource(Queue)
6857

6958
class Deque(object):
70-
'''
71-
72-
isEmpty : checks whether the deque is empty
73-
isFull : checks whether the deque is full
74-
insertRear : inserts an element at the rear end of the deque
75-
insertFront : inserts an element at the front end of the deque
76-
deleteRear : deletes an element from the rear end of the deque
77-
deleteFront : deletes an element from the front end of the deque
78-
79-
'''
80-
8159
def __init__(self, limit = 10):
8260
self.queue = []
8361
self.limit = limit
8462

8563
def __str__(self):
8664
return ' '.join([str(i) for i in self.queue])
8765

88-
# check if queue is empty
89-
def isEmpty(self):
66+
def is_empty(self):
67+
''' checks whether the deque is empty '''
9068
return len(self.queue) <= 0
9169

92-
# check if queue is full
93-
def isFull(self):
70+
def is_full(self):
71+
''' checks whether the deque is full '''
9472
return len(self.queue) >= self.limit
9573

96-
# for inserting at rear
97-
def insertRear(self, data):
98-
if self.isFull():
74+
def insert_rear(self, data):
75+
''' inserts an element at the rear end of the deque '''
76+
if self.is_full():
9977
return
10078
else:
10179
self.queue.insert(0, data)
10280

103-
# for inserting at front end
104-
def insertFront(self, data):
105-
if self.isFull():
106-
return
81+
def insert_front(self, data):
82+
''' inserts an element at the front end of the deque '''
83+
if self.is_full():
84+
return -1
10785
else:
10886
self.queue.append(data)
10987

110-
# deleting from rear end
111-
def deleteRear(self):
112-
if self.isEmpty():
113-
return
88+
def delete_rear(self):
89+
''' deletes an element from the rear end of the deque '''
90+
if self.is_empty():
91+
return -1
11492
else:
11593
return self.queue.pop(0)
11694

117-
# deleting from front end
118-
def deleteFront(self):
119-
if self.isFull():
95+
def delete_front(self):
96+
''' deletes an element from the front end of the deque '''
97+
if self.is_full():
12098
return
12199
else:
122100
return self.queue.pop()
101+
102+
def get_code(self):
103+
''' returns the code of the current class '''
104+
import inspect
105+
return inspect.getsource(Deque)

0 commit comments

Comments
 (0)