class Ratio:
def __init__(self):
self.num = 0
self.den = 0
def assign(self,n,d):
self.num = n
self.den = d
def convert(self):
return float(self.num)/self.den
def invert(self):
self.num,self.den = self.den,self.num
def print_(self):
print self.num , '/' , self.den ,
x = Ratio()
x.assign(22,7)
print "x = ",
x.print_()
print " = " , x.convert()
x.invert()
print "1/x = ",
x.print_()
class Ratio:
def __init__(self):
self.num = 0
self.den = 0
def assign(self,n,d):
self.num = n
self.den = d
def convert(self):
return float(self.num)/self.den
def invert(self):
self.num,self.den = self.den,self.num
def print_(self):
print self.num , '/' , self.den ,
class Ratio:
def __init__(self,n,d):
self.num = n
self.den = d
def print_(self):
print self.num , '/' , self.den ,
x = Ratio(-1,3)
y = Ratio(22,7)
print "x = ",
x.print_()
print " and y = ",
y.print_()
class Ratio:
def __init__(self,n=None,d=None):
if n==None:
self.num = 0
self.den = 1
elif d==None:
self.num = n
self.den = 1
else:
self.num = n
self.den = d
def print_(self):
print self.num , '/' , self.den ,
x = Ratio()
y = Ratio(4)
z = Ratio(22,7)
print "x = ",
x.print_()
print "\ny = ",
y.print_()
print "\nz = ",
z.print_()
class Ratio:
def __init__(self,n=None,d=None):
if n==None:
self.num = 0
self.den = 1
elif d==None:
self.num = n
self.den = 1
else:
self.num = n
self.den = d
def print_(self):
print self.num , '/' , self.den ,
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
def print_(self):
print self.num , '/' , self.den ,
x = Ratio()
y = Ratio(4)
z = Ratio(22,7)
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
def numerator(self):
return self.num
def denominator(self):
return self.den
def print_(self):
print self.num , '/' , self.den ,
x = Ratio(22,7)
print x.numerator() , '/' , x.denominator()
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=1):
self.num = n
self.den = d
self.reduce()
def numerator(self):
return self.num
def denominator(self):
return self.den
def print_(self):
print self.num , '/' , self.den ,
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g
x = Ratio(100,-360)
x.print_()
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=None):
if d == None:
self.num = n.num
self.den = n.den
else:
self.num = n
self.den = d
self.reduce()
def numerator(self):
return self.num
def denominator(self):
return self.den
def print_(self):
print self.num , '/' , self.den ,
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g
x = Ratio(100,360)
y = Ratio(x)
print "x = ",
x.print_()
print "y = ",
y.print_()
def gcd(m,n):
# returns the greatest common divisor of m and n:
if (m<n):
m,n = n,m
while (n>0):
r = m % n
m = n
n = r
return m
class Ratio:
def __init__(self,n=0,d=None):
if d == None:
print "COPY CONSTRUCTOR CALLED"
self.num = n.num
self.den = n.den
else:
self.num = n
self.den = d
self.reduce()
def numerator(self):
return self.num
def denominator(self):
return self.den
def print_(self):
print self.num , '/' , self.den ,
def reduce(self):
# enforce invariant(den > 0):
if (self.num == 0 or self.den == 0):
self.num = 0
self.den = 1
return
if (self.den < 0):
self.den *= -1
self.num *= -1
# enforce invariant(gcd(num,den) == 1):
if (self.den == 1):
return
# it's already reduced
sgn = 0
if self.num < 0:
sgn = -1
else:
sgn = 1
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g
def f(r):
s = Ratio(r)
x = Ratio(22,7)
y = Ratio(x) #calls the copy constructor, copying x to y
f(y)
'''
Note : Python objects die when program gets exit.
'''
class Ratio:
def __init__(self):
print "OBJECT IS BORN."
def __del__(self):
print "OBJECT DIES."
x = Ratio()
print "Now x is alive."
print "Now between blocks."
y = Ratio()
print "Now y is alive."
class X:
def __init(self):
data = 0
p = X()
p.data = 22
print "p.data = " , p.data , " = " , p.data
p.data = 44
print " p.data = " , p.data , " = " , p.data
class Node:
def __init__(self,d,q=None):
self.data = d
self.next = q
n = int(raw_input())
q = Node(n)
while True:
n = int(raw_input())
if n<=0:
break
p = Node(n, q)
q = p
k = p
while k != None:
print k.data , '->' ,
k = k.next
print '*'
'''
Python does not support static data type.
Python automatically handles local variable so we need not to delete it.
'''
count = 0
class Widget:
def __init__(self):
global count
count += 1
w = Widget()
x = Widget()
print "Now there are " , count , 'widgets'
if True:
w = Widget()
x = Widget()
y = Widget()
z = Widget()
print "Now there are" , count , 'widgets'
print "Now there are " , count , 'widgets'
y = Widget()
print "Now there are " , count , 'widgets'
count = 0
class Widget:
def __init__(self):
global count
count += 1
def numWidgets(self):
global count
return count
w = Widget()
x = Widget()
print "Now there are " , w.numWidgets() , 'widgets'
if True:
w = Widget()
x = Widget()
y = Widget()
z = Widget()
print "Now there are" , w.numWidgets() , 'widgets'
print "Now there are " , w.numWidgets() , 'widgets'
y = Widget()
print "Now there are " , w.numWidgets() , 'widgets'
count = 0
class Widget:
def __init__(self):
global count
count += 1
def numWidgets(self):
global count
return count
w = Widget()
x = Widget()
print "Now there are " , w.numWidgets() , 'widgets'
if True:
w = Widget()
x = Widget()
y = Widget()
z = Widget()
print "Now there are" , w.numWidgets() , 'widgets'
print "Now there are " , w.numWidgets() , 'widgets'
y = Widget()
print "Now there are " , w.numWidgets() , 'widgets'