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):
print("Initial deque:")
# Note you can also rotate the dequeue:
# 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).
move_forward = True
while d:
nxt = d.pop()
print("Moving forward: %s"%(nxt))
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