Skip to content

Instantly share code, notes, and snippets.

@gekitsuu
Created December 10, 2018 17:56
Show Gist options
  • Save gekitsuu/a370462abf7f6d5db11b28b03184249d to your computer and use it in GitHub Desktop.
Save gekitsuu/a370462abf7f6d5db11b28b03184249d to your computer and use it in GitHub Desktop.
Fun with Eyeglass wipes
"""
I order https://www.amazon.com/Zeiss-Pre-Moistened-Cleaning-Wipes-count/dp/B01E9NC2Z4/ref=sr_1_3_a_it?ie=UTF8&qid=1544464237&sr=8-3&keywords=zeiss+wipes
from Amazon pretty regularly.
The packets come with 2 attached together. When I pull one out that's still 2
attached, I tear off one of them to use and put the other back in the box.
I wanted to know how many times I have to pull a packet from the box before
I am more likely to pull a single packet instead of a double.
An example of how to run this is
python ./wipes.py 10000 # This will run the simulation 10000 times and average the results
Average is 90.9923214320655
"""
import random
import sys
from collections import namedtuple
Packet = namedtuple('Packet', ['num_of_wipes'])
def initialize_box():
return [Packet(2) for x in range(200)]
def take_packet(box, packets_taken):
choice = random.randint(1, len(box)) - 1
chosen_packet = box.pop(choice)
if chosen_packet.num_of_wipes == 2:
box.append(Packet(1))
packets_taken += 1
return box, packets_taken
def chance_to_pull_double(box):
num_double_packets = len([x for x in box if x.num_of_wipes == 2])
chance_to_pull_double = num_double_packets / len(box)
return chance_to_pull_double
def main():
number_of_iterations = int(sys.argv[1])
results = []
for x in range(number_of_iterations):
box = initialize_box()
chances = 1
packets_taken = 0
while chances >= 0.5:
current_box, packets_taken = take_packet(box, packets_taken)
chances = chance_to_pull_double(current_box)
results.append(packets_taken)
print("Average is {0}".format(sum(results) / len(results)))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment