Theory
New library functions
Counting the number of tokens in a cell is such a common task in this level that we have added two functions to the library:
= count_and_take() # takes all tokens and returns their number = count() # counts tokens but does not take them
These functions work correctly regardless of the number of tokens that Karel already has in its bag. They are:
def count_and_take(): total = 0 while token_here(): take() total = total + 1 return total
def count_tokens(): total = count_and_take() repeat( put, total ) return total
We have also added step_back( n ) that has Karel step back a given number of steps, which will be useful in a number of missions in this level:
def step_back( n ): turn_around() repeat( move, n ) turn_around()
arrays
We already saw that a variable is a placeholder with a name and a value. For example:
friend = "John" print( friend )
An array is a placeholder with a name and multiple values; we tell the values apart using indices. For example:
friend = ["John", "Peter", "Susan", "Mark"] print( friend[0] ) print( friend[1] ) print( friend[2] ) print( friend[3] )
matrices
In the same way in which we can think of an array as a stack of variables, we can think of a matrix as a stack of arrays, i.e., a matrix is an array whose elements are arrays. Hence, arrays can represent values along one dimension while matrices can represent values along two dimensions, arranged in a grid.
tic_tac_toe_board = [ [ 0, 1, 0 ], [ 1, 1, 0 ], [ 0, 0, 1 ] ] print( tic_tac_toe_board ) # all the grid print( tic_tac_toe_board[ 1 ] ) # the 2nd row of the grid print( tic_tac_toe_board[ 1 ][ 0 ] ) # the 1st element of the 2nd row
Since the matrix is a column of arrays, we can print any row with a single print() command, but we cannot print a column with a single print() command.
Training Missions
7.TM1 Strawberries – soil analysis
Karel got the results of a soil analysis of his strawberry patch. To obtain the best strawberries he needs to plant this many plants in each of its 7-cell garden:
cell | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|---|
plants | 8 | 6 | 3 | 5 | 6 | 8 | 5 |
Answer: show it to me
Programs can handle arrays of any size with ease because we can ‘walk’ along them using a for loop.
def announce( cell, num ): m = "I put " + str(num) m = m + " berries in cell " m = m + str( cell ) say( m ) def plant_berries( n, a ): for i in range( 0, n ): move() repeat( put, a[i] ) announce( i, a[i] ) # main program size = 7 patch = [8, 6, 3, 5, 6, 8, 5] plant_berries( size, patch ) move()
7.TM2 Olympic dreams – Shot put
Now that the winter olympics are over, Karel is helping with the summer olympic team. Today he is helping the shot putters. He has to go into the field, look for shots, and alert the judges with the coordinates of the cells that have a shot in it. He does not need to pick the shots; the judges will do that. Easy enough. The field is 3 meters wide and 5 meters long.
Answer: show it to me
from my_lib import * def go_to_top( nr ): step_back( nr ) sidestep_left() def search( court, nr, nc ): for c in range( 0, nc ): for r in range( 0, nr ): move() if token_here(): announce( r, c ) go_to_top( nr ) def announce(r, c): m = "I found one at row " m = m + str( r ) m = m + " and col " + str( c ) say( m ) # main program rows = 3 cols = 5 court = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]] search( court, rows, cols )
7.1 Strawberries – Storm
A storm is coming! a storm is coming! We do not have time to replant each patch of strawberries individually. Pick them all up on one go but remember the number of plants in each cell and duplicate the patten in the fenced area. Then take Karel home to get some hot cocoa.
Answer: show it to me
We have to pick up all the strawberries as we move forward. We can set up an array and store the number of strawberries in each cell. When we replant the strawberries, we retrieve the values from the array.

ready…

phew!
7.2 Life in SoCal – the tar pits
★★ Work it out
Karel is volunteering at the Los Angeles Tar pits, an archaeological area full of fossils of sable-tooth tigers and mammoths.
Karel has to move all the bones found in a 3 x 4 rectangular area of the park to its new home inside the walls of the museum. Once Karel enters the building, he cannot go back for more bones so he has to pick them up all of them, at once, and remember the number of bones in each cell. Please help Karel.
Answer: Show it to me
7.3 Life in SoCal – Space shuttle
★★★ Work it out
NASA has finally graduated Karel as an Astro-droid and given him command of a space shuttle.. a little one, though. Please, have Karel load the equipment in the shuttle, in the same order in which it was laid out. As soon as Karel gets in the shuttle, the doors close and he cannot get out so he has to move all the equipment in a single pass.
“Ah… this is the same as the museum mission… piece of cake”. Hum.. is it? Probably not, otherwise it would not be worth 3 stars.
Answer: Show it to me
Optional Mission – Does not give a star
7.4 The rooms of doom – Angkor Vat
★★★★ Work it out
We are in Cambodia, looking at the beautiful structure of Angkor-Vat, mirrored on a lake. Following the structure, the challenge of the room of Doom is simple: mirror the pattern of tokens of the first room on the second and then you are allowed to go home. Once we go from the first room into the second one, we are not allowed back into the first room.
Answer: Show it to me