Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import itertools
  2.  
  3. def main():
  4. for people in range(2, 10):
  5. check_lockers(people)
  6.  
  7. def check_lockers(users):
  8. lockers = range(10)
  9. count = 0
  10. adjacent_count = 0
  11. for chosen_lockers in itertools.combinations(lockers, users):
  12. if is_adjacent(chosen_lockers):
  13. adjacent_count +=1
  14. count += 1
  15.  
  16. print 'For %s people and 10 lockers' % users
  17. print 'Adjacent configurations:', adjacent_count
  18. print 'Percent adjacent: %f4' % (adjacent_count / float(count))
  19. print
  20.  
  21. def is_adjacent(lockers):
  22. lockers = list(lockers)
  23. lockers.sort()
  24. for i in range(len(lockers) - 1):
  25. if lockers[i] + 1 != lockers[i+1]:
  26. return False
  27. return True
  28.  
  29. if __name__ == "__main__":
  30. main()
  31.  
  32. ##### RESULTS
  33.  
  34.  
  35. For 2 people and 10 lockers
  36. Adjacent configurations: 9
  37. Percent adjacent: 0.2000004
  38.  
  39. For 3 people and 10 lockers
  40. Adjacent configurations: 8
  41. Percent adjacent: 0.0666674
  42.  
  43. For 4 people and 10 lockers
  44. Adjacent configurations: 7
  45. Percent adjacent: 0.0333334
  46.  
  47. For 5 people and 10 lockers
  48. Adjacent configurations: 6
  49. Percent adjacent: 0.0238104
  50.  
  51. For 6 people and 10 lockers
  52. Adjacent configurations: 5
  53. Percent adjacent: 0.0238104
  54.  
  55. For 7 people and 10 lockers
  56. Adjacent configurations: 4
  57. Percent adjacent: 0.0333334
  58.  
  59. For 8 people and 10 lockers
  60. Adjacent configurations: 3
  61. Percent adjacent: 0.0666674
  62.  
  63. For 9 people and 10 lockers
  64. Adjacent configurations: 2
  65. Percent adjacent: 0.2000004
Add Comment
Please, Sign In to add comment