Chapter 13: Templates and Iterators

Example 13.1, Page no: 300

In [1]:
def swap(x,y):
    x[0],y[0] = y[0],x[0]

m = [22]
n = [66]
swap(m, n)
s1 = ["John Adams"]
s2 = ["James Madison"]
swap(s1, s2)
x = [22/7]
y = [-3]
swap(x, y)

Example 13.2, Page no: 301

In [2]:
def sort(v,n):
    for i in range(1,n):
        for j in range(n-i):
            if v[j] > v[j+1]:
                v[j],v[j+1] = v[j+1],v[j]

def print_( v,n):
    for i in range(n):
        print v[i],
    print ""
    
a = [55, 33, 88, 11, 44, 99, 77, 22, 66]
print_(a,9);
sort(a,9)
print_(a,9)
s = ["Tom", "Hal", "Dan", "Bob", "Sue", "Ann", "Gus"]
print_(s,7)
sort(s,7)
print_(s,7)
55 33 88 11 44 99 77 22 66 
11 22 33 44 55 66 77 88 99 
Tom Hal Dan Bob Sue Ann Gus 
Ann Bob Dan Gus Hal Sue Tom 

Example 13.3, Page no: 303

In [3]:
class Stack:
    def __init__(self,s=100):
        self.size = s
        self.top = -1
        self.data = []
    def push(self,x):
        self.data.append( x)
        self.top += 1
    def pop(self):
        d = self.data[self.top]
        self.data.pop(self.top)
        self.top -= 1
        return d        
    def isEmpty(self):
        return self.top == -1
    def isFull(self):
        return self.top==self.size-1
        
intStack1 = Stack(5)
intStack2 = Stack(10)
charStack = Stack(8)
intStack1.push(77)
charStack.push('A')
intStack2.push(22)
charStack.push('E')
charStack.push('K')
intStack2.push(44)
print intStack2.pop() 
print intStack2.pop() 
if (intStack2.isEmpty()):
    print "intStack2 is empty."
44
22
intStack2 is empty.

Example 13.4, Page no: 305

In [4]:
'''
= operator does work in python by defaultly.
'''
class Vector:
    def __init__(self,n=None):
        if type(n) == int :
            self.size = n
            self.data = []
        else:
            self.size = 8
            self.data = []
        for i in range(self.size):
            self.data.append(0)
v = Vector()
v.data[5] = 127
w = v
x = Vector(3)
print w.size
8

Example 13.5, Page no: 306

In [5]:
class Vector:
    def __init__(self,n=None):
        if type(n) == int :
            self.size = n
            self.data = []
        else:
            self.size = 8
            self.data = []
        for i in range(self.size):
            self.data.append(0)

class Array(Vector):
    def __init__(self,i,j):
        Vector.__init__(self,j-i+1)
        self.i0= i
    def __setitem__(self,k,v):
        self.data[k-self.i0] = v
    def __getitem__(self,i):
        return self.data[self.i0-i]
    def firstSubscript(self):
        return self.i0
    def lastSubscript(self):
        return self.i0+self.size-1

x = Array(1,3)
x.data[0] = 3.14159
x.data[1] = 0.08516
x.data[2] = 5041.92
print "x.size() = " , x.size 
print "x.firstSubscript() = " , x.firstSubscript() 
print "x.lastSubscript() = " , x.lastSubscript()
for i in range(0,3):
    print "x[" , i + 1 , "] = " , x.data[i]
x.size() =  3
x.firstSubscript() =  1
x.lastSubscript() =  3
x[ 1 ] =  3.14159
x[ 2 ] =  0.08516
x[ 3 ] =  5041.92

Example 13.6, Page no: 308

In [6]:
class Matrix:
    def __init__(self,r=1,c=1):
        self.rows = r
        self.columns = c
        self.vector = []
        for i in range(r):
            a = []
            for j in range(c):
                a.append(0)
            self.vector.append(a)

a = Matrix(2,3)
a.vector[0][0] = 0.0
a.vector[0][1] = 0.1
a.vector[0][2] = 0.2
a.vector[1][0] = 1.0
a.vector[1][1] = 1.1
a.vector[1][2] = 1.2

print "The matrix a has " , a.rows , " rows and ", a.columns , " columns:"
for i in range(2):
    for j in range(3):
        print a.vector[i][j] ,
    print ""
The matrix a has  2  rows and  3  columns:
0.0 0.1 0.2 
1.0 1.1 1.2 

Example 13.7, Page no: 309

In [7]:
friends = []


friends.insert(0,"Bowen, Van")
friends.insert(0,"Dixon, Tom")
friends.insert(0,"Mason, Joe")
friends.insert(0,"White, Ann")

for i in range(len(friends)):
    print friends[i], '->' ,
print '*'
friends.remove('White, Ann')
print "Removed: " , 'White, Ann'
for i in range(len(friends)):
    print friends[i], '->' ,
print '*'
White, Ann -> Mason, Joe -> Dixon, Tom -> Bowen, Van -> *
Removed:  White, Ann
Mason, Joe -> Dixon, Tom -> Bowen, Van -> *

Example 13.8, Page no: 313

In [8]:
friends = []
friends.append("Bowen, Van")
friends.append("Dixon, Tom")
friends.append("Mason, Joe")
friends.append("White, Ann")
for i in range(len(friends)):
    print friends[i], '->' ,
print '*'

friends.remove("Mason, Joe")
friends[1] = "Davis, Jim"
for i in range(len(friends)):
    print friends[i], '->' ,
print '*'

friends.insert(2,"Morse, Sam")
for i in range(len(friends)):
    print friends[i], '->' ,
print '*'

for i in range(len(friends)):
    print "[" ,friends[i] , "]" , '->' ,
print '*'
Bowen, Van -> Dixon, Tom -> Mason, Joe -> White, Ann -> *
Bowen, Van -> Davis, Jim -> White, Ann -> *
Bowen, Van -> Davis, Jim -> Morse, Sam -> White, Ann -> *
[ Bowen, Van ] -> [ Davis, Jim ] -> [ Morse, Sam ] -> [ White, Ann ] -> *