I’m developing a contest bot and encountered an issue: when two participants submit their answers nearly at the same moment, both are awarded a point and the bot advances to the next question. I need the system to credit only the fastest responder. Can someone suggest how to resolve this? Here’s an alternative code snippet to illustrate the approach:
def select_fastest_answer(entries, correct_val):
valid_entries = [e for e in entries if e.get('reply') == correct_val]
if valid_entries:
quickest = min(valid_entries, key=lambda e: e.get('timestamp'))
return quickest.get('user_id')
return None
# Example:
responses = [
{'user_id': 201, 'reply': 'answer', 'timestamp': 1020},
{'user_id': 202, 'reply': 'answer', 'timestamp': 1010}
]
winner = select_fastest_answer(responses, 'answer')
print('Winner:', winner)