Sunday Times Teaser 2693 – Let the Dog See the Rabbit
by Angela Newing
In the Burrows, Cook, Field, Skinner and Warren families the husbands own dogs and the wives own rabbits. Each dog bites a different rabbit.
Mr Skinner’s dog bites Mrs Warren’s rabbit. Mr Warren’s dog bites the rabbit owned by the wife of the owner of the dog that bites Mrs Field’s rabbit. Mrs Burrows’ rabbit is bitten by the dog owned by the husband of the lady whose rabbit is bitten by the dog owned by husband of the lady whose rabbit is bitten by the dog owned by Mr Cook.
(a) Whose dog bit Mrs Skinner’s rabbit.
(b) Whose rabbit was bitten by Mr Field’s dog.
One Comment
Leave one →
-
brian gladman permalink12345678910111213141516171819202122232425262728293031from itertools import permutationsnames = ('Burrows', 'Cook', 'Field', 'Skinner', 'Warren')for wives in permutations(names):# map the dogs to the rabbits they bit: bit[dog] => rabbitbit = dict(zip(names, wives))# Mr Skinner's dog bit Mrs Warren's rabbitif bit['Skinner'] != 'Warren':continue# Mr Warren's dog bit the rabbit owned by the wife of# the owner of the dog that bit Mrs Field's rabbit.if bit[bit['Warren']] != 'Field':continue# Mrs Burrows' rabbit was bitten by the dog owned by the# husband of the lady whose rabbit was bitten by the dog# belonging to the husband of the lady whose rabbit was# bitten by the dog belonging to Mr Cook.if bit[bit[bit['Cook']]] != 'Burrows':continue# invert the mapping: bit_by[rabbit] => doginv = dict(zip(wives, names))print("(a) Mr {} and (b) Mrs {}n".format(inv['Skinner'], bit['Field']))for name in names:print("The {} dog bit the {} rabbit".format(name, bit[name]))