Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def divide(a, b, dividends=[], period=[], is_fractional_part=0):
- # if current dividend is was already
- # processed, return nums of period
- if a in dividends:
- return period
- dividends.append(a)
- # we set is_fractional_part to 1 once
- # and for good if dividend is less than divider.
- # we append zero as a num of period and call
- # divide() again with new period and dividends
- if a < b:
- is_fractional_part = 1
- period = divide((a * 10), b, dividends, period, is_fractional_part)
- return period
- # if a and b is the same num, frac is not periodical
- # we just return period
- if a is b:
- return period
- # if a is less than b we check if we processing frac part of a num
- # otherwise we append nothing to period
- # then we call the divide() func with a result of integer division a by b
- if a > b:
- if is_fractional_part:
- period.append(a // b)
- period = divide((a % b), b, dividends, period, is_fractional_part)
- return period
- period_of_frac = list(divide(20, 3))
- print(period_of_frac)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement