Some "hello world"-style data structure objects. Helps with inane tech interview questions and/or teaching computer science - whichever comes first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

34 lines
764 B

from collections import deque
import random
# Populate empty deck with integers 1 to 10
d = deque()
n = 10
for i in range(n):
d.append(i+1)
print("Initial deque:")
print(d)
# Note you can also rotate the dequeue:
d.rotate(2)
d.rotate(-2)
# Pop from the list,
# moving from the front of the list (right) to the back of the list (left).
# If a random event happens, we switch directions and pop from the back (left).
print("Popping:")
move_forward = True
while d:
if(move_forward):
nxt = d.pop()
print("Moving forward: %s"%(nxt))
else:
nxt = d.popleft()
print("Moving backward: %s"%(nxt))
# 70% chance that we continue moving forward
if( random.random() < 0.7 ):
move_forward = not move_forward