Data Structures - Lab 05

Tuesday, Sept. 22n

Introduction to Linear Collections


Introduction

In the last two weeks I have allowed you to complete your lab with a partner.  This week I would like you to return to working as individuals.

In this week's lab you will be working very closely with material in your textbook.  Feel free to use your textbook as a guide to help explain ideas or answer questions.  However, please realize that I may take a slightly different approach than the does your book, so pay attention to what is written here in the lab.


Part A : Basic operations on the array

To begin, download the file arrays.py which contains the code for an Array class.  This is the same code that is presented on page 515 in your textbook.  If you have not already done so, please make sure that you read the introductory material to section 13.2 - Arrays.

 

Go on, do it.   I'll wait.

 

Create an Array and invoke the following commands:

>>> a = Array(10)
>>> len(a)
>>> print a
>>> a[0]="Dr"
>>> a[1]="Schafer"
>>> a[2]="is"
>>> a[3]="#"
>>> a[4]=1
>>> a[8]="later"
>>> print a
>>> print a[2]

[Q1]  What is the result of typing "len(a)"?

[Q2]  What is the result of typing "print a" the first time?

[Q3]  What is the result of typing "print a" the second time?

[Q4]  What is the result of typing "print a[2]"?

[Q5]  PREDICT what will happen if you type "a[1+2]=a[1]+a[2]"

[Q6]  Do it, then type "print a" and see what happened.  Were you right?  If not, what happened and why were you wrong?

[Q7]  PREDICT what will happen if you type "a[len(a)] = "the end"

[Q8]  Do it.  Were you right?  If not, what happened and why were you wrong?

 

Above, you created an array by explicitly passing it one parameter.  Create an Array using the two parameter version of the constructor by invoking the following commands [Reminder  : technically, these two constructors are the "two parameter" and "three parameter" versions.  The first parameter in every constructor is the "self" variable.  Since we don't see this variable when we use it I don't count it when talking about constructors/methods]:

>>> b = Array(10,5)
>>> len(b)
>>> print b

[Q9]  What is different about the way that the two parameter version works?

[Q10]  Look at the code in arrays.py.  What is in the code that allows this program to accept both a one parameter and a two parameter version?

 

If you have not done so, read section 13.2.2 about Random Access and Contiguous Memory.

[Q11]  How many machine operations are required to access the data in slot zero (in our last example, b[0])?

[Q12]  How many machine operations are required to access the data in the last slot (in our last example, b[9])?

 

Section 13.2.4 talks about physical size and logical size.  Just to review, type

>>> print a

[Q13]  What is the current physical size of this array?  What is the logical size?


Part B : Modifying the Array class

  1. Add two instance variables to the class

 

  1. Add two new methods to array.py  [Hint, use section 13.3 as a guideline although this code is not immediately usable].

For example, to test this I might type

>>> a = Array(5)
>>> len(a)
5
>>> a[1]="Hello"
>>> print a
[ None, "Hello", None, None, None ]
>>> a.grow(8)
>>> print a
[ None, "Hello", None, None, None, None, None, None ]


For example, to test this I might type

>>> a = Array(8)
>>> len(a)
8
>>> a[1]="Hello"
>>> print a
[ None, "Hello", None, None, None, None, None, None ]
>>> a.shrink(5)
>>> print a
[ None, "Hello", None, None, None ]

[SIG1]  When you have these four additions implemented you should call me over to demo these features.


Part C : If you get done early...

Some of you will get done with this very quickly while others of you will require the whole period.  If you get done early I strongly suggest you stick around and work on HW#2.  This way if you have questions I am available to provide answers.