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.
clearvars,clc
names=["book","candy","flowers","money","scarf"];
gifts=perms(names);
recipients=perms(names);
i=1;
while i <= length(gifts)
matches=find(gifts(i,:)==names);
if ~isempty(matches)
gifts(i,:)=[];
else
i=i+1;
end
end
recipients=gifts;
for g=1:length(gifts)
gift=gifts(g,:);
for r=1:length(recipients)
recipient=recipients(r,:);
matches=find(gift==recipient);
if ~isempty(matches) || recipient(2)~="scarf"
continue
end
if gift(find(recipient=="book"))~="candy" ...
|| gift(5)~="flowers"
continue
end
bookGiver=names(find(gift=="book"));
if bookGiver~=recipient(5)
continue
end
disp(recipient)
disp(gift)
disp(' ')
end
end
finds
"flowers" "scarf" "book" "candy" "money"
"scarf" "money" "candy" "book" "flowers"
"candy" "scarf" "book" "flowers" "money"
"scarf" "money" "candy" "book" "flowers"
"flowers" "scarf" "money" "book" "candy"
"money" "book" "scarf" "candy" "flowers"
Rather than add to the program, it's obvious that only the last set has Flowers being given a present that was the namesake of the scarf donor, as the sequence of donors is the top line below.
Donor: "Book", "Candy", "Flowers", "Money", "Scarf"
recipient: "candy" "scarf" "book" "flowers" "money"
gift: "scarf" "money" "candy" "book" "flowers"
|
Posted by Charlie
on 2023-11-28 14:43:37 |