Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import itertools
- def main():
- for people in range(2, 10):
- check_lockers(people)
- def check_lockers(users):
- lockers = range(10)
- count = 0
- adjacent_count = 0
- for chosen_lockers in itertools.combinations(lockers, users):
- if is_adjacent(chosen_lockers):
- adjacent_count +=1
- count += 1
- print 'For %s people and 10 lockers' % users
- print 'Adjacent configurations:', adjacent_count
- print 'Percent adjacent: %f4' % (adjacent_count / float(count))
- print
- def is_adjacent(lockers):
- lockers = list(lockers)
- lockers.sort()
- for i in range(len(lockers) - 1):
- if lockers[i] + 1 != lockers[i+1]:
- return False
- return True
- if __name__ == "__main__":
- main()
- ##### RESULTS
- For 2 people and 10 lockers
- Adjacent configurations: 9
- Percent adjacent: 0.2000004
- For 3 people and 10 lockers
- Adjacent configurations: 8
- Percent adjacent: 0.0666674
- For 4 people and 10 lockers
- Adjacent configurations: 7
- Percent adjacent: 0.0333334
- For 5 people and 10 lockers
- Adjacent configurations: 6
- Percent adjacent: 0.0238104
- For 6 people and 10 lockers
- Adjacent configurations: 5
- Percent adjacent: 0.0238104
- For 7 people and 10 lockers
- Adjacent configurations: 4
- Percent adjacent: 0.0333334
- For 8 people and 10 lockers
- Adjacent configurations: 3
- Percent adjacent: 0.0666674
- For 9 people and 10 lockers
- Adjacent configurations: 2
- Percent adjacent: 0.2000004
Add Comment
Please, Sign In to add comment