Last active
August 29, 2015 14:03
-
-
Save justinfay/53b574cf0a492f6795ef to your computer and use it in GitHub Desktop.
Python program resembling how I pile my socks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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