Comp 110 March Sadness

In this problem set you will generate a report that analyzes free throw data across all UNC basketball players this season. Despite the recent results of the tournament, Joel, Theo, and company had an incredible season coming off of a national championship. Here's to taking one last look back at the team's data this semester.

Learning Objectives

After completing this problem set, you should:

1. Be able to use the higher-order function filter to narrow a collection of data down based on some predicate criteria

2. Be able to use the higher-order function  map to process a collection of data by applying a transform function to each element

3. Be able to use the higher-order function reduce to compute a value by processing a collection and accumulating a result (i.e., with a list of numbers, find the sum or maximum)

4. Have practice writing function literals in conjunction with higher-order functions. Though this is not required, it is highly encouraged.

This problem set specifies the required functions you must write and export, however, you are free to arrive at computing the results any way you would like. We strongly encourage you to make as much use applying filter/map/reduce as possible and to reuse the early functions you write wherever possible to avoid redundancy in your code.

Part 0. Starting the Dev Environment

As with in lecture, begin by opening up VSCode, ensuring your project is still open, and then running the following two commands in the VSCode Integrated Terminal:

  1. npm run pull
  2. npm start

The first command "pulls" the latest files needed for COMP110 into your repo, if any. The second command starts the development environment and will open your web browser to the local server. 

The scaffolding of this problem set is already setup for you in the ps04-march-sadness folder. You will be working in the ps04-march-sadness/stats-app.ts file.

Begin by filling in your name and ONYEN in the respective fields of the honor code portion of the file.

0.1 Imports

We have already imported the functions and class you'll need for this project, but it's worth thinking through what is imported for you. Note that filter, map, and reduce are being imported from the file "list-utils.ts" and the Game class is imported from the "Game.ts" file also located in the project directory.

0.2 Data

The data for this project is every UNC men's basketball team player's stat lines from each game this season (minus this last unspeakable game). The file our data resides in is the data/ps04-player-data.csv file. Navigate stats-app.ts file in your web browser and try selecting this data file. You should see a large table print out with a season's worth of data in it. Don't worry, we'll be focusing on free throw data only.

Note that free throw data is contained in 2 properties: fta is short for free throws attempted and ftm is short for free throws made.

0.3 The Goal

Your goal is to generate a report that should look similar to the following by printing the results of calling the functions we require below:

1. Team Totals:
Made: ###
Attempted: ###
 
2. Percentages:
Overall: ##.####
of Wins: ##.####
of Losses: ##.#### 
Joel Berry II: ##.####
 
3. Single Game Record:
Most FTs made: ##
By Player(s): ? → null

Note that the #'s will be replaced with the actual computed numbers. 

Also note, when the autograder is posted it will test using a different data set than this season's to ensure your computations work more generally.

1. Team Totals

1.1 - ftMade - Declare and export a function named ftMade, short for "free throws made". Given a List of Game objects as a parameter, it should return the total number of free throws made by all UNC players in all games this season

Hint: use map to transform the list of Games to a list of numbers where each number is the corresponding Game object's ftm (free throws made) property value. Then reduce by adding up all of the numbers in this list.

From your main function, you should delete the TODO #1 and its corresponding print(games) statement. Then you should test your ftMade function by calling it correctly and printing out its results to begin generating your report. See the example output above for some direction on how to format your report.

For this data set, the correct result you should expect to see returned by the ftMade function is 492. When the grader tests your ftMade function it will use a different, randomized data set to verify its correctness.

1.2 - ftAttempts - Declare and export a function named ftAttempts, short for "free throw attempts". Given a List of Game objects, it should return the total number of free throws attempted by all UNC players in all games this season. Each Game object has a property named fta that will tell you the number of free throws the given player attempted in an individual game. Like with ftMade, you should test this function from your main function and add its result to your printed output. For this data set, the correct result you should expect to see returned is 664.

2. Percentages

Starting from this section, we will assume you are comfortable continuing to test each of the following functions by calling it from your main function and printing its results in a report-like way as per above. Additionally, each of the functions outlined below must be declared and exported.

The goal for these analyses is to compare season free throw percentages overall, in wins, in losses, and then by a specific player.

2.1 - ftPercent - Given a list of Game data, calculate and return the entire team's free throw percentage as calculated by the formula: # made / # attempts * 100.0. The correct result for the data set provided is around 74 percent.

2.2 - ftPercentInWins - Given a list of Game data, calculate and return the entire team's free throw percentage as calculated above, but for only the games in which UNC won. The outcome property of a Game object will be the string "won" if UNC won. The correct result for the data set provided is around 75 percent. Hint: you should use the filter function to help you narrow down your list of Games!

2.3 - ftPercentInLosses - Given a list of Game data, calculate and return the entire team's free throw percentage as calculated above, but for only the games in which UNC lost. The outcome property of a Game object will be the string "lost" if UNC lost.

2.4 - ftPercentOfBerry - Given a list of Game data, calculate and return Joel Berry's free throw percentage for the season. The player property of a Game object contains the player's name. Open the data file in Excel or Google Sheets to know the exact string value you'll need to test for to narrow down on Berry's data only.

3. Single Game Record

What is the most free throws a player made in a single game this season? Who made that many in a game? These are the questions you'll answer in this final section.

3.1 - ftMadeRecord - Given a List of Game objects, each which corresponds to a single player's performance in a single game, return the most free throws made (Game property ftm) by any player of any game.

3.2 - playersByFTMade - This function needs two parameters. Given a List of Game objects and a specific number of made free throws, return a List of only the names of players who made that many free throws in a game. When printing your results in main, print the player(s) holding the record for free throws made. Example output in main (incorrect results used below): 

3. Single Game Record:
Most FTs made: 999
By Player(s): KJ Smith → null

Challenge - Reduce, Reuse, Recycle

Once you have your functions working, try improving your code by avoiding duplicating logic wherever possible. A concise, clean solution containing all of the required functions in this problem set can be written in under 30 lines of code (ignoring the honor code, imports, and main function). Hint: Use function literals!