High School Computer Science and Programming Workshop
Class 9: Values, Types, Dictionaries


Hesam Samimi, Cliff Kushler
Ananda Living Wisdom School



Here is a direct link to Snap!


To play with Snap! you can just click on the play button on some of the images, like the one above. If you have any problems with it loading, visit Snap! directly in a new browser tab (On that page, click "Run Snap! now"). Another possible solution, if your browser is not Chrome, is to give Chrome a try.


Values, Types

A value is anything we can have in a universe, and a type is the kind of that thing. We use types to classify the kinds of things we can have.

For example, in this Snap! universe, one kind of type is Number, which can values like 42 or 5.3. We have also seen values true and false, for example when we tried the contains tile on a list, to see if a value was existed in a list or not. The type of those two values is called Boolean.

We can see another example of values and types in the figure above. That's right: values such as "what?" or "are you kidding?" belong to a type called String.

Can you see another example of a type there? You got it! List itself is a type and the value:

is an example of the list type.

In fact, every sprite we create represents a value, as well as a type! Why a type? Remember? Because each sprite is also a prototype and we can make many clones (new values) from it! Here is our own Ballio type from Class 2:


Dictionaries

We make a shopping list when we need to go shopping. We call it a list but it's more than a list, is't it? Is it a set then? Again, it's more than that, because other than saying what we need, it usually tells us how much of each item we need:

In programming we have a different type name for these: a Dictionary. That is because just like an English dictionary is a set of terms, each associated with a definition, a dictionary value has a set of values (called keys), each associated with another value.

A dictionary is one-to-one relation between values of one type, and values of another type. For example, we can say about the shopping list figure above that it is a dictionary which takes values of type Produce to values of type Number.


Operations

Like any other data structure, a dictionary has a set of operations. The main ones are typically called put and get.

put is used to add a new association to the dictionary, for example we can add Tomatoes: 2 to our shopping-list dictionary above.

get is used to look up an association in the dictionary, for example we can ask the value for key Avocados from our shopping-list dictionary above, and it should give us the value 3.

What do you think should happen if we look up something that is not in the dictionary, for example asking for Apples? That' right! You will just get nothing!


Exercise: Implement A Dictionary!

In this exercise we will make a dictionary in Snap!. I started this for you. Clicking on the pencil should do put and eye glasses should do get. We are using a couple of list variables called keys and values to store the items in the dictionary (separating keys and values, by putting all keys in one list and all associated values in a another list). But the program is broken! put works fine, but get doesn't. Can you fix it?

Here is a direct link to this exercise.


Home Exercise: Stacks

A Stack is another kind of a data structure, very similar to the List. Instead of list's add and remove, a stack has push and pop operations. push pushes a new item on top, and pop pops out the top one!

Think of a stack of dishes in your kitchen cabinet. You normally pick up the top one (pop) when you need a plate, and when you have washed the dishes, you push one plate at a time to the top of the stack of plates.

When we did the List example in last class, the add operation worked exactly as a stack's push. Yes, it is easier to think of a stack as vertical, but a horizontal one just like our list would still be a stack nonetheless! One the other hand, the remove operation removed the first ball (first-in-first-out or FIFO), while a stack's pop operation would remove the last ball (last-in-first-out or LIFO)!


Exercise: Modify List to Make a Stack!

Open up the list example, shown again below, and see if you can modify some of the scripts to turn it into a stack!

Here is a direct link to this exercise.

Congrats! Now you have a working stack. Save your work on your computer.


Next: Class 10: Graphs, An Algorithm
Previous: Class 8: Binary Search Trees
Back to: Table of Contents