fork(6) download
  1. import itertools
  2.  
  3. def outcomes():
  4. """
  5. Compute the outcomes from rolling an eight-sided die twice.
  6. """
  7. return [[(i,j) for j in xrange(1,9)] for i in xrange(1,9)]
  8.  
  9. def favorable(outcome):
  10. """
  11. An outcome if favorable if the sum of both numbers in the
  12. outcome is even.
  13. """
  14. return sum(outcome)%2==0
  15.  
  16. a = outcomes()
  17.  
  18. # The size of the sample space
  19. print 'Size of sample space:', len(a)*len(a[0])
  20.  
  21. # The sample space itself
  22. for outcome in a:
  23. print outcome
  24.  
  25. b = itertools.chain(*a)
  26. a = list(b)
  27.  
  28. # The number of favorable outcomes
  29. b = [outcome for outcome in a if favorable(outcome)]
  30. print 'Favorable outcomes:',len(b)
Success #stdin #stdout 0.02s 4736KB
stdin
Standard input is empty
stdout
Size of sample space: 64
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8)]
[(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8)]
[(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8)]
[(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8)]
[(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8)]
[(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8)]
[(7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8)]
[(8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8)]
Favorable outcomes: 32