Sunday Times Teaser 2725 – Darts Match
by Graham Smithers
Five teams of four players each entered a darts competition, the names of the twenty players being: Andrew, Alexander, Austin, Anthony, Benjamin, Charles, Christopher, Elijah, Jacob, Jayden, Jackson, James, Jason, Mason, Michael, Nathan, Newman, Robert, Samuel and William. With the names of those in each team arranged alphabetically, the first was the captain and the last was the reserve.
The teams were such that the names of any two players in a team shared just two letters in common (but these might occur more than once in a name).
With the teams ordered alphabetically by their captain’s names what were the names of their reserves?
One Comment
Leave one →
-
brian gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142from itertools import combinationsfrom operator import itemgetter# the list of names in lower casenames = [x.lower() for x in('Andrew', 'Alexander', 'Austin', 'Anthony', 'Benjamin','Charles', 'Christopher', 'Elijah', 'Jacob', 'Jayden','Jackson', 'James', 'Jason', 'Mason', 'Michael','Nathan', 'Newman', 'Robert', 'Samuel', 'William')]# find three players who can team up with the first name in 'names'def compile_team(names):# list other names that share two letters with the first namenl = tuple(n for n in names[1:] iflen(set(names[0]) & set(n)) == 2)# select three names from this list to form a team of fourfor n3 in combinations(nl, 3):# check that all pairs share only two letters in commonif all(len(set(a) & set(b)) == 2for a, b in combinations(n3, 2)):# if so this is a possible teamyield (names[0],) + n3# recursively find five teams of four plaayer eachdef solve(teams, nms):# once we have compiled four teams, the names left are the fifthif len(teams) == 4:yield teams + [nms]else:for t in compile_team(nms):yield from solve(teams + [t], [n for n in nms if n not in t])for teams in solve([], names):# sort the teams in order of their captainst5 = sorted(teams, key=itemgetter(0))# compile and output the list of reservesnl = ', '.join(t[3] for t in t5)p = nl.rfind(',')nl = nl[:p] + ' and' + nl[p+1:]print('The reserves are {}.'.format(nl.title()))