← 返回 doordash 的题目列表Secret Santa Assignment
类型:online_judge
In a Secret Santa event, there are N participants. Each person needs to give and receive one gift, following these conditions:
Each recipient has one giver.
Each giver gives to one recipient.
No one can give a gift to themselves.
Write a function assign_gifts(N, last_year_assignment=None) to generate this year's assignment. If last_year_assignment is provided, ensure this year's assignment differs from last year's while still satisfying all conditions.
Input:
N: Integer, number of participants.
last_year_assignment: Dictionary, last year's assignment with givers as keys and recipients as values.
Output:
A dictionary, this year's assignment.
Test cases:
assign_gifts(5)
# Possible output: {1: 2, 2: 3, 3: 4, 4: 5, 5: 1}
assign_gifts(3, {1: 2, 2: 3, 3: 1})
# Possible output: {1: 3, 3: 2, 2: 1}
Example
Input
5