使用python简化合理数字(Simplifying a Rational Number using python)

编程入门 行业动态 更新时间:2024-10-28 00:19:57
使用python简化合理数字(Simplifying a Rational Number using python)

我正在处理有理数的python问题,它有一个简化它的方法。 例如12/8给出3/2 。 我已经完成了这个问题,并得到了正确的答案,但我通过查找分子和分母的gcd来完成。 有人可能会使用一些内置的特殊Python特性或函数,模块或python特有的任何东西来帮助它,如你所说的“Pythonic方式!”。

是否有这样的方式或任何测试用例应该包括在内以涵盖所有可能性?

这是我的代码:

class RationalNumber: def __init__(self, n, d=1): self.n=n self.d=d '''def gcd(self, a, b): // I have taken out gcd by two methods: recursion and while loop if b>a: t=a a=b b=t while a%b != 0: r=a%b a=b b=r return b ''' def gcd(self, a, b): if a%b==0: return b else: return self.gcd(b, a%b) def simplify(self): x=self.gcd(self.n, self.d) self.n=self.n/x self.d=self.d/x return RationalNumber(self.n, self.d) def __str__(self): print "%s/%s"%(self.n, self.d) r1 = RationalNumber(12,8) print r1.simplify()

当我运行该程序时,它给出了答案并给出了一个错误:

Traceback (most recent call last): File "C:\Python27\CTE Python Practise\New folder\RationalNumberSimplify.py", line 42, in <module> print r1.simplify() TypeError: __str__ returned non-string (type NoneType)

请帮助我消除错误并改进代码并使其更加pythonic!

I'm working on question in python which deals with rational numbers, and it has a method which simplifies it. For example 12/8 gives 3/2. I have done that question and getting the correct answer also but I have done by finding gcd of the numerator and denominator. May someone help doing it using some inbuilt special python features or functions, modules or anything unique to python as you say "The Pythonic way!"

Is there such a way or any test cases should be included to cover all possibilities?

Here is my code:

class RationalNumber: def __init__(self, n, d=1): self.n=n self.d=d '''def gcd(self, a, b): // I have taken out gcd by two methods: recursion and while loop if b>a: t=a a=b b=t while a%b != 0: r=a%b a=b b=r return b ''' def gcd(self, a, b): if a%b==0: return b else: return self.gcd(b, a%b) def simplify(self): x=self.gcd(self.n, self.d) self.n=self.n/x self.d=self.d/x return RationalNumber(self.n, self.d) def __str__(self): print "%s/%s"%(self.n, self.d) r1 = RationalNumber(12,8) print r1.simplify()

When I run the program it gives the answer and gives an error:

Traceback (most recent call last): File "C:\Python27\CTE Python Practise\New folder\RationalNumberSimplify.py", line 42, in <module> print r1.simplify() TypeError: __str__ returned non-string (type NoneType)

Please help me removing the error and improving the code and making it more pythonic!

最满意答案

使用@stranac提到的分数模块。 至于你关于你的错误的其他问题,可以通过替换__str__方法__str__解决

def __repr__(self): return "%s/%s"%(self.n, self.d)

对于__str__或__repr__您需要返回一个字符串, 而不是简单地将其打印出来。 查看问题可能会有所帮助:

Python中__str__和__repr__之间的区别

Use the fraction module mentioned by @stranac. As for your other question about your error, it can be fixed by replacing the method __str__ with

def __repr__(self): return "%s/%s"%(self.n, self.d)

For either, __str__ or __repr__ you'll need to return a string not simply print it out. It may be helpful to look over the question:

Difference between __str__ and __repr__ in Python

更多推荐

本文发布于:2023-08-06 06:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字   python   Simplifying   Number   Rational

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!