<s> '''<STR_LIT>''' <EOL> from types import * <EOL> def gcd ( a , b ) : <EOL> '''<STR_LIT>''' <EOL> while b : <EOL> a , b = b , a % b <EOL> return a <EOL> def rat ( num , den = <NUM_LIT:1> ) : <EOL> if isinstance ( num , complex ) or isinstance ( den , complex ) : <EOL> return complex ( num ) / complex ( den ) <EOL> if isinstance ( num , float ) or isinstance ( den , float ) : <EOL> return float ( num ) / float ( den ) <EOL> return Rat ( num , den ) <EOL> class Rat : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , num , den = <NUM_LIT:1> ) : <EOL> if den == <NUM_LIT:0> : <EOL> raise ZeroDivisionError , '<STR_LIT>' <EOL> if ( isinstance ( num , complex ) or <EOL> isinstance ( den , complex ) ) : <EOL> self . __num = complex ( num ) / complex ( den ) <EOL> self . __den = complex ( <NUM_LIT:1> ) <EOL> return <EOL> if isinstance ( num , float ) or isinstance ( den , float ) : <EOL> self . __num = float ( num ) / float ( den ) <EOL> self . __den = <NUM_LIT:1.0> <EOL> return <EOL> if ( isinstance ( num , self . __class__ ) or <EOL> isinstance ( den , self . __class__ ) ) : <EOL> new = num / den <EOL> if not isinstance ( new , self . __class__ ) : <EOL> self . __num = new <EOL> if isinstance ( new , complex ) : <EOL> self . __den = complex ( <NUM_LIT:1> ) <EOL> else : <EOL> self . __den = <NUM_LIT:1.0> <EOL> else : <EOL> self . __num = new . __num <EOL> self . __den = new . __den <EOL> else : <EOL> g = gcd ( num , den ) <EOL> self . __num = num / g <EOL> self . __den = den / g <EOL> try : <EOL> numi = int ( self . __num ) <EOL> deni = int ( self . __den ) <EOL> except ( OverflowError , TypeError ) : <EOL> pass <EOL> else : <EOL> if self . __num == numi and self . __den == deni : <EOL> self . __num = numi <EOL> self . __den = deni <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __num , self . __den ) <EOL> def __str__ ( self ) : <EOL> if self . __den == <NUM_LIT:1> : <EOL> return str ( self . __num ) <EOL> else : <EOL> return '<STR_LIT>' % ( str ( self . __num ) , str ( self . __den ) ) <EOL> def __add__ ( a , b ) : <EOL> try : <EOL> return rat ( a . __num * b . __den + b . __num * a . __den , <EOL> a . __den * b . __den ) <EOL> except OverflowError : <EOL> return rat ( long ( a . __num ) * long ( b . __den ) + <EOL> long ( b . __num ) * long ( a . __den ) , <EOL> long ( a . __den ) * long ( b . __den ) ) <EOL> def __radd__ ( b , a ) : <EOL> return Rat ( a ) + b <EOL> def __sub__ ( a , b ) : <EOL> try : <EOL> return rat ( a . __num * b . __den - b . __num * a . __den , <EOL> a . __den * b . __den ) <EOL> except OverflowError : <EOL> return rat ( long ( a . __num ) * long ( b . __den ) - <EOL> long ( b . __num ) * long ( a . __den ) , <EOL> long ( a . __den ) * long ( b . __den ) ) <EOL> def __rsub__ ( b , a ) : <EOL> return Rat ( a ) - b <EOL> def __mul__ ( a , b ) : <EOL> try : <EOL> return rat ( a . __num * b . __num , a . __den * b . __den ) <EOL> except OverflowError : <EOL> return rat ( long ( a . __num ) * long ( b . __num ) , <EOL> long ( a . __den ) * long ( b . __den ) ) <EOL> def __rmul__ ( b , a ) : <EOL> return Rat ( a ) * b <EOL> def __div__ ( a , b ) : <EOL> try : <EOL> return rat ( a . __num * b . __den , a . __den * b . __num ) <EOL> except OverflowError : <EOL> return rat ( long ( a . __num ) * long ( b . __den ) , <EOL> long ( a . __den ) * long ( b . __num ) ) <EOL> def __rdiv__ ( b , a ) : <EOL> return Rat ( a ) / b <EOL> def __mod__ ( a , b ) : <EOL> div = a / b <EOL> try : <EOL> div = int ( div ) <EOL> except OverflowError : <EOL> div = long ( div ) <EOL> return a - b * div <EOL> def __rmod__ ( b , a ) : <EOL> return Rat ( a ) % b <EOL> def __pow__ ( a , b ) : <EOL> if b . __den != <NUM_LIT:1> : <EOL> if isinstance ( a . __num , complex ) : <EOL> a = complex ( a ) <EOL> else : <EOL> a = float ( a ) <EOL> if isinstance ( b . __num , complex ) : <EOL> b = complex ( b ) <EOL> else : <EOL> b = float ( b ) <EOL> return a ** b <EOL> try : <EOL> return rat ( a . __num ** b . __num , a . __den ** b . __num ) <EOL> except OverflowError : <EOL> return rat ( long ( a . __num ) ** b . __num , <EOL> long ( a . __den ) ** b . __num ) <EOL> def __rpow__ ( b , a ) : <EOL> return Rat ( a ) ** b <EOL> def __neg__ ( a ) : <EOL> try : <EOL> return rat ( - a . __num , a . __den ) <EOL> except OverflowError : <EOL> return rat ( - long ( a . __num ) , a . __den ) <EOL> def __abs__ ( a ) : <EOL> return rat ( abs ( a . __num ) , a . __den ) <EOL> def __int__ ( a ) : <EOL> return int ( a . __num / a . __den ) <EOL> def __long__ ( a ) : <EOL> return long ( a . __num ) / long ( a . __den ) <EOL> def __float__ ( a ) : <EOL> return float ( a . __num ) / float ( a . __den ) <EOL> def __complex__ ( a ) : <EOL> return complex ( a . __num ) / complex ( a . __den ) <EOL> def __cmp__ ( a , b ) : <EOL> diff = Rat ( a - b ) <EOL> if diff . __num < <NUM_LIT:0> : <EOL> return - <NUM_LIT:1> <EOL> elif diff . __num > <NUM_LIT:0> : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def __rcmp__ ( b , a ) : <EOL> return cmp ( Rat ( a ) , b ) <EOL> def __nonzero__ ( a ) : <EOL> return a . __num != <NUM_LIT:0> <EOL> def __coerce__ ( a , b ) : <EOL> return a , Rat ( b ) <EOL> def test ( ) : <EOL> '''<STR_LIT>''' <EOL> print rat ( - <NUM_LIT:1> L , <NUM_LIT:1> ) <EOL> print rat ( <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> a = rat ( <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> print int ( a ) , long ( a ) , float ( a ) , complex ( a ) <EOL> b = rat ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> l = [ a + b , a - b , a * b , a / b ] <EOL> print l <EOL> l . sort ( ) <EOL> print l <EOL> print rat ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> print a + <NUM_LIT:1> <EOL> print a + <NUM_LIT:1> L <EOL> print a + <NUM_LIT:1.0> <EOL> try : <EOL> print rat ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> raise SystemError , '<STR_LIT>' <EOL> except ZeroDivisionError : <EOL> print '<STR_LIT:OK>' <EOL> print rat ( <NUM_LIT:2> ) , rat ( <NUM_LIT> ) , rat ( <NUM_LIT:3> , <NUM_LIT:2> ) , rat ( <NUM_LIT> + <NUM_LIT> ) , rat ( <NUM_LIT> , <NUM_LIT> ) <EOL> list = [ <NUM_LIT:2> , <NUM_LIT> , rat ( <NUM_LIT:3> , <NUM_LIT:2> ) , <NUM_LIT> + <NUM_LIT> ] <EOL> for i in list : <EOL> print i , <EOL> if not isinstance ( i , complex ) : <EOL> print int ( i ) , float ( i ) , <EOL> print complex ( i ) <EOL> print <EOL> for j in list : <EOL> print i + j , i - j , i * j , i / j , i ** j , <EOL> if not ( isinstance ( i , complex ) or <EOL> isinstance ( j , complex ) ) : <EOL> print cmp ( i , j ) <EOL> print <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
