11 Team Activity: Functional Programming
Instructions
Work as a team as explained in the instructions for the lesson 2 team activity.
Purpose
Reinforce in your mind the concept that a function can be passed as an argument into another function.
Assignment
The CSV file named pupils.csv
contains data about
100 students. Unfortunately, the data is not sorted. As a team,
write a program that reads the contents of pupils.csv
into a compound list, sorts the list by birthday from oldest to
youngest, and then prints the list with the data about each student
on a separate line.
Helpful Documentation
- The Reading Files article explains how to setup VS Code so that your Python program can read from a text file.
- The preparation content for lesson 7 explains how to use compound lists.
- The preparation content for lesson 9 explains how to read the contents of a text file into a list.
- The preparation content for this lesson explains higher-order functions, nested functions, lambda functions and how to call the Python built-in
sorted function
.
Steps
Do the following:
- Download these two files: pupils.csv and pupils.py and save them in the same folder.
- Open the
pupils.csv
file in VS Code and notice that it has three columns: givenName, surname, and birthdate. - Open the
pupils.py
file in VS Code. At the bottom of the file write a function namedprint_list
that takes a list as a parameter and prints each element of the list on a separate line. In other words, thisprint_list
function should include afor
loop that prints each element on a separate line. - Near the top of
pupils.py
, below the three indexes, write themain
function. Inside themain
function, write statements to do the following:- Call the
read_compound_list
function to read thepupils.csv
file into a list named students_list. - Write a lambda function that will extract the birthdate from a student.
- Write a call to the Python built-in
sorted
function that will sort the students_list by birthdate from oldest to youngest. - Print the students_list by calling the
print_list
function.
- At the bottom of the
pupils.py
file write a call to themain
function.
Core Requirements
Your program must contain the following:
- A function named
print_list
that takes a list as a parameter and prints the list with each element of the list on a separate line. - A function named
main
that callsread_compound_list
andprint_list
. - Statements in the
main
function that sort the students_list by birthdate from oldest to youngest.
Stretch Challenges
If your team finishes the core requirements in less than an hour, complete one or more of these stretch challenges. Note that the stretch challenges are optional.
- Within the
main
function, replace the code that sorts the students_list by birthdate, with code that sorts the students_list by given name. - Within the
main
function, replace the code that sorts the students_list by birthdate, with code that sorts the students_list by birth month and day. In other words, the code should sort the students_list by birthdate but ignore the year when a student was born.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Run your program and ensure that your program’s output is sorted correctly as shown below.
> python pupils.py Ordered from Oldest to Youngest ['Cody', 'Gjoni', '2008-01-14'] ['Jakob', 'Moore', '2008-08-09'] ['Dylan', 'Bradford', '2008-10-11'] ['Chih-Yang', 'Olson', '2008-12-23'] ['Camdon', 'Radke', '2009-01-31'] ['Jacob', 'Ortiz', '2009-02-04'] ['Tanner', 'McAllister', '2009-02-13'] ['Aoi', 'Lee', '2009-02-22'] ['Colton', 'Kent', '2009-02-25'] ['Marco', 'Zeng', '2009-06-25'] ⋮ Ordered by Given Name ['Adam', 'Chase', '2009-08-04'] ['Aidan', 'Havens', '2014-08-12'] ['Alexander', 'Bingham', '2015-01-30'] ['Amarsanaa', 'Cromar', '2010-07-15'] ['Ammon', 'Reeder', '2014-11-22'] ['Andrea', 'Omokoh', '2014-11-08'] ['Aoi', 'Lee', '2009-02-22'] ['Aranza', 'Billman', '2012-12-08'] ['Benjamin', 'Rojas', '2010-12-24'] ['Brody', 'Wilson', '2013-10-29'] ⋮ Ordered by Birth Month and Day ['Mitchel', 'Elliott', '2010-01-03'] ['Nathan', 'Bowman', '2014-01-07'] ['Christian', 'White', '2015-01-09'] ['Manoel', 'Gonzalez', '2014-01-10'] ['Cody', 'Gjoni', '2008-01-14'] ['Curtis', 'Loveridge', '2011-01-14'] ['Alexander', 'Bingham', '2015-01-30'] ['Camdon', 'Radke', '2009-01-31'] ['Jacob', 'Ortiz', '2009-02-04'] ['Tanner', 'McAllister', '2009-02-13'] ⋮
Sample Solution
Please work diligently with your team for the one hour meeting. After the meeting is over, please compare your solution to the sample solution or the stretch solution. Please do not look at the sample solution until you have either finished the program or diligently worked for at least one hour. At the end of the hour, if you are still struggling to complete the assignment, you may use the sample solution to help you finish.
Submission
When you have finished the activity, please report your progress via the associated I‑Learn quiz. When asked about which of the requirements you completed, feel free to include any work done during the team meeting or after the meeting, including work done with the help of the sample solution, if necessary. In short, report on what you were able to accomplish, regardless of when you completed it or if you needed help from the sample solution.