Sunday Times Teaser 2704 – Celebrity Gardeners
by Graham Smithers
In a show, each of six gardeners (Beardshaw, Dimmock, Flowerdew, Greenwood, Klein and Thrower) exhibited two different shrubs, showing twelve shrubs in total (Abutilon, Azalea, Berberis, Cornus, Euonymus, Hibiscus, Holly, Magnolia, Potentilla, Spiraeas, Weigela and Wisteria) among them.
For each gardener and their two shrubs, any two of their three names always shared exactly two letters (ignoring case and noting that letters may occur more than once in a name).
(a) Which two shrubs did Beardshaw exhibit?
(b) Who exhibited the Abutilon?
One Comment
Leave one →
-
brian gladman permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253from collections import defaultdictfrom itertools import combinationsgardeners = ( 'Beardshaw', 'Dimmock', 'Flowerdew', 'Greenwood','Klein', 'Thrower' )shrubs = ( 'Abutilon', 'Azalea', 'Berberis', 'Cornus', 'Euonymus','Hibiscus', 'Holly', 'Magnolia', 'Potentilla', 'Spiraeas','Weigela', 'Wisteria' )# compile a dictionary that maps gardeners to pairs of shrubs that# each share exactly two letters with the gardener and exactly two# letters with each otherg2s = defaultdict(list)for g in gardeners:# compile a list of shrubs that are 'compatible' with this gardenerl = [s for s in shrubs if len(set(g.lower()) & set(s.lower())) == 2]# but only pairs of shrubs that share exactly two lettersfor s1, s2 in combinations(l, 2):if len(set(s1.lower()) & set(s2.lower())) == 2:g2s[g] += [(s1, s2)]# solve for a list of gardeners (g) and shrubs (s) given a# dictionary (g2s) as above - return a dictionary mapping# gardeners to the shrubs that they exhibit.def solve(g, s, g2s, d=dict()):# if all gardeners have been allocated shrubsif not g:yield delse:# otherwise consider each possible pair of shrubs for the# next gardener on the listfor shrubs in g2s[g[0]]:# are these shrubs still available?if set(shrubs) <= s:# if so, add this pair to our dictionary for the solution,# remove them from the set of available shrubs and move on# to the next gardenerdd = d.copy()dd[g[0]] = shrubsyield from solve(g[1:], s - set(shrubs), g2s, dd)# run through the solutions, compiling the required answersfor sol in solve(gardeners, set(shrubs), g2s):for g in sorted(sol):if g == 'Beardshaw':s1 = 'n{} exhibited a {} and a {}.'.format(g, *sol[g])if 'Abutilon' in sol[g]:s2 = 'nThe {} was exhibited by {}.'.format('Abutilon', g)print(g, sol[g])print(s1 + s2)