Skip to content

Instantly share code, notes, and snippets.

@justinfay justinfay/sockpile.py
Last active Aug 29, 2015

Embed
What would you like to do?
Python program resembling how I pile my socks.
import random
# Each colour of sock represented by an integer.
socks = 2 * range(20)
# Randomize the order of our socks.
random.shuffle(socks)
# Container variable for our sorted socks and working stack.
sorted = []
stack = []
# Iterate over the unsorted socks one by one.
for sock in socks:
if sock in stack:
# If we have a matching sock already picked add them to our
# sorted socks.
stack.remove(sock)
sorted.append(sock)
else:
# Otherwise place the sock in our sorting stack.
stack.append(sock)
# Print a per sock representation of our sorting procedure.
print 'Stack:', stack
print 'Sorted:', sorted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.