question 170 Nested Data Structures · hard
Build question threads from flat tables
The Q&A export arrives as two flat tables: questions and answers. The site's thread page needs them nested: each question with the IDs of its answers, best first. Answers that point at a question you weren't given (deleted ones) are ignored.
Function
thread_view(questions, answers)questionsdictionaries with
question_id, title and tagsanswersdictionaries with
answer_id, question_id and scoreReturnsa list with one
{"question_id": …, "title": …, "answers": [answer ids]} per question, in the questions' orderAnswer orderhighest score first; equal scores by smaller
answer_id first; a question with no answers gets []Must notchange either input list or any record in them
Examples
callthread_view([{'question_id': 70015316, 'title': 'TypeError when I inherit from a base class', 'tags': ['python']}, {'question_id': 70015402, 'title': 'Why does my CSV have blank lines?', 'tags': ['csv']}, {'question_id': 70015577, 'title': 'How do I read a gzip file?', 'tags': []}], [{'answer_id': 70009556, 'question_id': 70015316, 'score': 4}, {'answer_id': 70009601, 'question_id': 70015402, 'score': 9}, {'answer_id': 70009587, 'question_id': 70015316, 'score': 11}, {'answer_id': 70009612, 'question_id': 70015316, 'score': 4}, {'answer_id': 70009999, 'question_id': 69999999, 'score': 2}])returns[{'question_id': 70015316, 'title': 'TypeError when I inherit from a base class', 'answers': [70009587, 70009556, 70009612]}, {'question_id': 70015402, 'title': 'Why does my CSV have blank lines?', 'answers': [70009601]}, {'question_id': 70015577, 'title': 'How do I read a gzip file?', 'answers': []}]
callthread_view([{'question_id': 70015577, 'title': 'How do I read a gzip file?', 'tags': []}], [])returns[{'question_id': 70015577, 'title': 'How do I read a gzip file?', 'answers': []}]
callthread_view([], [{'answer_id': 70009556, 'question_id': 70015316, 'score': 4}, {'answer_id': 70009601, 'question_id': 70015402, 'score': 9}, {'answer_id': 70009587, 'question_id': 70015316, 'score': 11}, {'answer_id': 70009612, 'question_id': 70015316, 'score': 4}, {'answer_id': 70009999, 'question_id': 69999999, 'score': 2}])returns[]