Skip to content

Instantly share code, notes, and snippets.

@justinfay
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfay/53b574cf0a492f6795ef to your computer and use it in GitHub Desktop.
Save justinfay/53b574cf0a492f6795ef to your computer and use it in GitHub Desktop.
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