Press enter to skip the top menu

Robotics and Programming

Lists

Introduction

Lists can be looked at as extensions of variables and, therefore, before looking at lists we shall revise what we have already learnt about variables. The main features of variables that we are familiar with include:

We have mentioned those facts about variables in order to address many misconceptions learners have about them (Sorva, 2018) and as lists are extensions of variables we want to ensure that learners won't encounter lists, while having misconceptions about variables (Robins, 2019). For this reason we shall again take a look at the first program where we introduced variables.

Listing 1
                       
                            movingDistance = 20
                            Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                            Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                            movingDistance+=20
                            Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                            Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                            movingDistance+=20
                            Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                            Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                            movingDistance+=20
                            Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                            Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                       
                    

The code above is simply a copy of Listing 3 from the page Variables. It is the first program in which we introduced the concept of variables and we shall therefore discuss it in relation to the issues with and misconceptions about variables that we have noted above.

At line 1 the system is requested to reserve space in memory to store an integer value. (We know it is an integer because there is no decimal point in the number.) The value 20 is stored in this space and throughout the program's run this memory space will be referred to as movingDistance

At line 2 we want the robot to move forward. The distance it is meant to move is the value stored in movingDistance. Thus the robot will move forward 20 cm.

Line 4 adds 20 to the value already stored in movingDistance. This is achieved by copying the current value of movingDistance and the value 20 to the CPU, where the two values are added and the result 40 is stored back in movingDistance. This means that when control moves to line 5 the robot will now move forward by a distance of 40cm, since that is the value currently stored in movingDistance.

At line 7 the value of movingDistance is increased to 60 in the same way as we described at line 4 and therefore at line 8 the robot moves forward 60cm.

Finally at line 10 movingDistance is increased to 80 causing the robot to move 80cm at line 11.

Above, when we were revising the properties of variables, we stated that once a new value is stored in a variable the previous value is lost. Referring to Listing 1, this means that once line 10 finishes execution movingDistance will have a value of 80. All the previous values of 20, 40 and 60 are irretrievably lost for the simple reason that values are stored in memory by turning individual memory circuits on or off, where off is represented by zero and on by 1.

Figure 1

The image above shows a representation of how the values 20, 40, 60 and 80 are stored in a computer's memory as binary digits 1 and 0, each digit being represented by either a circuit being turned on or off. Once the on/off arrangement of the circuits are rearranged when a new value is stored, there is no way of getting the previous arrangement back. Therefore an individual variable has neither access to its own previous values, nor can it store multiple values.

So is there a way of associating a number if different values with the same variable name? Yes there is but it is not referred to as a variable but as a List!

Go to top

When do we need Lists?

Values changing through Calculation

Listing 2
                         
                                movingDistance = 20
                                for loopCounter in range(4):
                                    Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                                    Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                                    movingDistance+=20                           
                        

We have met listing 2 before. It is simply listing 1 adapted to a for loop and for comparison purposes we shall follow its processing as far as the first iteration of the for loop

Just like listing 1, movingDistance is set to 20 and then the loop is activated with loopCounter having a value of 0. In the body of the loop the robot moves forward 20cm. At line 5 movingDistance is increased to 40, which, when the control goes back to line 3 will cause the robot to move forward 40cm.

As the loop iterates movingDistance will be increased to 60 and then to 80 resulting in the robot moving forward 60cm in the third iteration and 80 in the fourth iteration.

What Listing 1 and listing 2 have in common is that once movingDistance is initialised at line 1 of both programs, its next value can be calculated from its current value, mo matter how many times the loop iterates.


Values changing randomly

Listing 3
                                movingDistance = 44
                                Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                                Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                                movingDistance =70
                                Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                                Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                                movingDistance = 63
                                Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                                Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                                movingDistance = 38
                                Ed.Drive(Ed.FORWARD, Ed.SPEED_4, movingDistance)
                                Ed.Drive(Ed.SPIN_LEFT, Ed.SPEED_4, 90)
                        

At first glance the above piece of code looks like that in Listing 1, but if we look closer at lines 1, 4, 7 and 10 we see that the different values of movingDistance bear no relationship to each other. Those values, 44, 70, 63 and 38, are actually random numbers generated by a random number generator. Since there is no relationship between those numbers we cannot calculate one value based on the previous value. Thus, with our current state of programming knowledge, if we wanted our robot to perform 20 move forward and turns where all the forward distances were random numbers we would need to have a 60 line program, i.e. a program that would be 5 time longer than listing 1. This is not acceptable. So to solve this problem we need to use lists.


What is a list?

In our case, when programming the Edison robot a list would be a list of random numbers. Our set of random numbers above an be converted into a list that EdPy can understand by enclosing the numbers in square brackets as follows:

[44, 70, 63, 38].

This list has four elements in it. We can think of each element as a variable. The reason is that everything we said about individual variables above applies to elements of a list:

  • A list element is a small block of memory that is reserved to store one value.
  • A list element can store only one value at any one time.
  • If a new value is stored in a list element the previous value is lost and cannot be retrieved.

If you compare the above to the features of variables that we looked at in the introduction above they are almost identical except for the naming. Elements of a list don't have names; only the list itself has a name. The individual elements of a list have indexes, starting at 0. Thus if we were to name our list as distnc then the list and its elements could be visualised as shown below.

Here we see that only the list itself have a name and that each element has a unique index beginning at zero. Thus element 0 has a value of 44 and element 1 a value of 70. Similarly we can say that 63 is element 2 and 38 is element 3. This can be written more programmatically as follows:

distnc[0] == 44

distnc[1] == 70

distnc[2] == 63

distnc[3] == 38


Lists: Desk Exercises

Above we have examples of four lists, each one with a different number of elements. Using those lists you are required to fill in the table below. Two cells are already filled in. The first indicates that Distances[5] has a value of 84, which if you check above is correct. Fill in the other values.

Download a copy of the exercises
Go to top

Programming with Lists

A simple Program

Listing 4
                           
                                distnc = Ed.List(4,[44,70,63,38])
                                size = len(distnc)
                                for loopCounter in range(size):
                                    Ed.Drive(Ed.FORWARD, 4, distnc[loopCounter])
                                    Ed.Drive(Ed.SPIN_LEFT, 4, 90)                          
                        

The above code is based on Listing 3 and Listing 4 from the page Iteration. The only difference between it and Listing 4 is that elements of a list are used to determine the forward movement of the robot.

At line 1 the list is created. The name of the list is distnc (which is an abbreviation of 'distance'). It is created using the function Ed.List(). In this case the function is given two parameters: 4 and [44,70,63,38]. The first parameter indicates the number of elements the list can store, while the second parameter supplies the values that are to be stored in the list. Remember that the number of elements in the list must be equal the first parameter.

The second line creates a variable named size and stores the number of elements in the list there. For getting the number of elements in the list it uses the function len(). In this case it will return a value of 4.

At line 3 we meet the for loop:

for loopCounter in range(size):

This is the first time we have encountered a for loop where the parameter of range() is a variable instead of an integer constant. The parameter, of course, is the variable size, and since that has been given a value of 4 at line 2 then line 3 is equivalent to

for loopCounter in range(4):

..a format that is already familiar to us. Our loop will iterate four times.

Line 4 is where we meet our first sticky bit:

Ed.Drive(Ed.FORWARD, 4, distnc[loopCounter])

We know that the beginning of the line means 'drive the robot forward at speed 4...', but what is the meaning of

distnc[loopCounter]

Since this is our first iteration through the loop the variable loopCounter will have a value of 0 and thus

distnc[loopCounter] will be equivalent to distnc[0], which will be 44.

Thus the robot will drive forward for 44cm.

On the second iteration of the loop loopCounter will have a value of 1. Thus distnc[loopCounter] will be equivalent to distnc[1] which will have a value of 70. Thus at line 3 the robot will drive forward 70cm.

On the third iteration of the loop loopCounter will have a value of 2. Thus distnc[loopCounter] will be equivalent to distnc[3] which will have a value of 63. Thus at line 3 the robot will drive forward 70cm.

For the reasons we have given above on the fourth iteration of the loop the robot will drive forward 38cm.

Below is a tracing table and a notional machine.

