Participants: Book, Candy, Flowers, Money and Scarf.
Gifts: book, candy, flowers, money and scarf.
The participants are unmarried, grown-up people of mixed genders, each being both a giver (donor) and a receiver, in a way that no one gave or received an item bearing his or her name and none gave a present to the person from whom he or she received one.
The present received by Flowers was the name of the donor of the scarf, Scarf sent flowers to the book-giver, Candy gave Scarf a present and Book received candy.
a. Find out, who gave what to whom.
b. You may deduce (with no certainty) from the nature of the gifts the probable gender of each of the people mentioned,
Source : (Slightly abridged) problem #98 from Penguin’s Problems book by William and Savage 1940.
From "none gave a present to the person from whom he or she received one", we know that the 5 make up a circuit; no sub-circuit of 2 or 3
Use a single Capital letter to represent each person, and a lowercase letter to be each gift. Generate 11 digit strings, alternating Upper and lower case letters: Donor-gift-Recipient etc. The 11th character is the same as the first to indicate how the circuit wraps around. wlog let Book be first in the list, so each 11 character string starts and ends with B
Translate each constraint into constraints on the strings.
no one gave or received an item bearing his or her name
no double letters such as cC or Ff
The present received by Flowers was the name of the donor of the scarf
the lowercase letter before F, when capitalized is before s
Scarf sent flowers to the book-giver
Sf appears; index of b is 2 more than f
Candy gave Scarf a present
index of S is 2 more than C
Book received candy
cB appears
The unique solution is: BsCmSfMbFcB
Book gave a scarf to Candy who gave money to Scarf, ... etc
--------------------
participants = ['B', 'C', 'F', 'M', 'S']
gifts = [x.lower() for x in participants]
from itertools import permutations
patterns = []
for people in permutations(participants):
if people[0] != 'B':
continue
peopleCircle = []
peopleCircle = (list(people) + ['B'])
for things in permutations(gifts):
circleOfGifts = ['B']
for i in range(5):
circleOfGifts.append(things[i])
circleOfGifts.append(peopleCircle[i+1])
patterns.append(''.join(circleOfGifts))
for p in patterns:
q = p.lower()
if 'bb' in q or 'cc' in q or 'ff' in q or 'mm' in q or 'ss' in q:
continue
indexF = p.index('F')
indexs = p.index('s')
if p.index(p[indexF - 1].upper()) != indexs - 1:
continue
if 'Sf' not in p:
continue
indexb = p.index('b')
indexf = p.index('f')
if (indexf + 2)%10 != indexb:
continue
indexS = p.index('S')
indexC = p.index('C')
if (indexC + 2)%10 != indexS:
continue
print(p)
|
Posted by Larry
on 2023-11-28 22:31:17 |