1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# find a set of scores (scrs) over (n) innings such that # the scores are all different and the cumulative batting # averages after each innings (avgs) are all different def solve(n, i=0, scrs=tuple(), avgs=tuple()): # have we finished? if i == n: yield scrs, avgs else: # consider the possible scores in the next innings for s in range(30): # form the next cumulative batting average av, r = divmod((i * avgs[-1] if i else 0) + s, i + 1) # if it is integer and the score hasn't occurred before if not r and s not in scrs: # proceed to the next innings yield from solve(n, i + 1, scrs + (s,), avgs + (av,)) # for storing the solution s6x, sc, av, cnt = 0, 0, 0, 0 # consider possible scores over five innings for scrs, avgs in solve(5): # collect solutions for a sixth innings # meeting the same contraints t = -sum(scrs) % 6 s6v = [x for x in range(30) if x % 6 == t and x not in scrs] # consider the minimum score that gives a # solution and find the five innings solution # for which this minimum value is a maximum if s6v and s6v[0] >= s6x: # do we have a new maximum? if s6v[0] > s6x: s6x = s6v[0] cnt = 0 # otherwise print a solution for the current maximum cnt += 1 sc = scrs + (s6x,) av = avgs + ((5 * avgs[-1] + s6x) // 6,) print(f'{s6x}({cnt}): scores: {sc}, averages: {av}.') print(f'Answer: {s6x}') |