Monday, October 6, 2014

List Applications

List Applications
  1. Watch the video below entitled "List Applications." The link to the slides is just above the video.
  2. There are many application areas where lists can be beneficial. A short example program that plays a short song/jingle is created in this lesson (watch the video "Playing a song with two parallel lists"). Can you identify this song (be the first to post to Piazza - but please check whether someone else has already posted)? This program creates a block called playSong that receives two parameters as input - a list of notes, and a corresponding list representing the duration of each note. The meaning is that the note found in a particular index of the list "notes" is played by the duration indicated using the same index into the "duration" list (e.g., when index is 2, note 65 will be played for a duration of 0.15 seconds). The playSong block is called twice with the same parameters with a brief rest between each iteration. Study the code in the video and on slide 2 of "List Applications" - notice how the index loops over the length of the notes, and how the "play note" block is called by retrieving both the note and the duration from the two parallel lists. If you would like to understand more about how to play notes in Snap!, please check out the following web page (suggested by Bucky Garner) that lists the note values.
  3. Snap! provides a pre-existing block called "contains" that will return a Boolean value based on whether a certain item is contained within a specific list. However, in some cases, it may be useful to know the specific location in the list where an item appears, beyond just whether it exists in the list (e.g., it may be helpful to know that a specific student name is found at index 3 in a studentNames list to allow for further processing of that student). Watch the video below entitled "Searching for the location of an item in a list." Study the code that appears in this video (and on slide 4 of the presentation) and observe the check for the item in the loop using the "if" statement. Also, notice how the value of the index is reported back to the caller when the item is found, or else reported back as -1 when all of the list items have been exhausted without a successful search. Try to follow the logic of this code - ask questions on Piazza or Hangouts if this is not clear to you how it works.


Planning Ahead 

 Reusable software is very important to improve productivity and even correctness of software. Procedural abstraction allows us to create our own blocks that are generalized for many different contexts of usage. This lesson explored the benefits of customized blocks and also introduced the concept of lists. Make sure that you study the various list examples in this lesson and notice the pattern that is often used for iterating over a list (see point 4 above in the "Lists" section that mentions the parts of this pattern).

 The next lesson will be focus on two case studies that help guide you into creating more detailed programs than what we have seen in the past lessons. You will create a maze game through a guided video, and then you will be asked to create a version of Snappy Bird by following the instructions on a 30-page tutorial. Before you move forward to the next lesson, please make sure that you understand the general programming concepts previously introduced in the lessons of Unit 3.

No comments:

Post a Comment