-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr-6.14.py
88 lines (71 loc) · 1.68 KB
/
r-6.14.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
80
81
82
83
84
85
86
87
88
"""
R-6.14 Repeat the previous problem using the deque D and an initially empty
stack S.
prev problem:
R-6.13 Suppose you have a deque D containing the numbers (1,2,3,4,5,6,7,8),
in this order. Suppose further that you have an initially empty queue Q.
Give a code fragment that uses only D and Q (and no other variables) and
results in D storing the elements in the order (1,2,3,5,4,6,7,8).
"""
from collections import deque
d = deque([1, 2, 3, 4, 5, 6, 7, 8])
s = []
print(d)
# s: 1, 2, 3, 8, 7, 6, 4, 5 # might be shorter if pushed: 8, 7, 6, 1, 2, 3, 5, 4 test out
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.pop())
s.append(d.pop())
s.append(d.pop())
s.append(d.popleft())
s.append(d.popleft())
print(f's:{s}')
# d: 5, 4, 6, 7, 8, 3, 2, 1
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
print(d)
# s: 1, 2, 3, 5, 4, 6, 7, 8
s.append(d.pop())
s.append(d.pop())
s.append(d.pop())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
print(f's:{s}')
# d: 8, 7, 6, 4, 5, 3, 2, 1
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
print(d)
# s: 8, 7, 6, 4, 5, 3, 2, 1
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
s.append(d.popleft())
# d: 1, 2, 3, 5, 4, 6, 7, 8
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
d.append(s.pop())
print(d)
assert list(d) == [1, 2, 3, 5, 4, 6, 7, 8]