A cinema counts, for every ticket sold last month, the kind of film and whether the customer also bought popcorn. The counts are a two-way table given as a dictionary of dictionaries: table[row][col] is the number of tickets in that row and column, for example
{"comedy": {"popcorn": 120, "none": 80},
"drama": {"popcorn": 45, "none": 105},
"horror": {"popcorn": 90, "none": 60}}
Every row has the same column names. Write three_questions(table, row, col) that returns a tuple of three floats for a ticket chosen at random from all the tickets:
P(col | row): among the tickets in that row, the share in that column;P(row | col): among the tickets in that column, the share in that row;P(row and col): the share of all tickets that are in both.
A conditional probability whose condition has no tickets at all is None, and so is the joint probability if the whole table is empty.
Examples
Input: the table above, row = "horror", col = "popcorn"
Output: (0.6, 0.35294117647058826, 0.18)
Explanation: 90 of the 150 horror tickets came with popcorn (0.6); 90 of the 255 popcorn
tickets were for horror films (0.353); 90 of all 500 tickets were both (0.18).
Constraints
- 1 to 20 rows and 1 to 20 columns; counts are integers between
0and10**6 rowandcolare names in the table- floats are compared with a tolerance of
1e-6
Goals
- Read a joint probability and both conditional probabilities from a two-way table
- Divide by the row total for P(column | row) and by the column total for P(row | column)
- Handle an empty row or column