mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
...
This commit is contained in:
parent
bc442a0727
commit
c3cb3f1c1b
@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
# Time: 5 min
|
||||||
|
|
||||||
__author__ = "bt3"
|
__author__ = "bt3"
|
||||||
|
|
||||||
@ -8,33 +9,39 @@ class Queue(object):
|
|||||||
self.enq = []
|
self.enq = []
|
||||||
self.deq = []
|
self.deq = []
|
||||||
|
|
||||||
def enqueue(self, value):
|
def enqueue(self, item):
|
||||||
self.enq.append(value)
|
return self.enq.append(item)
|
||||||
|
|
||||||
def dequeue(self):
|
def deque(self):
|
||||||
if not self.deq:
|
if not self.deq:
|
||||||
while self.enq:
|
while self.enq:
|
||||||
self.deq.append(self.enq.pop())
|
self.deq.append(self.enq.pop())
|
||||||
return self.deq.pop()
|
return self.deq.pop()
|
||||||
|
|
||||||
def isEmpty(self):
|
def peak(self):
|
||||||
return not (self.enq + self.deq)
|
if not self.deq:
|
||||||
|
while self.enq:
|
||||||
|
self.deq.append(self.enq.pop())
|
||||||
|
if self.deq:
|
||||||
|
return self.deq[-1]
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
return len(self.enq) + len(self.deq)
|
return len(self.enq) + len(self.deq)
|
||||||
|
|
||||||
|
def isempty(self):
|
||||||
|
return not (self.enq + self.deq)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
q = Queue()
|
q = Queue()
|
||||||
|
for i in range(1,11):
|
||||||
for i in range(1,10):
|
|
||||||
q.enqueue(i)
|
q.enqueue(i)
|
||||||
|
print 'Size:', q.size()
|
||||||
assert(q.isEmpty() == False)
|
print 'Is empty?', q.isempty()
|
||||||
|
print 'Peak: ', q.peak()
|
||||||
assert(q.size() == 9)
|
print
|
||||||
|
print 'Dequeuing...'
|
||||||
for i in range(1, 10):
|
for i in range(10):
|
||||||
print q.dequeue()
|
print q.deque()
|
||||||
|
print 'Size:', q.size()
|
||||||
assert(q.isEmpty() == True)
|
print 'Is empty?', q.isempty()
|
||||||
|
print 'Peak: ', q.peak()
|
@ -11,20 +11,22 @@ def find_long_con_inc(seq):
|
|||||||
[-2, 3, 5]
|
[-2, 3, 5]
|
||||||
>>> find_long_con_inc([1, 3, -2, 3, 5, 6])
|
>>> find_long_con_inc([1, 3, -2, 3, 5, 6])
|
||||||
[-2, 3, 5, 6]
|
[-2, 3, 5, 6]
|
||||||
|
>>> find_long_con_inc([1, 3, 4, -13, 2, 5, 8, -1, 2,-17])
|
||||||
|
[-13, 2, 5, 8]
|
||||||
'''
|
'''
|
||||||
|
|
||||||
aux = []
|
res, aux = [], []
|
||||||
result = []
|
|
||||||
seq.append(-float('infinity'))
|
seq.append(-float('infinity'))
|
||||||
|
|
||||||
for i, pivot in enumerate(seq[:-1]):
|
for i, n in enumerate(seq[:-1]):
|
||||||
aux.append(pivot)
|
aux.append(n)
|
||||||
if pivot > seq[i+1]:
|
if n > seq[i+1]:
|
||||||
if len(aux) > len(result):
|
if len(res) < len(aux):
|
||||||
result = aux
|
res = aux[:]
|
||||||
aux = []
|
aux = []
|
||||||
|
|
||||||
return result
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user