# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1414: World series

From New Scientist #2574, 21st October 2006

Baseball’s so-called World Series is won by the first of the two teams involved to win four matches. Since matches cannot be tied, that means that it may be decided in 4, 5, 6 or 7 matches. Matches 1 and 2 are held on Team A’s ground, matches 3, 4 and (if required) 5 on Team B’s ground, and (if required) matches 6 and 7 on Team A’s ground.

1. Assume that each team has an equal chance of winning each match and tell me what, as a percentage expressed to as many decimal place as are necessary, is the probability that (a) the same number of matches will be played on each ground; (b) match 7 will be played.

2. Now assume that there is a 60 percent probability that any match will be won by the home team and answer questions (a) and (b) above.

[enigma1414]

### One response to “Enigma 1414: World series”

1. Jim Randell 8 July 2013 at 8:38 am

This Python 3 program uses the fractions module to maintain accuracy. It runs in 79ms.

```from fractions import Fraction as F
from enigma import printf

# home teams
homes = 'AABBBAA'

# generate matches
# s - sequence of winners of matches played so far
# p - probability of this match outcome
# h - probability of home team win
# a - probability of away team win
def matches(s, p, h, a):
n = len(s)
# are we done?
if s.count('A') == 4 or s.count('B') == 4:
yield (s, p, n)
else:
for x in 'AB':
q = (h if homes[n] == x else a)
yield from matches(s + x, p * q, h, a)

# solve the puzzle with the specifed home/away win probabilities
# (a) find the probability that the same number of matches is played on each ground
# (b) find the probability that match 7 will be played
def solve(hw, aw, part):
a = b = 0
for (s, p, n) in matches('', 1, hw, aw):
# (a) if there are 4 or 6 matches
if n in (4, 6): a += p
# (b) if there are 7 matches
if n == 7: b += p
printf("({part}a) {a} ({p}%)", p=float(100 * a))
printf("({part}b) {b} ({p}%)", p=float(100 * b))

# (1) even chances
solve(F(1, 2), F(1, 2), 1)

# (2) same, but with 60/40 chances
solve(F(3, 5), F(2, 5), 2)
```

Solution: (1a) 43.75%; (1b) 31.25%; (2a) 43.008%; (2b) 32.032%.