Deques of Python Stories

I was introducing deques in my Computer Science 1 class today in Python and decided to tell a story around the lesson. I figured I would share as this seemed to sink-in with my classes.

Zara loves Computer Science and using technology. She is hosting a party with several of her friends and wants to have their names displayed on the screen in the order they arrived and then remove them when they depart.

The friends of Zara are:

  • Alice
  • Betty
  • Christy
  • Darla
  • Elizabeth
  • Fran
  • Ginger
  • Holly

Alice, Betty and Christy arrive at the party at the same time and enter in that order.

from collections import deque
myGuestList = deque(["Alice", "Betty", "Christy"])
print(myGuestList)

At this point, we have 3 guests at the party: Alice in index 0, Betty in index 1, and Christy in index 2.

Darla and Elizabeth arrive at the party at the same time and enter in that order.

myGuestList.append("Darla")
myGuestList.append("Elizabeth")
print(myGuestList)

We now have 5 guests at the party: Alice in index 0, Betty in index 1, Christy in index 2, Darla in index 3, and Elizabeth in index 4.

Alice and Betty H-A-T-E Elizabeth. Almost instantly upon seeing her name on the screen, they both leave the party.

myGuestList.popleft()
myGuestList.popleft()
print(myGuestList)

We now have 3 guests at the party: Christy has now moved to index 0, Darla has moved to index 1, and Elizabeth has moved to index 2.

Ginger and Holly arrive at the party at the same time and enter in that order.

myGuestList.append("Ginger")
myGuestList.append("Holly")
print(myGuestList)

We now have 5 guests at the party: Christy is in index 0, Darla is in index 1, Elizabeth is in index 2, Ginger is in index 3, and Holly is in index 4.

Regretfully, Holly had been to another party before coming to Zara’s party and gets sick when she gets inside and has to leave.

myGuestList.pop()
print(myGuestList)

We now have 4 guests at the party: Christy is in index 0, Darla is in index 1, Elizabeth is in index 2, and Ginger is in index 3.

Elizabeth is Holly’s sister and leaves to take care of her sister and possibly ridicule her for her choice of the day.

del myGuestList[2]
print(myGuestList)

We now have 3 guests at the party: Christy is in index 0, Darla is in index 1, and Ginger who has moved to index 2.

Zara realizes that while the computer shows 3 guests at the party, there are 4 guests there. Somehow, Fran came in right after Elizabeth and did not get entered into the computer during the “drama” between Alice and Betty with Elizabeth. Fran needs to be entered into the correct location.

myGuestList.insert(2,"Fran")
print(myGuestList)

We now have 4 guests at the party: Christy is in index 0, Darla is in index 1, Fran in index 2, and Ginger who has moved to index 3.

Zara realizes that there is one person who has been there the entire time, but is not on the list – herself. Since she has been there the entire time, she should be placed at the bottom of the deque.

myGuestList.appendleft("Zara")
print(myGuestList)

We now have 5 people at the party: Zara is in index 0, Christy moves index 1, Darla moves to index 2, Fran moves to index 3, and Ginger moves to index 4.

Everyone, including Zara agrees that the party is pretty lame and that there is a better party down the street. So, everyone is going to leave the party.

myGuestList.clear()
print(myGuestList)

We now have nobody at the party and the deque is empty!

Leave a Reply

Your email address will not be published. Required fields are marked *