Full writeup:
http://blog.samsandberg.com/2014/11/03/socks-pair-matching/
Usage:
$ python socks.py 3 -n 100000
3 pairs of socks
100000 iterations
{False: 60011, True: 39989}
Good 39.99% of the time
Full writeup:
http://blog.samsandberg.com/2014/11/03/socks-pair-matching/
Usage:
$ python socks.py 3 -n 100000
3 pairs of socks
100000 iterations
{False: 60011, True: 39989}
Good 39.99% of the time
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() |