Section 8.3
Coding Task #1
Learning Goals
Students will gain experience writing code in the agnostic language of AP CSP
AAP-1.D.7: The exam reference sheet provides the notation [value1, value2, value3, …] to create a list with those values as the first, second, third, and so on items. For example,
● aList ← [value1, value2, value3,...] creates a new list that contains the values value1, value2, value3, and … at indices 1, 2, 3, and … respectively and assigns it to aList.
● aList ß [] creates a new empty list and assigns it to aList.
● aList ß bList assigns a copy of the list bList to the list aList. For example, if bList contains [20, 40, 60], then aList will also contain [20, 40, 60] after the assignment.
AAP-1.D.8: The exam reference sheet describes a list structure whose index values are 1 through the number of elements in the list, inclusive. For all list operations, if a list index is less than 1 or greater than the length of the list, an error message is produced and the program will terminate.
AAP-2.N.2: List procedures are implemented in accordance with the syntax rules of the programming language.
Objectives and Description
The objective of this lesson is to provide students with exposure to the agnostic coding language used in the AP CSP exam through a coding task. Additionally, this task is meant to prepare students for the first sprint of their ARC Challenge.
Activities
Activity 8.3.1 (55 minutes)
Students should be provided with a copy of the AP CSP exam reference sheet
Students should write a script that does the following:
create an empty list assigned to a variable named songs
add file names song1, song2, and song3 to this list
remove the 2nd element of the list
define a procedure named addSong that takes in the fileName of a song as an argument. This procedure should:
if the length of the list is less than 10, adds this fileName to the end of songs
otherwise, remove the first element of the list before adding fileName to the end of songs
define a procedure named addSongs that takes in two lists listA and listB as arguments. This procedure should:
if the combined lengths of listA and listB exceeds 10, remove enough elements from the beginning of listA to make room for listB's elements
listA will always have more elements than listB
append all the elements contained in listB to the end of listA
stores the combined list in songs
Note: assume that for all list operations, if a list index is less than 1 or greater than the length of the list, the program will end and produce an error message.
Students may have error messages while learning lists. Remind them that list procedures are implemented in accordance with the syntax rules of the programming language.