Skip to content

Instantly share code, notes, and snippets.

@alcides

alcides/fraccao.py Secret

Last active Mar 22, 2017
Embed
What would you like to do?
class Frac:
def __init__(self, num, den):
""" Constroi um objecto Frac com num como numerador e den como denominador """
self.num = num
self.den = den
def getNum(self):
""" Devolve o numerador """
return self.num
def getDen(self):
""" Devolve o denominador """
return self.den
def __str__(self):
return "{0} / {1}".format(self.num, self.den)
def gcd_alternativo(self, a, b):
sol = 1
i = 1
while i <= a and i <= b:
if a % i == 0 and b % i == 0:
sol = i
i+=1
return sol
def gcd_alternativo2(self, a, b):
for i in range(min(a,b), 0, -1):
if a % i == 0 and b % i == 0:
return i
def gcd(self, a, b):
m = min(a,b)
sol = 1
for i in range(2,m+1):
if a % i == 0 and b % i == 0:
sol = i
return sol
def simplify(self):
if self.num == self.den:
return Frac(1,1)
d = self.gcd( self.num, self.den)
return Frac( self.num / d , self.den / d )
def add(self, f2):
""" Adiciona a fraccao actual com a fraccao f2 """
"""
# Desnecessario este if, mas aumenta a performance se na maior parte dos casos
# os denominadores forem iguais
if self.getDen() == f2.getDen():
return Frac(self.getNum() + f2.getNum(), self.getDen())
"""
n_num = f2.getDen() * self.getNum() + self.getDen() * f2.getNum()
n_den = self.getDen() * f2.getDen()
n_f = Frac(n_num, n_den)
return n_f.simplify()
def multiply(self, f2):
n_num = self.getNum() * f2.getNum()
n_den = self.getDen() * f2.getDen()
n_f = Frac(n_num, n_den)
return n_f.simplify()
# Alternativamente
return Frac(self.getNum() * f2.getNum(), self.getDen() * f2.getDen()).simplify()
f = Frac(32,24)
#print f.gcd(32, 24)
#print f.gcd_alternativo(32, 24)
#print f.gcd_alternativo2(32, 24)
print f.simplify()
#print Frac(1,3).add(Frac(1,2))
#print Frac(1,3).multiply(Frac(1,2))
print Frac(2,3).multiply(Frac(3,2))
# versao que resolve os exercicios 1c e 1d
import math # for the distance methods
x = 1
class Point2D:
"""represent points in 2D space"""
def __init__(self, x = 0.0, y = 0.0):
"""Assume x,y are ints or floats"""
self.x = float(x)
self.y = float(y)
def teste(self):
self.x = 2
print x
def getX(self):
return self.x
def getY(self):
return self.y
def setX(self, newX):
self.x = float(newX)
def setY(self, newY):
self.y = float(newY)
def distance(self, p):
""" Distancia entre dois pontos """
a = p.getX() - self.getX()
b = p.getY() - self.getY()
return math.sqrt(a ** 2 + b ** 2)
def distanceOrigin(self):
""" Distancia a origem """
origem = Point2D()
return self.distance(origem)
def __str__(self):
""" Transforma self (Point2D) numa string """
return "Ponto (x={0},y={1})".format(self.getX(), self.getY())
# Versão mutável
def shift(self, deltaX, deltaY):
self.x += float(deltaX)
self.y += float(deltaY)
# Versão imutável
def shift(self, deltaX, deltaY):
return Point2D(self.x + deltaX, self.y + deltaY)
from point2d import Point2D
q = Point2D()
q.teste()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.