0% found this document useful (0 votes)
0 views1 page

Dsaa 64

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views1 page

Dsaa 64

Uploaded by

farawayfromhere
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

2.

6 A Proof by Induction 49

1 class PyList:
2 def __init__(self):
3 self.items = []
4
5 # The append method is used to add commands to the sequence.
6 def append(self,item):
7 self.items = self.items + [item]
8
9 ...
Listing 2.3 Inefficient Append

The code in Listing 2.3 appends a new item to the list as follows:

1. The item is made into a list by putting [ and ] around it. We should be careful
about how we say this. The item itself is not changed. A new list is constructed
from the item.
2. The two lists are concatenated together using the + operator. The + operator is
an accessor method that does not change either original list. The concatenation
creates a new list from the elements in the two lists.
3. The assignment of self.items to this new list updates the PyList object so it now
refers to the new list.

The question we want to ask is, how does this append method perform as the size
of the PyList grows? Let’s consider the first time that the append method is called.
How many elements are in the list that is referenced by self.items? Zero, right? And
there is always one element in [item]. So the append method must access one element
of a list to form the new list, which also has one element in it.
What happens the second time the append method is called? This time, there is
one element in the list referenced by self.items and again one element in [item].
Now, two elements must be accessed to form the new list. The next time append is
called three elements must be accessed to form the new list. Of course, this pattern
continues for each new element that is appended to the PyList. When the nth element
is appended to the sequence there will have to be n elements copied to form the new
list. Overall, how many elements must be accessed to append n elements?

2.6 A Proof by Induction

We have already established that accessing each element of a list takes a constant
amount of time. So, if we want to calculate the amount of time it takes to append n
elements to the PyList we would have to add up all the list accesses and multiply by
the amount of time it takes to access a list element plus the time it takes to store a
list element. To count the total number of access and store operations we must start
with the number of access and store operations for copying the list the first time an
element is appended. That’s one element copied. The second append requires two

You might also like