forked from qiwsir/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide.py
61 lines (47 loc) · 1.96 KB
/
divide.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env python
#coding:utf-8
def divide(numerator, denominator, detect_repetition=True, digit_limit=None):
# 如果是无限小数,必须输入限制的返回小数位数:digit_limit
# digit_limit = 5,表示小数位数5位,注意这里的小数位数是截取,不是四舍五入.
if not detect_repetition and digit_limit == None:
return None
decimal_found = False
v = numerator // denominator
numerator = 10 * (numerator - v * denominator)
answer = str(v)
if numerator == 0:
return answer
answer += '.'
# Maintain a list of all the intermediate numerators
# and the length of the output at the point where that
# numerator was encountered. If you encounter the same
# numerator again, then the decimal repeats itself from
# the last index that numerator was encountered at.
states = {}
while numerator > 0 and (digit_limit == None or digit_limit > 0):
if detect_repetition:
prev_state = states.get(numerator, None)
if prev_state != None:
start_repeat_index = prev_state
non_repeating = answer[:start_repeat_index]
repeating = answer[start_repeat_index:]
return non_repeating + '[' + repeating + ']'
states[numerator] = len(answer)
v = numerator // denominator
answer += str(v)
numerator -= v * denominator
numerator *= 10
if digit_limit != None:
digit_limit -= 1
if numerator > 0:
return answer + '...'
return answer
if __name__=="__main__":
print "5divide2",
print divide(5,2)
print "10divide3",
print divide(10,3)
print divide(10,3,5)
print "15divide7"
print divide(15,7)
print divide(15,7,True,3)