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()
วันอาทิตย์ที่ 15 พฤศจิกายน พ.ศ. 2558
สมัครสมาชิก:
บทความ (Atom)
commit เพิ่มเติม : เพื่ม userstory, bookmark
>> add userstory ทำการเพิ่ม userstory ใน functional tests >> add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...
-
1 2 3 4 5 6 7 8 9 def setup (): print (d2b( 8 )) def d2b (num): x = "" while num > 0 : x = str ( int (n...
-
1 2 3 4 5 6 7 8 9 10 11 12 13 def test_layout_and_styling ( self ): # Edith goes to the home page self . brow...
-
การใช้ template inheritence คือการสืบทอดเทมเพลต เพื่อให้ไม่ต้องเขียนโค้ดหน้าเว็บๆซ้ำๆ เราจะมีหน้าหลักอันเดียว (base.html or parent) ภายในนั...