-
Notifications
You must be signed in to change notification settings - Fork 8
/
cheatsheet-queues.py
79 lines (59 loc) · 2.18 KB
/
cheatsheet-queues.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# ___
# / _ \ _ _ ___ _ _ ___ ___
# | | | | | | |/ _ \ | | |/ _ \/ __|
# | |_| | |_| | __/ |_| | __/\__ \
# \__\_\\__,_|\___|\__,_|\___||___/
#
# https://docs.python.org/2/library/collections.html#collections.deque
print '--queues'
from collections import deque
d = deque('ghi')
for elem in d: # iterate over the deque's elements
print elem.upper()
d.append('j') # add a new entry to the right side
d.appendleft('f') # add a new entry to the left side
print d
d.pop() # return and remove the rightmost item # j
d.popleft() # return and remove the leftmost item # f
print list(d) # list the contents of the deque
print 'g' in d # search the deque
print d
d.extend('jkl') # add multiple elements at once
print d
d.extendleft('abc') # extendleft() reverses the input order
print d
d.rotate(1) # right rotation
print d
d.rotate(-1) # left rotation
print d
print len(d)
a = deque(reversed(d)) # make a new deque in reverse order
print a
del a[0]
print a
# ____ _ _ _ ___
# | _ \ _ __(_) ___ _ __(_) |_ _ _ / _ \ _ _ ___ _ _ ___ ___
# | |_) | '__| |/ _ \| '__| | __| | | | | | | | | | |/ _ \ | | |/ _ \/ __|
# | __/| | | | (_) | | | | |_| |_| | | |_| | |_| | __/ |_| | __/\__ \
# |_| |_| |_|\___/|_| |_|\__|\__, | \__\_\\__,_|\___|\__,_|\___||___/
# |___/
#
#https://docs.python.org/2/library/heapq.html
import heapq
print '\n--heaps'
heap = []
heapq.heappush(heap, 2)
print heap
a = [3, 2, 1]
heapq.heapify(a)
print a
def heapsort(iterable):
h = []
for value in iterable:
heapq.heappush(h, value)
return [heapq.heappop(h) for i in range(len(h))]
b = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
print heapsort(b)
#nlargest(n,iterable) -> list: n largest from iterable
print heapq.nlargest(3, b)
print heapq.nsmallest(3, b)