Comment detail

分数を小数に展開 (Nested Flatten)

This comment is reply for 340 匿名: 10/33が{0.3}になるのがどうか・・・(分数を小数に展開). Go to thread root.

自分なりに書き換えてみました。どんなもんでしょう。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def f(a, b):
        x = ''
        h = {a: 0}
        while 1:
                i, a = divmod(a * 10, b)
                x += str(i)
                if a == 0:
                        return '0.' + x
                if h.has_key(a):
                        return '0.%s{%s}' % (x[:h[a]], x[h[a]:])
                h[a] = len(x)

Index

Feed

Other

Link

Pathtraq

loading...