在Python中使用生成器函数实现长除法 [英] Implementing long division using a generator function in python

查看:125
本文介绍了在Python中使用生成器函数实现长除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为尝试理解生成器函数的练习,我试图编写一个模拟长除法并一次返回一位数字的函数。我已经编写了此功能,但似乎没有用。但是,如果我在外壳中逐行浏览它,它确实可以完成我想要的操作,所以我不确定该怎么做。我已经在互联网上阅读了有关生成器函数的帖子:

As an exercise to try to understand generator functions I am trying to write a function that simulates long division and returns the number one digit at a time. I have written this function and it doesn't seem to work. However if I step through it line by line in the shell it does exactly what I want it to do so I'm not sure what to do going forward. I've read through posts on the internet about generator functions here:

据我了解,我只是用yield语句替换了return语句。不是这样吗?有人可以告诉我我做错了吗?

and from what I understand I just replace the return statement with a yield statement. Is this not the case? Can anybody please tell me what I am doing wrong? Any help is appreciated.

def decimals(number):    
    """
    Takes a numnber and generates the digits of  1/n.

    """
    divisor = number
    dividend = 1


    while dividend % divisor != 0:
        #Floor division is the // operator        
        quotient = divisor // dividend
        remainder = dividend % divisor

        temp = quotient * divisor
        if remainder != 0 :
            temp = quotient * divisor

        if temp > dividend:
            dividend = dividend * 10
            dividend = dividend - temp
        else:
            dividend = dividend - temp
        yield quotient



def main():
    gen = decimals(4)
    print(next(gen))

if __name__ == "__main__":
    main()


推荐答案

您的主要问题是您只能从生成器中输出一个值: next(gen)。要输出整个生成器,请根据其值列出一个列表: print(list(decimals(4))),或按值打印该值:

Your main problem is that you output only a single value from the generator: next(gen). To output the whole generator, make a list from it's values: print(list(decimals(4))), or print it value by value:

for digit in decimals(4):
    print(digit)

要处理无尽的生成器(例如,通过 decimals(3)调用),例如使用 itertools.islice 的有限数量的值:

To deal with endless generators (e.g., from a decimals(3) call), you can for example take only a limited amount of values from it with itertools.islice:

from itertools import islice
for digit in islice(decimals(3), 10):
    print(digit)

此外,我认为您的算法有问题。似乎无法产生正确的结果。我认为,它应该看起来像这样:

Also, I think there is something wrong with your algorithm. It doesn't seem to produce the correct results. I think, it should look something like this:

def decimals(number):    
    """
    Takes a number and generates the digits of  1/n.

    """
    divisor = number
    dividend = 1
    remainder = 1

    while remainder:
        #Floor division is the // operator        
        quotient = dividend // divisor
        remainder = dividend % divisor

        if remainder < divisor:
            dividend = remainder * 10
        else:
            dividend = remainder
        yield quotient

作为补充,此代码可能仍会更短。例如:

As a side note, this code may still be made shorter. For example:

def decimals(number):    
    dividend = 1
    while dividend:      
        yield dividend // number
        dividend = dividend % number * 10

这篇关于在Python中使用生成器函数实现长除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