Python练习题答案: 馏分类【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
馏分类【难度:2级】:
答案1:
# Something goes Here ...class Fraction:def __init__(self, numerator, denominator):g = gcd(numerator, denominator)self.top = numerator / gself.bottom = denominator /g#Equality testdef __eq__(self, other):first_num = self.top * other.bottomsecond_num = other.top * self.bottomreturn first_num == second_num#The rest goes heredef __add__(self, other):numerator = self.top * other.bottom + self.bottom * other.top;denominator = self.bottom * other.bottom;return Fraction(numerator, denominator)def __str__(self):return str(self.top) + "/" + str(self.bottom)def gcd(x, y):if (y == 0):return xreturn gcd(y, x % y)
答案2:
def gcd(a, b):while b != 0:t = bb = a % ba = treturn aclass Fraction:def __init__(self, numerator, denominator):reducer = gcd(numerator, denominator)self.top = numerator/reducerself.bottom = denominator/reducer__eq__ = lambda self, other: self.top * other.bottom == other.top * self.bottom__add__ = lambda self, other: Fraction(self.top*other.bottom + other.top*self.bottom, self.bottom*other.bottom)__str__ = lambda self: "{}/{}".format(self.top, self.bottom)
答案3:
from fractions import gcdclass Fraction:def __init__(self, numerator, denominator):self.top = numeratorself.bottom = denominatordef __eq__(self, other):first_num = self.top * other.bottomsecond_num = other.top * self.bottomreturn first_num == second_numdef __add__(self, other):top = self.top * other.bottom + self.bottom * other.topbottom = self.bottom * other.bottomg = gcd(top, bottom)return Fraction(top//g, bottom//g)def __str__(self):return '{0.top}/{0.bottom}'.format(self)
答案4:
def gcd(a, b):while b:a, b = b, a%breturn aclass Fraction:def __init__(self, numerator, denominator):g = gcd(numerator, denominator)self.top = numerator // gself.bottom = denominator // gdef __eq__(self, other):first_num = self.top * other.bottomsecond_num = other.top * self.bottomreturn first_num == second_numdef __add__(a, b):"""a + b"""return Fraction(a.top * b.bottom +b.top * a.bottom,a.bottom * b.bottom)def __str__(self):"""str(self)"""if self.bottom == 1:return str(self.top)else:return '%s/%s' % (self.top, self.bottom)
答案5:
class Fraction:def __init__(self, numerator, denominator):gcd = self._greatest_divisor(numerator, denominator)self.top = numerator // gcdself.bottom = denominator // gcddef _common_denom(self, other):return self.bottom * other.bottomdef _multiply_num(self, orig_num, orig_denom, common_denom):mult = common_denom // orig_denomreturn orig_num * multdef _nums_and_denom(self, other):bottom = self._common_denom(other)num_self = self._multiply_num(self.top, self.bottom, bottom)num_other = self._multiply_num(other.top, other.bottom, bottom)return num_self, num_other, bottomdef _greatest_divisor(self, num1, num2):""" Euclid's algorithm, recursive version """if num2 == 0:return num1 else:return self._greatest_divisor(num2, num1 % num2)def __eq__(self, other):first_num = self.top * other.bottomsecond_num = other.top * self.bottomreturn first_num == second_numdef __add__(self, other):num_self, num_other, bottom = self._nums_and_denom(other)top = num_self + num_otherreturn Fraction(top, bottom)def __repr__(self):return "{0}/{1}".format(self.top, self.bottom)
答案6:
class Fraction:def __init__(self,n,d):self.n,self.d=n,ddef __eq__(self,o):return self.n*o.d==self.d*o.ndef __add__(self,o):gcd=lambda a,b:gcd(b,a%b) if b else ax,y=self.n*o.d+o.n*self.d,self.d*o.dg=gcd(x,y)return Fraction(x//g,y//g)def __str__(self):return '{}/{}'.format(self.n,self.d)
答案7:
def nwd(a, b):# substitute versionwhile not a == b:if a < b:a, b = b, aa = a - breturn adef nww(a, b):return (a * b) // nwd(a, b)class Fraction:def __init__(self, numerator, denominator):self.numerator = numeratorself.denominator = denominatordef __eq__(self, other):first_num = self.numerator * other.denominatorsecond_num = other.numerator * self.denominatorreturn first_num == second_numdef __add__(self, other):common = nww(self.denominator, other.denominator)a_numerator = self.numerator * (common // self.denominator)b_numerator = other.numerator * (common // other.denominator)numerator = a_numerator + b_numeratorreturn self._simplify(numerator, common)@classmethoddef _simplify(cls, numerator, denominator):nw = nwd(numerator, denominator)numerator //= nwdenominator //= nwreturn cls(numerator, denominator)def __repr__(self):return "{numerator}/{denominator}".format(numerator=self.numerator, denominator=self.denominator)
答案8:
class Fraction:def __init__(self, numerator, denominator):gcd = self.__gcd(numerator, denominator)self.top = numerator // gcdself.bottom = denominator // gcddef __eq__(self, other): return self.top == other.top and self.bottom == other.bottomdef __str__(self): return '%d/%d' % (self.top, self.bottom)def __add__(self, other):lcm = self.__lcm(self.bottom, other.bottom)n = self.top * lcm / self.bottom + other.top * lcm / other.bottomreturn Fraction(n, lcm)def __gcd(self, a, b):while b: a, b = b, a % breturn adef __lcm(self, a, b): return (a * b) / self.__gcd(a, b)
景越Python基础训练营QQ群
欢迎各位同学加群讨论,一起学习,共同成长!
Python练习题答案: 馏分类【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战相关推荐
- Python练习题答案: IRR计算 - 盈利能力的评价【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
IRR计算 - 盈利能力的评价[难度:2级]: 答案1: def irr(c):precision, guess, v, lastPositiveGuess, lastNegativeGuess = ...
- Python练习题答案: 杰克的家【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
杰克的家[难度:2级]: 答案1: VERSES = """\ This is the house that Jack built.This is the malt th ...
- Python练习题答案: 分类新会员【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
分类新会员[难度:1级]: 答案1: def openOrSenior(data):return ["Senior" if age >= 55 and handicap &g ...
- Python练习题答案: CIS 122#12中的构造【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
CIS 122#12中的构造[难度:1级]: 答案1: # For your convenience, the following functions from previous challenges ...
- Python练习题答案: 转换货币II【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
转换货币II[难度:2级]: 答案1: def solution(to,lst):dolSym, eurSym, power = ('', '€', -1) if to=='EUR' else ('$ ...
- Python练习题答案: 海盗!是大炮准备好了!?【难度:0级】--景越Python编程实例训练营,1000道上机题等你来挑战
海盗!是大炮准备好了!?[难度:0级]: 答案1: def cannons_ready(gunners):return 'Shiver me timbers!' if 'nay' in gunners ...
- Python练习题答案: 摩门经【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战
摩门经[难度:2级]: 答案1: from math import log, ceil def mormons(starting_number, reach, target):return ceil( ...
- Python练习题答案: 财富通灵塔的乘驾【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战
财富通灵塔的乘驾[难度:3级]: 答案1: def ride_of_fortune(artifact, explorers):STATES = {'A': 'B', 'B': 'A'}DIRS = { ...
- Python练习题答案: 赛车#1:简化拖动赛【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战
赛车#1:简化拖动赛[难度:1级]: 答案1: def drag_race(length, anna, bob):a = length / anna.speed + anna.reaction_tim ...
最新文章
- MySQL服务器学习笔记!(二) ——数据库各项操作
- 周立波贼经典的话31-40
- 第六十六期:软件架构之道的一次感悟
- ZeroMQ简介:一种高性能的异步消息传递库
- 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比。...
- Nancy之Cache的简单使用
- Nanohttpd 异常 Explicit termination medthod 'end' not called 解决方法
- 兔子数列规律怎么讲_兔子数列规律
- 基于PP-OCR训练表格识别模型
- 如何随心意改变桌面快捷方式的图标
- 小程序华为手机canvas不显示问题
- word一打字就有下划线_word下划线上怎么打字 不破坏下滑线技巧
- VS提示无可用源,此模块的调试信息…
- 安装系统时,硬盘格式转换
- Wt(C++ Web) 源码编译以及cmake配置
- 元宇宙漫游指南-区块链构建元宇宙基础设施,一文搞清楚元宇宙和区块链
- C语言之联合体通用变量类型之妙用
- 动态八卦图html,HTML绘制太极八卦图
- 费舍尔算法(文字描述,过于繁琐,其实实现很简单)
- 学生计算机测评安排,计算机系学生综合素质测评办法(修改)