CodyKochmann
11/7/2017 - 10:01 PM

iterate over digits in a division problem

iterate over digits in a division problem

from generators import loop 

def iter_division(top, bottom):
    ''' iterate numbers in fractions '''
    cur_rem = lambda a,b:(a/b,a%b)
    current, remainder = cur_rem(top,bottom)
    # yield digits before the decimal
    for i in map(int, str(current)):
        yield i
    # yield the digits after the decimal
    for _ in loop():
        current, remainder = cur_rem(remainder*10, bottom)
        yield current

pi = iter_division(22, 7)
for i in range(10):
    print(next(pi))