|
14 | 14 | import math |
15 | 15 |
|
16 | 16 | def test_doors(doors): |
17 | | - '''Check that all open door numbers are perfect squares''' |
18 | | - for i, door in enumerate(doors): |
19 | | - door_num = i + 1 |
20 | | - if door and math.sqrt(door_num) % 1 > 0: # if door open and door number is not a perfect square |
21 | | - return False |
| 17 | + '''Check that all open door numbers are perfect squares''' |
| 18 | + for i, door in enumerate(doors): |
| 19 | + door_num = i + 1 |
| 20 | + if door and math.sqrt(door_num) % 1 > 0: # if door open and door number is not a perfect square |
| 21 | + return False |
22 | 22 |
|
23 | | - return True |
| 23 | + return True |
24 | 24 |
|
25 | 25 |
|
26 | 26 | def print_doors(doors): |
27 | | - for door_num, door in enumerate(doors): |
28 | | - print "Door #{0}: {1}".format(door_num+1, "Open" if door else "Closed") |
| 27 | + for door_num, door in enumerate(doors): |
| 28 | + print "Door #{0}: {1}".format(door_num+1, "Open" if door else "Closed") |
29 | 29 |
|
30 | 30 |
|
31 | 31 | def pass_doors(doors): |
32 | | - for pass_num in xrange(1, 101): # Make 100 passes |
33 | | - current_door = pass_num-1 # Start current door offset at the current pass number ('-1' to account for lists/arrays starting count at 0) |
34 | | - while current_door <= 99: |
35 | | - doors[current_door] = not doors[current_door] # Open door if close, close if open (negate old value using 'not') |
36 | | - current_door += pass_num # Increment current door number by current pass number |
37 | | - return doors |
| 32 | + for pass_num in xrange(1, 101): # Make 100 passes |
| 33 | + current_door = pass_num-1 # Start current door offset at the current pass number ('-1' to account for lists/arrays starting count at 0) |
| 34 | + while current_door <= 99: |
| 35 | + doors[current_door] = not doors[current_door] # Open door if close, close if open (negate old value using 'not') |
| 36 | + current_door += pass_num # Increment current door number by current pass number |
| 37 | + return doors |
38 | 38 |
|
39 | 39 | pass_doors_optimized = lambda doors: [0 if math.sqrt(door_num+1) % 1 > 0 else 1 for door_num, door in enumerate(doors)] |
40 | 40 |
|
41 | 41 |
|
42 | 42 | def main(): |
43 | | - # Create list of 100 elements initialized to 0 to represent all doors being closed. |
44 | | - doors = [0 for x in xrange(0, 100)] |
| 43 | + # Create list of 100 elements initialized to 0 to represent all doors being closed. |
| 44 | + doors = [0 for x in xrange(0, 100)] |
45 | 45 |
|
46 | | - # Run algorithm |
47 | | - doors = pass_doors(doors) |
| 46 | + # Run algorithm |
| 47 | + doors = pass_doors(doors) |
48 | 48 |
|
49 | | - # Print final door states |
50 | | - print_doors(doors) |
| 49 | + # Print final door states |
| 50 | + print_doors(doors) |
51 | 51 |
|
52 | | - # Test algorithm |
53 | | - result = test_doors(doors) |
54 | | - print "Algorithm has {0}".format("passed" if result else "failed") |
| 52 | + # Test algorithm |
| 53 | + result = test_doors(doors) |
| 54 | + print "Algorithm has {0}".format("passed" if result else "failed") |
55 | 55 |
|
56 | | - # Run optimized algorithm |
57 | | - doors = pass_doors_optimized(doors) |
58 | | - print_doors(doors) |
| 56 | + # Run optimized algorithm |
| 57 | + doors = pass_doors_optimized(doors) |
| 58 | + print_doors(doors) |
59 | 59 |
|
60 | | - # Test optimized algorithm |
61 | | - result = test_doors(doors) |
62 | | - print "Optimized algorithm has {0}".format("passed" if result else "failed") |
| 60 | + # Test optimized algorithm |
| 61 | + result = test_doors(doors) |
| 62 | + print "Optimized algorithm has {0}".format("passed" if result else "failed") |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
|
66 | 66 | if __name__ == "__main__": |
67 | | - main() |
| 67 | + main() |
0 commit comments