A flat-sharing site asks everyone the same questions ("How tidy are you?", "How often do you have guests?") and records each answer on a scale from 1 to 5. Answers are stored as lists in question order, with None for a question someone skipped.
To compare a newcomer's answers me with another person's answers, look only at the questions both of them answered. On those, the disagreement is the mean of the absolute differences between the two answers: the total difference divided by the number of shared questions. People who share no answered question with the newcomer cannot be compared.
Write best_flatmate(me, people) that returns the index of the person in people with the smallest disagreement. If several people have the same smallest disagreement, prefer the one who shares more answered questions with the newcomer, and if that is also equal, the smaller index. Return None if nobody can be compared.
Examples
Input: me = [5, 2, None, 4]
people = [[4, 2, 1, 4], [5, None, None, None], [1, 1, 5, 1], [5, 3, 3, 3]]
Output: 1
Explanation: person 0 differs by 1 + 0 + 0 = 1 over 3 shared questions (1/3); person 1 agrees
exactly on the one shared question (0); person 2 differs by 4 + 1 + 3 = 8 (8/3); person 3
by 0 + 1 + 1 = 2 (2/3).
Input: me = [3, 3], people = [[4, 4], [2, None], [None, None]]
Output: 0
Explanation: persons 0 and 1 both have disagreement 1, and person 0 shares two answers.
Person 2 cannot be compared.
Constraints
0 <= len(people) <= 5000; every list has the same length, between 1 and 40- every answer is
Noneor a whole number from1to5 - the tests build large surveys with
survey_answers(n, q, seed), which returns(me, people)and is available in your code
Goals
- Compare two vectors using only the positions where both have a value
- Turn a Manhattan distance into a mean difference so partial answers are comparable
- Apply a tie rule with more than one level