Figure 1: A tracing table for Listing 4
A notional machine to illustrate the concept of lists

Lists Exercise

Exercise 1: Modify Listing 4 so that each forward movement is 30cm but the turning angles in degrees will be 120, 80, 110 and 90.


Go to top

More list processing

Moving through the list backwards

Previously we processed a list starting at the first element, i.e. element zero and then moving on to elements 1, 2 and 3 in turn. We are not restricted, however, to moving from the first to the last element; we can also move from the last to the front. To demonstrate this we shall extend Listing 4 so that once it has completed its outward journey it will reverse back over the same route.

Listing 5
                      
                                distnc = Ed.List(4,[44,70,63,38])
                                size = len(distnc)
                                for loopCounter in range(size):
                                    Ed.Drive(Ed.FORWARD, 4, distnc[loopCounter])
                                    Ed.Drive(Ed.SPIN_LEFT, 4, 90)
                                turnKey = size - 1
                                for loopCounter in range(size):
                                    listIndex = turnKey - loopCounter
                                    Ed.Drive(Ed.BACKWARD, Ed.SPEED_4, distnc[listIndex])
                                    Ed.Drive(Ed.SPIN_RIGHT, 4, 90)                          
                        

The first five lines of Listing 5 should be familiar to you; they are simply a copy of Listing 4. The for loop that spans lines 3 to 5 uses the value of loopCounter to access each element of the list distnc in turn and use the value found there to determine how far forward the robot travels. Again recall that loopCounter starts at 0 and for each subsequent iteration goes up to 1, 2 and finally 3. Our goal, however, in this example is to get another for loop

Go to top

Summary

Lists are constructs that can hold a number of values. In our examples above our lists contained only four elements. In EdPy lists can have a maximum or 250 elements, while in standard Python a list can have up to 536,870,912 elements!

In our examples the list elements hold only integer values

Only the list itself has a name and the individual elements of the list are accessed by their index values. Those index values start at zero.

Go to top

Revision

Multi choice

Fill in the Blanks

Go to top

Assignment Part 3

The work requested here will contribute to your grade in AS91883 Create a computer program. As more concepts are studied the work on this assignment will be either extended, or else modified to incorporate facilities provided by the other concepts.

NCEA requirements

A computer program uses:

  • variables storing at least two types of data (e.g. numeric, text, Boolean)
  • sequence, selection and iteration control structures
  • input from a user, sensors or another external source
  • and one or more of:

  • data stored in collections (e.g. lists, arrays, dictionaries)
  • user-defined methods, functions or procedures.

Sequence, variables and loops are used in this assignment, but the main focus is the if and if..else constructs of the Selection concept and data read from light sensors, light tracking sensors and keypad.

Brief

Using drawing tools and an A1 sized page create a multi sided shape with at least six sides. All sides must have different lengths. Also all turning angles must be between 45 degrees and 150 degrees and no two angles must have the same value.

Part 1: Develop a program that will be enable the robot to trace your drawing from start to finish correctly, including lengths and turning angles.

Part 2: Extend the above program so that the robot can retrace its journey back to the start.

Part 3: Develop a new program that will make the robot keep moving forward until it detects an obstacle. Once an obstacle is detected the distance traveled by the robot is stored in an element of a list and then the robot makes a 90 degree left turn. This is repeated four times. Once the fourth part of the robot's journey is complete, the robot retraces is journey back to the beginning, using the data stored in the list.

The following evidence of completion must be supplied:

  • Algorithms for code. You may supply separate algorithms for each component of your code
  • Copy of the code
  • Video of the robot executing the programs
  • Main components of the code commented as well as an overall comment
  • Written documentation of how you completed your project

Marking Scheme

Assignment ComponentsGrade
Robot can follow line accuratelyA
Robot stops when obstacle detectedA
Robot does not restart until obstacle removedA
Lights are on inside tunnel onlyA
Video of the running program submittedA
Some valid comments in the codeA
Basic documentation suppliedA
Robot plays a beep while waiting for removal of obstacleM
Either the main components or the overall comment are providedM
Robot plays a series of beeps ever 2 seconds while waitingE
Both the main components and the overall comment are providedE
Go to top