From 8b70358a5b0d28abddf4ed653644126410d74a4d Mon Sep 17 00:00:00 2001 From: holly Date: Wed, 1 Nov 2023 11:56:28 +0000 Subject: [PATCH 1/2] completed tech test --- junior_tech_test.py | 84 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 11 deletions(-) diff --git a/junior_tech_test.py b/junior_tech_test.py index 78de724..080c565 100644 --- a/junior_tech_test.py +++ b/junior_tech_test.py @@ -1,59 +1,121 @@ +import csv + + def read_csv_file(file_path): """ Read a CSV file and return its content as a list of dictionaries. """ - return + with open(file_path, 'r') as file: + reader = csv.reader(file) + headers = next(reader) + data = [] + for row in reader: + row_dict = {} + for i in range(len(headers)): + if i < len(row): + row_dict[headers[i]] = row[i] + else: + row_dict[headers[i]] = None + data.append(row_dict) + return data def get_unique_teams(data): """ Return a set of unique team names from the provided data. """ - return + unique_teams = set() + + for row in data: + if 'team_name' in row and row['team_name'] is not None: + unique_teams.add(row['team_name']) + return unique_teams def get_most_common_event_type(data): """ Return the most common event type name from the provided data. """ - return + event_types = [row['event_type_name'] for row in data if 'event_type_name' in row and row['event_type_name'] is not None] + if not event_types: + return None + return max(set(event_types), key=event_types.count) + def filter_by_team(data, team_name): """ Filter the data by the provided team name and return the filtered data. """ - return + filtered_data = [row for row in data if 'team_name' in row and row['team_name'] == team_name and row['team_name'] is not None] + return filtered_data + + def count_event_type_by_team(data, team_name, event_type_name): """ Count the number of events of a specific type for a given team. """ - return + count = sum(1 for row in data if row['team_name'] == team_name and row['event_type_name'] == event_type_name) + return count + + def average_pass_length_by_team(data, team_name): """ - Calculate the average pass length for the provided team to 1 decimal place + Calculate the average pass length for the provided team to 1 decimal place. """ - return + pass_lengths = [float(row['pass_length']) for row in data if row['team_name'] == team_name and row['pass_length']] + if not pass_lengths: + return 0.0 + average_length = round(sum(pass_lengths) / len(pass_lengths), 1) + return average_length + + + def filter_players_by_position(data, position_name): """ Return a list of player names who play at the provided position. """ - return + players = {row['player_name'] for row in data if row['player_position_name'] == position_name} + return players + + def count_successful_passes(data): """ Count the number of successful passes (not considering pass outcome). """ - return + successful_passes = 0 + for i in range(len(data) - 1): + current_event = data[i] + next_event = data[i + 1] + + if current_event["event_type_name"] == "Pass" and next_event["event_type_name"] == "Ball Receipt*": + successful_passes += 1 + + return successful_passes + + # I can't figure this one out + def filter_by_period(data, period): """ Return a list of events that occurred in the provided period (e.g., 1 or 2). """ - return + filtered_data = [row for row in data if row.get('period') == period] + return filtered_data + def count_shots_by_player(data, player_name): """ Count the number of shots taken by the provided player. """ - return + shots = 0 + index = set() + + for row in data: + if row["player_name"] == player_name and row["event_type_name"] == "Shot": + if row["index"] not in index: + index.add(row["index"]) + shots += 1 + + return shots From 7aadb9c29a001af8370066e6e232e9f23682cf5c Mon Sep 17 00:00:00 2001 From: holly Date: Wed, 1 Nov 2023 12:18:03 +0000 Subject: [PATCH 2/2] completed tech test --- junior_tech_test.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/junior_tech_test.py b/junior_tech_test.py index 080c565..719ec4c 100644 --- a/junior_tech_test.py +++ b/junior_tech_test.py @@ -5,18 +5,12 @@ def read_csv_file(file_path): """ Read a CSV file and return its content as a list of dictionaries. """ - with open(file_path, 'r') as file: - reader = csv.reader(file) - headers = next(reader) + + with open(file_path, encoding='utf-8') as c: + reader = csv.DictReader(c, delimiter=',') data = [] for row in reader: - row_dict = {} - for i in range(len(headers)): - if i < len(row): - row_dict[headers[i]] = row[i] - else: - row_dict[headers[i]] = None - data.append(row_dict) + data.append(row) return data def get_unique_teams(data):