80 Star 601 Fork 262

编程语言算法集 / Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
proper_fractions.py 1.12 KB
AI 代码解读
一键复制 编辑 原始数据 按行查看 历史
from math import gcd
def proper_fractions(denominator: int) -> list[str]:
"""
this algorithm returns a list of proper fractions, in the
range between 0 and 1, which can be formed with the given denominator
https://en.wikipedia.org/wiki/Fraction#Proper_and_improper_fractions
>>> proper_fractions(10)
['1/10', '3/10', '7/10', '9/10']
>>> proper_fractions(5)
['1/5', '2/5', '3/5', '4/5']
>>> proper_fractions(-15)
Traceback (most recent call last):
...
ValueError: The Denominator Cannot be less than 0
>>> proper_fractions(0)
[]
>>> proper_fractions(1.2)
Traceback (most recent call last):
...
ValueError: The Denominator must be an integer
"""
if denominator < 0:
raise ValueError("The Denominator Cannot be less than 0")
elif isinstance(denominator, float):
raise ValueError("The Denominator must be an integer")
return [
f"{numerator}/{denominator}"
for numerator in range(1, denominator)
if gcd(numerator, denominator) == 1
]
if __name__ == "__main__":
from doctest import testmod
testmod()
Python
1
https://gitee.com/TheAlgorithms/Python.git
git@gitee.com:TheAlgorithms/Python.git
TheAlgorithms
Python
Python
master

搜索帮助