Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Socks - Pair Matching
import argparse
import random
def iteration(num_pairs):
socks = []
for i in xrange(num_pairs):
# 0 and 1 representing R and L socks
socks += [0, 1]
random.shuffle(socks)
for i in xrange(num_pairs):
# Sums of each two consecutive socks should be 1 (0 + 1)
if sum(socks[i*2:i*2+2]) != 1:
return False
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument('num_pairs', type=int)
parser.add_argument('-n', '--num_iterations', type=int, default=1)
args = parser.parse_args()
results = {
True: 0,
False: 0,
}
for i in xrange(args.num_iterations):
results[iteration(args.num_pairs)] += 1
print "%s pairs of socks" % args.num_pairs
print "%s iterations" % args.num_iterations
print results
print "Good %0.2f%% of the time " % (100.0 * results[True] / args.num_iterations)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment