-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfraction_to_recurring_decimal.rb
41 lines (35 loc) · 1.14 KB
/
fraction_to_recurring_decimal.rb
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
# https://leetcode.com/problems/fraction-to-recurring-decimal/
#
# Given two integers representing the numerator and denominator of a fraction,
# return the fraction in string format. If the fractional part is repeating,
# enclose the repeating part in parentheses.
#
# For example:
#
# Given numerator = 1, denominator = 2, return "0.5".
# Given numerator = 2, denominator = 1, return "2".
# Given numerator = 2, denominator = 3, return "0.(6)".
#
# Credits:
#
# Special thanks to @Shangrila for adding this problem and creating all
# test cases.
# @param {integer} numerator
# @param {integer} denominator
# @return {string}
def fraction_to_decimal(numerator, denominator)
sign = numerator * denominator >= 0 ? '' : '-'
numerator, denominator = numerator.abs, denominator.abs
quo, rem = numerator.divmod(denominator)
return sprintf("%s%d", sign, quo) if rem.zero?
out = sprintf("%s%d.", sign, quo)
m, reaching = {}, out.size
while rem.nonzero?
return out.insert(m[rem], '(') + ')' if m.key?(rem)
m[rem] = reaching
rem *= 10; reaching += 1
quo, rem = rem.divmod(denominator)
out << quo.to_s
end
out
end