question 151 Collections & Itertools · easy
Each question's answers, best first
A Q&A page shows the answers to each question with the highest score first. Write answers_by_question(answers): return a plain dictionary from each question ID to the IDs of its answers, highest score first. Answers with the same score stay in the order they were posted (their order in the list).
Function
answers_by_question(answers)answersa list of dictionaries with
answer_id, question_id and score (scores can be negative)Returnsa plain
dict of question ID → list of answer IDs, highest score firstExamples
callanswers_by_question([{'answer_id': 9001, 'question_id': 101, 'score': 3}, {'answer_id': 9002, 'question_id': 102, 'score': 5}, {'answer_id': 9003, 'question_id': 101, 'score': 7}, {'answer_id': 9004, 'question_id': 101, 'score': 3}])returns{101: [9003, 9001, 9004], 102: [9002]}
callanswers_by_question([{'answer_id': 1, 'question_id': 5, 'score': 0}, {'answer_id': 2, 'question_id': 5, 'score': 2}])returns{5: [2, 1]}
callanswers_by_question([])returns{}