From gslindstrom at gmail.com Mon Nov 17 16:58:44 2008 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Mon, 17 Nov 2008 09:58:44 -0600 Subject: [PyAR2] Programming Challenge III Message-ID: Here's one I have not done before. At work, we have door locks with 5 buttons, 1, 2, 3, 4 and 5. To open the door you must press the buttons in the correct order. I would like to see a program that will generate a random sequence for combinations (I believe my mathematical friends would say permutations because order matters) satisfying the following conditions: All of the buttons must be pressed to open the door. 1 or 2 of the buttons may be pressed simultaneously (example: 1 and 3 together, then 2, then 4 and 5 together). Extra credit: How many permutations are there using the above criteria? Good Luck, --greg -------------- next part -------------- An HTML attachment was scrubbed... URL: From gslindstrom at gmail.com Mon Nov 17 20:20:19 2008 From: gslindstrom at gmail.com (Greg Lindstrom) Date: Mon, 17 Nov 2008 13:20:19 -0600 Subject: [PyAR2] Programming Challenge III - My first cut Message-ID: OK. I played around for about 15 minutes... import random buttons = ['1','2','3','4','5'] def choose_button(): '''Select a button from the list and then remove it from the list''' global buttons button = random.choice(buttons) buttons.pop(buttons.index(button)) return button while len(buttons) > 0: if len(buttons)>1: number_of_buttons = random.randint(1, 2) if number_of_buttons == 1: print choose_button() else: print '%s, %s' % (choose_button(), choose_button()) else: print buttons[0] break Also, I think there are 450 permutations. IIRC, (5 choose 2) = 10 and (3 choose 2) = 3 For a combination with all single buttons there are 5! or 120 combinations For a combination with with 1 "double" and 4 "singles": (x,y) is "x choose y) 2 1 1 1 = (5,2) x 3 x 2 x 1 = 10 x 3 x 2 x 1 = 60 1 2 1 1 = 5 x (4,2) x 2 x 1 = 5 x 6 x 2 x 1 = 60 1 1 2 1 = 5 x 4 x (3,2) x 1 = 5 x 4 x 3 x 1 = 60 1 1 1 2 = 5 x 4 x 3 x (2,2) = 5 x 4 x 3 x 1 = 60 There are 240 combinations with 1 set being "doubled" With 2 sets being doubled 2 2 1 = (5,2) x (3,2) x 1 = 10 x 3 x 1 = 30 2 1 2 = (5,2) x 3 x (2,2) = 10 x 3 x 1 = 30 1 2 2 = 5 x (4,2) x (2,2) = 5 x 6 x 1 = 30 With 2 sets doubles there are 90 combinations. So, 120 + 240 + 90 = 450 combinations. -------------- next part -------------- An HTML attachment was scrubbed... URL: