Scratch Part 11: Lists

A list is what it sounds like: a number of items placed one after another, as in a shopping list. When you have a list you probably perform one or more associated tasks e.g. collecting the items one by one from around the shop. Other operations might be:

Other examples of lists:

In computer science there are special lists (called data structures) that include: arrays (not in Scratch), linked lists, circular lists, stacks and queues.

Scratch has commands for building and processing lists, which are found under Variables. First you need to make a list and then you will see:

Task 16: List Operations

Produce solutions for as many of these as you can:

16a. Populate a list

Think about how many items you want to put in the list. Do you want to use Ask to get the number from the user? Will the items be numbers generated at random? Will they be words entered by the user? Will they be collected from the screen in some way? e.g. by touching a sprite.

16b. Clear the list of all contents

For this you can use delete.

16c. Check if an item is in a list; if it is, return its position

For this you would have to look through the list until you find the item required. Will you ask the user to enter the value required?

16d. Insert and Delete items

Insert at the end and the start of a list. Delete from the end and the start. Insert to and delete from any position.

16e. Count the number of identical items in a list

This will require something to hold the count of items.

16f. Sum the numerical values of items in a list

This will require something to hold the total.

16g. Shuffle the items in a list

This will require an understanding of what 'shuffle' means. You will have to define this before you write the code. A shuffled list should be in a different order from what it was at the start, as when shuffling a pack of cards.

16h. Copy the contents of one list to another

By now you should be able to solve this yourself.

16i. Compare the contents of two lists

By now you should be able to solve this yourself.

16j. Reverse the order of items in a list

By now you should be able to solve this yourself.

16k. Sort the items in a list

Use the 'bubble sort'. You will need to understand this before you implement it in Scratch.

16l. Stack

Place items in a list at the end and remove them from the same end only. This implements a stack. By now you should be able to solve this yourself.

16m. Queue

Insert items at one end of the list and remove them from the other end. This implements a queue. By now you should be able to solve this yourself.

These are basic actions that you could use in a Scratch program such as a game. They are rather abstract in nature and typical of the basic routines found in more advanced programming.

Elevator Problem

The elevator problem uses a list to store requests for floors. Requests can be placed in the list by the programmer who can either enter values and save the program in that state or use add thing to list to make the program do it at run time. The user can add items to a list by clicking on the display's + sign. Items can also be entered in the request list by clicking on a button that uses add thing to list to insert an item.