MultiSet to Set

Now that you have learned the basic operations to add, access, and remove items in a list, let’s integrate them into a basic procedure.

A procedure is a group of blocks that have been combined to perform a specific task. In Scratch, this is done with the “Make a Block” button in the “More Blocks” pane.

To demonstrate, let’s create a block that resets two lists to a default configuration.

First, we will click the “Make a Block” button, and name the block something descriptive, like “reset lists":

Next, we will associate some code with it. Any time the newly created reset lists block is used, all of the code under the define reset lists block will be executed:

Finally, we may use the new reset lists block anywhere in our code. In this example, the reset lists block is called anytime the sprite is clicked:

Your Task

Your task is to create a block that creates a set from a multiset. A multiset is a collection of items that may contain duplicates; a set does not contain duplicate entries. Order does not matter in either collection.

Example:

Given the multiset { a, d, c, a, c, b }, you are to create a block that will populate the corresponding set { a, d, c, b }. Note that because order does not matter, { b, a, d, c } is also a valid solution.

The Scratch starter code provided here includes two lists entitled multiset and set. You will create a block to perform the conversion as part of the DeDupe sprite, an arrow that begins the “de-duplication” conversion process when clicked.

Note: A completely correct solution will work for any list of items, not just the sample one provided.

The following screenshot depicts the output of a correct solution:

After you have a working solution, submit your code and a description of how you tested it. Note that you should try to test it on more lists than just the one provided!

Extensions

For an additional challenge, try the following:

  • Create a block that incorporates the selection sort code in Reorder so that the items in Set are always in alphabetical order.
  • Create a block that converts MultiSet into a Set rather than simply creating a new Set. To do this, you will need to selectively remove items from MultiSet.