Sunday Times Teaser 2571 – Dog Show
by Angela Newing
Published: 1 January 2012 (link)
Five dogs were entered by owners Alan, Brian, Colin, David and Edward, whose surnames are Allen, Bryan, Collins, Davis and Edwards. No owner had the same first and second initials, no two had the same pair of initials and none shared an initial with their breed. Colin was in position 1, David Allen’s dog was in position 4, Brian’s corgi was not next to the collie; the chow and doberman were at opposite ends. The other breed was a dachshund. In the voting, dogs eliminated in order were Mr Bryan’s, the corgi, David’s and the doberman.
Who won, and what breed was their dog?
One Comment
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879from itertools import permutationsfirst_names = ('Alan', 'Brian', 'Colin', 'David', 'Edward')second_names = ('Allen', 'Bryan', 'Collins', 'Davis', 'Edwards')breeds = ('Chow', 'Collie', 'Corgi', 'Dachshund', 'Doberman')# compile a list of the compatible combinations of first names# with surnames - each entry in this list is a tuple containing# five pairs of compatible first and second namesname_to_surname = []# permute the surnamesfor surnames in permutations(second_names):# associate names with surnamesz = tuple(zip(first_names, surnames))# no person has the same two initial lettersif any(f[0] == s[0] for f, s in z):continue# no two people have the same pair of initial letterst = list(frozenset(f[0] + s[0]) for f, s in z)if len(t) != len(set(t)):continue# David's surname is Allenif first_names.index('David') != surnames.index('Allen'):continuename_to_surname += [z]# permute the dogs in the line upfor dogs in permutations(breeds):# the Chow and the Doberman were at opposite ends of the lineif set((dogs.index('Chow'), dogs.index('Doberman'))) != set((0, 4)):continue# the Corgi was not next to the Collieif abs(dogs.index('Corgi') - dogs.index('Collie')) == 1:continue# pick a set of five compatible names and surnamesfor fs in name_to_surname:# permute them for association with their possible dogsfor fsp in permutations(fs):# create the five (name, surname, dog) triplesnames, surnames = zip(*fsp)z = list(zip(names, surnames, dogs))# no dog breed shares an initial letter with those of its ownerif any(d[0] in (f[0], s[0]) for f, s, d in z):continue# Colin is not in the first position and David is not in the fourthif 'Colin' not in z[0] or 'David' not in z[3]:continue# Brian owns the Corgiif names.index('Brian') != dogs.index('Corgi'):continuefor f, s, d in z:# neither David nor Mr Bryan own either the Corgi or the Dobermanif (f == 'David' or s == 'Bryan') and d in ('Corgi', 'Doberman'):break# now filter out those mentioned during the elimination processif s == 'Bryan' or d == 'Corgi' or f == 'David' or d == 'Doberman':continue# leaving the winning combinationprint('{} {} won with his {}.'.format(f, s, d))