Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
45
Task/Queue-Definition/Python/queue-definition-1.py
Normal file
45
Task/Queue-Definition/Python/queue-definition-1.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
class FIFO(object):
|
||||
def __init__(self, *args):
|
||||
self.contents = list(args)
|
||||
def __call__(self):
|
||||
return self.pop()
|
||||
def __len__(self):
|
||||
return len(self.contents)
|
||||
def pop(self):
|
||||
return self.contents.pop(0)
|
||||
def push(self, item):
|
||||
self.contents.append(item)
|
||||
def extend(self,*itemlist):
|
||||
self.contents += itemlist
|
||||
def empty(self):
|
||||
return bool(self.contents)
|
||||
def __iter__(self):
|
||||
return self
|
||||
def next(self):
|
||||
if self.empty():
|
||||
raise StopIteration
|
||||
return self.pop()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Sample usage:
|
||||
f = FIFO()
|
||||
f.push(3)
|
||||
f.push(2)
|
||||
f.push(1)
|
||||
while not f.empty():
|
||||
print f.pop(),
|
||||
# >>> 3 2 1
|
||||
# Another simple example gives the same results:
|
||||
f = FIFO(3,2,1)
|
||||
while not f.empty():
|
||||
print f(),
|
||||
# Another using the default "truth" value of the object
|
||||
# (implicitly calls on the length() of the object after
|
||||
# checking for a __nonzero__ method
|
||||
f = FIFO(3,2,1)
|
||||
while f:
|
||||
print f(),
|
||||
# Yet another, using more Pythonic iteration:
|
||||
f = FIFO(3,2,1)
|
||||
for i in f:
|
||||
print i,
|
||||
15
Task/Queue-Definition/Python/queue-definition-2.py
Normal file
15
Task/Queue-Definition/Python/queue-definition-2.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class FIFO: ## NOT a new-style class, must not derive from "object"
|
||||
def __init__(self,*args):
|
||||
self.contents = list(args)
|
||||
def __call__(self):
|
||||
return self.pop()
|
||||
def empty(self):
|
||||
return bool(self.contents)
|
||||
def pop(self):
|
||||
return self.contents.pop(0)
|
||||
def __getattr__(self, attr):
|
||||
return getattr(self.contents,attr)
|
||||
def next(self):
|
||||
if not self:
|
||||
raise StopIteration
|
||||
return self.pop()
|
||||
6
Task/Queue-Definition/Python/queue-definition-3.py
Normal file
6
Task/Queue-Definition/Python/queue-definition-3.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from collections import deque
|
||||
fifo = deque()
|
||||
fifo. appendleft(value) # push
|
||||
value = fifo.pop()
|
||||
not fifo # empty
|
||||
fifo.pop() # raises IndexError when empty
|
||||
Loading…
Add table
Add a link
Reference in a new issue