Sunday Times Teaser 2757 – Sports Quiz
by Graham Smithers
Published: 26 July 2015 (link)
A sports quiz featured one footballer, one cricketer and one rugby player each week. Over the six-week series the footballers featured were (in order) Gerrard, Lambert, Lampard, Rooney, Smalling and Welbeck. The cricketers were (in some order) Carberry, Compton, Robson, Shahzad, Stokes and Tredwell. The rugby players (in some order) were Cipriani, Launchbury, Parling, Robshaw, Trinder and Twelvetrees. Each week, for any two of the three names, there were just two different letters of the alphabet that occurred in both names (with the letters possibly occurring more than once).
List the cricketers in the order in which they appeared.
One Comment
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253from collections import defaultdictfrom itertools import productfb = ('Gerrard', 'Lambert', 'Lampard', 'Rooney', 'Smalling', 'Welbeck')cr = ('Carberry', 'Compton', 'Robson', 'Shahzad', 'Stokes', 'Tredwell')rg = ('Cipriani', 'Launchbury', 'Parling', 'Robshaw', 'Trinder', 'Twelvetrees')# for a map 'f2rc' from each footballer to a list of (cricket player, rugby# player) name pairs (cr pairs), remove those pairs in lists with more than# one pair where the pair contains a name in the cr pair 'cr'def eliminate(f2cr, cr):for f in list(f2cr.keys()):if len(f2cr[f]) > 1:for c, r in f2cr[f]:if c == cr[0] or r == cr[1]:f2cr[f].remove((c, r))return f2cr# map footballers to lists of cr pairs with which they can pairf2c, f2r = defaultdict(list), defaultdict(list)for f in fb:sf = set(f.lower())# cricket players that can pairf2c[f] = tuple(c for c in cr if len(set(c.lower()) & sf) == 2)# rugby players that can pairf2r[f] = tuple(r for r in rg if len(set(r.lower()) & sf) == 2)# for each footballer list their possible cr pairsf2cr = defaultdict(list)for f in fb:for c, r in product(f2c[f], f2r[f]):if len(set(c.lower()) & set(r.lower())) == 2:f2cr[f] += [(c, r)]# list footballers with only one possible cr pair and then remove# cr pairs from any footballer lists with more than one cr pair# where the pair contains a name uniquely associated with another# footballer; continue until all footballers have a unique cr pairwhile True:# map footballers to cr pairs where the latter is uniquesd = dict((f, f2cr[f][0]) for f in f2cr.keys() if len(f2cr[f]) == 1)if len(sd) == 6:break# eliminate cr pairs from lists for footballer that have more than# one pair if they include a name that is used in a unique cr pairfor f in sd:f2cr = eliminate(f2cr, sd[f])# output the list of cricket and rugby players for each footballerprint(', '.join(sd[f][0] for f in fb))print(', '.join(sd[f][1] for f in fb))