A short coding challenge, this code was written in Python.
- Input text/csv "Score" column has only numbers, error handling wasn't implemented for this case.
- The text/csv file has delimeter ','. This can be improved in the future for user to choose delimeter but wasn't part of this challenge
- There are no empty "cells", this assumption is similar to 1.
- If there is data in the file, there will always be a top scorer, even if there is one person.
This Python script is designed to determine the top scorer from a CSV file or text file containing data about individuals and their scores. The script reads the input data, parses it into dictionaries, identifies the top scorer(s), sorts them alphabetically by name, and displays their names along with their scores.
-
Modular Design: The code is structured into several functions to promote modularity and readability. Each function has a specific responsibility, making the code easier to understand and maintain.
-
Error Handling: Robust error handling has been implemented throughout the code. It ensures that the script gracefully handles various exceptional situations, such as empty input, malformed data, or inconsistent columns in the CSV file. Detailed error messages are provided to assist users in identifying issues.
-
Input Flexibility: The script reads data from standard input, which means it can accept input from both files and manual input through the command line. This design choice enhances the versatility of the script.
-
Data Parsing: The
parse_csvfunction processes CSV data, checking for a minimum of one header and one data row. It ensures data consistency by verifying that the number of columns in each row matches the number of headers. -
Top Scorer Identification: The
find_top_scorersfunction identifies the top scorers by extracting and comparing scores from the parsed data. It allows for multiple top scorers if they have the same highest score. -
Sorting: To present the top scorers alphabetically, the
sort_data_alphabeticallyfunction sorts the data based on the individuals' first and second names. -
Display: The
display_person_infofunction is responsible for displaying each top scorer's name. Additionally, the script displays the top scorer's score after listing their names. -
Main Function: The
mainfunction orchestrates the execution of the script. It reads the input, processes the data, identifies the top scorers, sorts and displays their information, and terminates the script.
- Run the script in a terminal.
- Provide the CSV data through standard input (e.g., by piping a CSV file or manually entering data).
- The script will identify the top scorers, sort them alphabetically, and display their names and scores.
$ cat TestData.csv | python TopScorers.pyThis Python script is designed with a focus on readability, maintainability, and robust error handling. It allows for flexible input methods and provides clear feedback to the user. The modular structure ensures that future updates and enhancements can be made with ease while maintaining code quality.