Sunday Times Teaser 3079 – Halls of Residence
by Angela Newing
Published Sunday September 26 2021 (link)
Oak Hall at Woodville University has groups of five study bedrooms per flat and they share a kitchen/diner. In one flat live language students Andy, Bill, Chris, Dave and Ed. Bill, whose home town is Dunstable is reading French. The person in room 5 comes from Colchester and Dave comes from Brighton. The chap reading German has the room with a number one greater than the man from Gloucester. Chris occupies room 3, and Ed is reading Italian. The man in room 2 is reading Spanish, and the man reading English has a room whose number is two different from the student from Reigate.
What is Andy’s subject and where is his home?
2 Comments
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from itertools import permutationsnames = 'Andy Bill Chris Dave Ed'.split()towns = 'Brighton Colchester Dunstable Gloucester Reigate'.split()study = 'English French German Italian Spanish'.split()# students are considered in alphabetical order# permute the home towns for the studentsfor homes in permutations(towns):n2h = dict(zip(names, homes))h2n = dict(zip(homes, names))# Bill's home town is in Dunstable and Dave's is in Brightonif n2h['Bill'] != 'Dunstable' or n2h['Dave'] != 'Brighton':continue# permute the languages being studied by the studentsfor langs in permutations(study):n2l = dict(zip(names, langs))l2n = dict(zip(langs, names))# Bill is studying French and Ed is studying Italianif n2l['Bill'] != 'French' or n2l['Ed'] != 'Italian':continue# permute the students room numbersfor rooms in permutations(range(1, 6)):n2r = dict(zip(names, rooms))r2n = dict(zip(rooms, names))# Chris occupies room threeif r2n[3] != 'Chris':continue# The person in room two is studying Spanish and the# home of the person in room five is in Colchesterif n2l[r2n[2]] != 'Spanish' or n2h[r2n[5]] != 'Colchester':continue# The person studying German has the room with a number# one greater than the man from Gloucesterif n2r[l2n['German']] != n2r[h2n['Gloucester']] + 1:continue# The man studying English has a room whose number is# two different from the student from Reigateif abs(n2r[l2n['English']] - n2r[h2n['Reigate']]) != 2:continueprint(" Name Room Language Home Town")for n in names:print(f"{n:>5} {n2r[n]:>4} {n2l[n]:>8} {n2h[n]:>10}")
-
GeoffR permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950from itertools import permutationsnames = 'Andy Bill Chris Dave Ed'.split()towns = 'Brighton Colchester Dunstable Gloucester Reigate'.split()langs = 'English French German Italian Spanish'.split()# allocate students to roomsfor S in permutations(range(1, 6)):andy, bill, chris, dave, ed = S# allocate towns to roomsfor T in permutations(range(1, 6)):brighton, colchester, dunstable, gloucester, reigate = T# allocate languages to roomsfor L in permutations(range(1, 6)):english, french, german, italian, spanish = L# check other teaser conditionsif bill != dunstable or bill != french:continueif colchester != 5:continueif dave != brighton:continueif german != gloucester + 1:continueif chris != 3:continueif ed != italian:continueif spanish != 2:continueif abs(english - reigate) != 2:continueprint(" Name Room Language Home Town")for i in range(5):r = S[i]print(f"{names[i]:>5} {r:>5} {langs[L.index(r)]:>9} {towns[T.index(r)]:>11}")