Created
July 6, 2019 05:18
-
-
Save eyaltrabelsi/04b5b690783d6667d6ac33050b483425 to your computer and use it in GitHub Desktop.
Python trick using Double-ended queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> import collection | |
>>> Q = collections.deque() | |
>>> Q.append(1) | |
>>> Q.appendleft(2) | |
>>> Q.extend([3, 4]) | |
>>> Q.extendleft([5, 6]) | |
>>> Q | |
deque([6, 5, 2, 1, 3, 4]) | |
>>> Q.pop() | |
4 | |
>>> Q.popleft() | |
6 | |
>>> Q | |
deque([5, 2, 1, 3]) | |
>>> Q.rotate(3) | |
>>> Q | |
deque([2, 1, 3, 5]) | |
>>> Q.rotate(-3) | |
>>> Q | |
deque([5, 2, 1, 3]) | |
>>> last_three = collections.deque(maxlen=3) | |
>>> for i in range(4): | |
... last_three.append(i) | |
... print ', '.join(str(x) for x in last_three) | |
... | |
0 | |
0, 1 | |
0, 1, 2 | |
1, 2, 3 | |
2, 3, 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment