blob: 4182105abfeb57a5f2cca82f04f13bf5ea38ef40 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#!/opt/local/bin/python
#
# See ../README.md
#
#
# Run as: python ch-2.py < input-file
#
import fileinput
#
# See ../README.md for description of the how the method works
#
def long_division (numerator, denominator):
BASE = 10
fraction = str (numerator // denominator) + "."
position = len (fraction)
seen = {}
numerator = numerator % denominator
while not (numerator in seen):
if numerator == 0:
return (fraction)
seen [numerator] = position
fraction = fraction + str (BASE * numerator // denominator)
numerator = BASE * numerator % denominator
position = position + 1
return (fraction [:seen [numerator]] + "(" +
fraction [ seen [numerator]:] + ")")
for line in fileinput . input ():
print (long_division (*map (lambda _: int (_), line . split ())))
|