import pandas as pd from typing import Any, cast, Union, Dict def max_at( data: pd.DataFrame, col: str ) -> pd.DataFrame: return data[data[col] == data[col].max()] def best( data: pd.DataFrame ) -> pd.DataFrame: d = max_at( data, 'weekly' ) e = max_at( data, 'assignments' ) f = max_at( data, 'reviews' ) return cast( pd.DataFrame, d.combine_first( e ).combine_first( f ) ) def get_total( data: pd.DataFrame ): weekly_min = data['weekly'].apply( lambda x: min( x, 9 ) ) return weekly_min + data['assignments'] + data['reviews'] def add_total( data: pd.DataFrame ) -> pd.DataFrame: return data.assign( total = get_total ) def compute_total( data: pd.DataFrame ) -> pd.DataFrame: tot = add_total( data ) return tot[ [ 'student', 'total' ] ] def compute_averages( data: pd.DataFrame ) -> Dict[ str, float ]: data = add_total( data ) cols = data[ [ 'weekly', 'assignments', 'reviews' ] ] return dict( cols.mean() ) import run_tests