วันอาทิตย์ที่ 15 พฤศจิกายน พ.ศ. 2558

fraction

class fraction:
    def __init__(self, a, b=1):
        self.a=a
        self.b=b
    def __add__ (self,other):
        a=self.b #1
        b=other.b #5
        x=0
        if a>b:
            x=a
        else:
            x=b
        while (not(x%a==0 and x%b==0)):
            x+=1
        c=x/a
        d=x/b
        return fraction(int(self.a*c+other.a*d),x)
    def print_value (self):
        print( self.a,'/',self.b)

def setup():
    a=fraction(5,10)
    b=fraction(2,5)
    c=a+b
    c.print_value()
    assert c.a==9 and c.b==10
setup()
============================================================
class fraction:
    def __init__(self, a, b=1):
        self.a=a
        self.b=b
    def __add__ (self,other):
        a=self.b
        b=other.b
        if a==b:
            return fraction(self.a+other.a,self.b)
        else:
            return fraction(self.a*b+other.a*a,a*b)
    def print_value (self):
        print( self.a,'/',self.b)
def setup():
    a=fraction(5,10)
    b=fraction(2,5)
    c=a+b
    c.print_value()
    assert c.a==45 and c.b==50
setup()
==============================================
def setup():
    a=Fraction(3,5)
    b=Fraction(4,6)
    c=a.add(b)
    print (c.top ,"/",c.down)
class Fraction:
    def __init__(self,top,down):
        self.top=top
        self.down=down
    def add(self,b):
        lcm = (self.down*b.down)/self.gcd(self.down,b.down)
        return Fraction((self.top*lcm/self.down)+(b.top*lcm/b.down),lcm)
       
    def gcd(self,a,b):
        if (b==0):
            return a
        else:
            return self.gcd(b,a%b)
setup()

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...