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)
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)
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."
'''
= 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
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]
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 ""
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 '*'
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 '*'