Skip to content

Instantly share code, notes, and snippets.

@loisaidasam
Last active August 29, 2015 14:08
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 loisaidasam/1307fa9988404cbe1bed to your computer and use it in GitHub Desktop.
Save loisaidasam/1307fa9988404cbe1bed to your computer and use it in GitHub Desktop.
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