Advertisement
Guest User

periods

a guest
Dec 13th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. def divide(a, b, dividends=[], period=[], is_fractional_part=0):
  2.     # if current dividend is was already
  3.     # processed, return nums of period
  4.     if a in dividends:
  5.         return period
  6.  
  7.     dividends.append(a)
  8.  
  9.     # we set is_fractional_part to 1 once
  10.     # and for good if dividend is less than divider.
  11.     # we append zero as a num of period and call
  12.     # divide() again with new period and dividends
  13.     if a < b:
  14.         is_fractional_part = 1
  15.  
  16.         period = divide((a * 10), b, dividends, period, is_fractional_part)
  17.         return period
  18.  
  19.     # if a and b is the same num, frac is not periodical
  20.     # we just return period
  21.     if a is b:
  22.         return period
  23.  
  24.     # if a is less than b we check if we processing frac part of a num
  25.     # otherwise we append nothing to period
  26.     # then we call the divide() func with a result of integer division a by b
  27.     if a > b:
  28.         if is_fractional_part:
  29.             period.append(a // b)
  30.         period = divide((a % b), b, dividends, period, is_fractional_part)
  31.         return period
  32.  
  33.  
  34. period_of_frac = list(divide(20, 3))
  35.  
  36. print(period_of_frac)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement