def swapargs(a,b):
temp=a[0]
a[0]=b[0]
b[0]=temp
#Variable declaration
i=[10]
j=[20]
x=[10.1]
y=[23.3]
a=['x']
b=['z']
print "Original i, j: ",i[0],j[0]
print "Original x,y: ",x[0],y[0]
print "Original a,b: ",a[0],b[0]
swapargs(i,j) #swap integers
swapargs(x,y) #swap floats
swapargs(a,b) #swap chars
#Result
print "Swapped i, j: ",i[0],j[0]
print "Swapped x,y: ",x[0],y[0]
print "Swapped a,b: ",a[0],b[0]
def myfunc(x,y):
print x,y
myfunc(10,"hi")
myfunc(0.23,10L)
def swapargs(a,b):
if isinstance(a[0],int): #integer version
temp=a[0]
a[0]=b[0]
b[0]=temp
print "Inside swapargs int specialization."
else: #generic version
temp=a[0]
a[0]=b[0]
b[0]=temp
print "Inside template swapargs."
#Variable declaration
i=[10]
j=[20]
x=[10.1]
y=[23.3]
a=['x']
b=['z']
print "Original i, j: ",i[0],j[0]
print "Original x,y: ",x[0],y[0]
print "Original a,b: ",a[0],b[0]
swapargs(i,j) #calls explicitly overloaded swapargs()
swapargs(x,y) #calls generic swapargs()
swapargs(a,b) #calls generic swapargs()
#Result
print "Swapped i, j: ",i[0],j[0]
print "Swapped x,y: ",x[0],y[0]
print "Swapped a,b: ",a[0],b[0]
def f(a,b=None):
if(b==None): #First version of f()
print "Inside f(X a)"
else: #Second version of f()
print "Inside f(X a, Y b)"
f(10) #calls f(X)
f(10,20) #calls f(X,Y)
def repeat(data,times):
while times:
print data
times-=1
repeat("This is a test",3)
repeat(100,5)
repeat(99.0/2, 4)
def myabs(val):
if val<0:
return -val
else:
return val
#Result
print myabs(-10)
print myabs(-10.0)
print myabs(-10L)
print myabs(-10.0)
def SIZE():
return 100
class queue:
def __init__(self):
self.q=[]
self.sloc=self.rloc=0
#Put an object into the queue
def qput(self,i):
if self.sloc==SIZE():
print "Queue is full."
return
self.sloc+=1
self.q.append(i)
#Get an object from the queue.
def qget(self):
if self.rloc==self.sloc:
print "Queue Underflow."
return
a=self.rloc
self.rloc+=1
return self.q[a]
#Create two integer queues
a=queue()
b=queue()
a.qput(10)
b.qput(19)
a.qput(20)
b.qput(1)
print a.qget(),
print a.qget(),
print b.qget(),
print b.qget()
#Create two double queues
c=queue()
d=queue()
c.qput(10.12)
d.qput(19.99)
c.qput(-20.0)
d.qput(0.986)
print c.qget(),
print c.qget(),
print d.qget(),
print d.qget()
class myclass:
def __init__(self,a,b):
self.__i=a
self.__j=b
def show(self):
print self.__i,self.__j
ob1=myclass(10,0.23)
ob2=myclass('X',"This is a test")
#Result
ob1.show() #Show int,double
ob2.show() #Show char.char*
def SIZE():
return 10
class atype:
def __init__(self):
self.a=[]
for i in range(SIZE()):
self.a.append(i)
#Implementing the [] overloading
def op1(self,i,j):
if (i<0 or i>=SIZE()):
print "\nIndex value of ",
print i," is out-of-bounds."
return
self.a[i]=j
def op2(self,i):
if (i<0 or i>SIZE()-1):
print "\nIndex value of ",
print i," is out-of-bounds."
return
return self.a[i]
#Variable declaration
intob=atype()
doubleob=atype()
print "Integer array: ",
for i in range(SIZE()):
intob.op1(i,i)
for i in range(SIZE()):
print intob.op2(i),
print ""
print "Double array: ",
for i in range(SIZE()):
doubleob.op1(i,float(i)/3)
for i in range(SIZE()):
print doubleob.op2(i),
intob.op1(12,100)
class atype:
def __init__(self,size):
self.a=[]
self.size=size
for i in range(size):
self.a.append(i)
#Implementing the [] overloading
def op1(self,i,j):
if (i<0 or i>self.size-1):
print "\nIndex value of ",
print i," is out-of-bounds."
return
self.a[i]=j
def op2(self,i):
if (i<0 or i>self.size-1):
print "\nIndex value of ",
print i," is out-of-bounds."
return
return self.a[i]
#Variable declaration
intob=atype(10)
doubleob=atype(15)
print "Integer array: ",
for i in range(10):
intob.op1(i,i)
for i in range(10):
print intob.op2(i),
print ""
print "Double array: ",
for i in range(15):
doubleob.op1(i,float(i)/3)
for i in range(15):
print doubleob.op2(i),
intob.op1(12,100) #generates runtime error
class atype:
def __init__(self,size=10):
self.a=[]
for i in range(size):
self.a.append(i)
#Implementing the [] overloading
def op1(self,i,j):
if (i<0 and i>SIZE()-1):
print "Index value of "
print i," is out-of-bounds."
exit(1)
self.a[i]=j
def op2(self,i):
if (i<0 and i>SIZE()-1):
print "Index value of "
print i," is out-of-bounds."
exit()
return self.a[i]
#Variable declaration
intob=atype(100)
doubleob=atype()
defarray=atype()
print "Integer array: ",
for i in range(100):
intob.op1(i,i)
for i in range(100):
print intob.op2(i),
print ""
print "Double array: ",
for i in range(10):
doubleob.op1(i,float(i)/3)
for i in range(10):
print doubleob.op2(i),
print ""
print "Defarray array: ",
for i in range(10):
doubleob.op1(i,i)
for i in range(10):
print doubleob.op2(i),
class myclass:
def __init__(self,a):
if isinstance(a,int):
print "Inside myclass<int>specialization"
self.__x=a*a
else:
print "Inside generic myclass"
self.__x=a
def getx(self):
return self.__x
d=myclass(10.1)
print "double: ",d.getx(),"\n"
i=myclass(5)
print "int: ",i.getx()