Speeding our way through

How does it feel to be awesome? You’re now midway through the adventure. Hopefully you’ve had a good week of catching up, relaxing or working ahead and hanging with us on the discussion forums.

Remember last week when we made a list of shapes? Remember also, how we made " sign at the beginning and the end of each shape? That is because we told the computer to treat our rough-looking shapes like Strings. They are a way of presenting a bunch of characters together. Of course I simplified this, but I like to first understand these concepts at a high level and then dig in.

Important: In Python strings can be represented with the sign " as well as '. It does not matter which one you choose to use, but I recommend you choose one and stick to it.

We will also look into something called sequences. They are called like that because they are storing a sequence of elements. We have already met lists, now we will meet their friends tuples and dicts.

Remember, you can change the element in the list like this:

list = ['a', 'b', 'c']
list[1] = 'g'

which would change our list to ['a', 'g', 'c']. An index in a list always starts with 0.

If we want to make a tuple we would use () signs instead of [] to wrap our elements in it. It is impossible to change elements in a tuple, which means we would use it on data that doesn’t need to change. In our Tetris game we could store the points vlues of our little rectangles and their coordinates in a list of tuples, like so:

shapes = [(Point(Point(center.x - 1, center.y), 
           Point(center.x, center.y), 
           Point(center.x + 1, center.y), 
           Point(center.x + 1, center.y + 1)), 
           	           .
           	           .
           	           .
]

We could get the points out like this J_shape = shapes[0]. This is a little awkward though, as it is very hard to remember which shape is at which index in the list of tuples.

So a better sequence to store the points is a dict. This is used when you need to find an element but you don’t know the index number, however you have a key value that you can search by. For example, our shapes have four points each. This is how we could store them in a dict and know which is which too:

shapes = {"J_shape": [Point(Point(center.x - 1, center.y),
		      Point(center.x    , center.y),
	              Point(center.x + 1, center.y),
		      Point(center.x + 1, center.y + 1)],
			  
	  "I_shape": [Point(center.x - 2, center.y),
		      Point(center.x - 1, center.y),
		      Point(center.x    , center.y),
		      Point(center.x + 1, center.y)],
		.
		.
		.
					
}

Now we can search like this shapes.get("I_shape") and this would leave us with proper points to draw the I_shape.

Make sense? If not, no fear - this week’s exercises and readings will help to clear up any questions you may have. And you can always head over to the discussion forums and ask your questions of the group there.

OK, I have these goodies for you this week:


Reading

You will find proper information about the structures in Chapter 7, Chapter 9 and Chapter 10 from the online book How to Think Like a Computer Scientist.

Trying it out

Codecademy also has some good exercises that will show us how to work with structures. Work on units called Lists & Dictionaries and Student Becomes the Teacher.

Exercises

From the MIT OCW’s course A Gentle Introduction to Programming Using Python

Project

Now we already have the board and we know what the basic shapes are, but we have to learn how to write a code to make them. Could you help me to make my board look like this: Tetrominoes

I saw the color can be changed with code like this: rectangle.setFill('deep pink') and the outline width can be set like this: rectangle.setWidth(3)

Please come to our discussion forums and let’s do this thing together. You can also search for people to help you solve this problem or just come and hang with the rest of us. You are always welcome!

Keep on keepin’ on!

moocie face
–Your mechanical friend MOOC-E


Next section: