class Ratio:
def __init__(self,n=None,d=None):
if d==None:
self.num = n.num
self.den = n.den
elif n==None:
self.num = 0
self.den = 1
else:
self.num = n
self.den = d
def equals(self):
return self # retuns calling object.
class Ratio:
def __init__(self,n=None,d=None):
pass
def equals(self):
pass
class Ratio:
def __init__(self,n=None,d=None):
if d==None:
self.num = n.num
self.den = n.den
elif n==None:
self.num = 0
self.den = 1
else:
self.num = n
self.den = d
z = Ratio(22,7)
y = z
x = z
x = Ratio(22,7)
y = Ratio(x)
z = x
w = x
class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
def __mul__(self,y):
pass
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=None,d=None):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
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(22,7)
y = Ratio(-3,8)
z = x
z.print_()
x = y*z
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=None,d=None):
self.num = n
self.den = d
self.reduce()
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def print_(self):
print self.num , '/', self.den
class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
self.reduce()
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def __eq__(self,y):
return (x.num * y.den == y.num * x.den)
def print_(self):
print self.num , '/', self.den
'''
Python does not use << operator for printing. So here we are just declaring function name as print_.
'''
class Ratio:
def __init__(self,n=None,d=None):
self.num = n
self.den = d
def __imul__(self,y):
self.num = self.num * y.num
self.den = self.den * y.den
def __eq__(self,y):
return (x.num * y.den == y.num * x.den)
def print_(self):
print self.num , '/', self.den
x = Ratio(22,7)
y = Ratio(-3,8)
x.print_() , y.print_()
'''
Python does not have >> for input. so we will use input function.
'''
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 __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den
def input(self):
self.num = int(raw_input('Numerator : '))
self.den = int(raw_input('Denominator : '))
self.reduce()
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()
y = Ratio()
x.input()
y.input()
x.print_()
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=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den
def input(self):
self.num = int(raw_input('Numerator : '))
self.den = int(raw_input('Denominator : '))
self.reduce()
def __float__(self):
return float(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 = 15.py
g = gcd(sgn*self.num,self.den)
self.num /= g
self.den /= g
x = Ratio(-5,8)
print "x = " ,
x.print_()
print ", float(x) = " , float(x)
P = Ratio(22,7)
PI = float(P)
print "P = " ,
P.print_()
print ", PI = " , PI
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 __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den
def __iadd__(self,n):
self.num += self.den
return self
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(22,7)
x += 1
y = x
print "y = " ,
y.print_()
print ", x = ",
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=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den
def __iadd__(self,n):
self.num += self.den
return self
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(22,7)
y = Ratio(x.num,x.den)
x += 1
print "y = " ,
y.print_()
print ", x = ",
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=1):
self.num = n
self.den = d
self.reduce()
def __mul__(self,y):
z = Ratio(self.num * y.num, self.den * y.den)
return z
def print_(self):
print self.num , '/', self.den
def __getitem__(self,k):
if k == 1:
return self.num
else:
return 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(22,7)
print "x = " ,
x.print_()
print "x[1] = " , x[1] , ", x[2] = " , x[2]