Given a fraction. Convert it into a decimal. 
If the fractional part is repeating, enclose the repeating part in parentheses.
 
Example 1:
Input: numerator = 1, denominator = 3
Output: "0.(3)"
Explanation: 1/3 = 0.3333... So here 3 is 
recurring.
Example 2:
Input: numerator = 5, denominator = 2
Output: "2.5"
Explanation: 5/2 = 2.5
 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function fractionToDecimal() which takes numerator and denominator as input parameter and returns its decimal form in string format.
Note: In case, the numerator is divisible by the denominator, just print the integer value.
 
Expected Time Compelxity: O(k) where k is constant.
Expected Space Complexity: O(k)
 
Constraints:
1 ≤ numerator, denominator ≤ 2000

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
	def fractionToDecimal(self, numerator, denominator):
		# Code here

Solutions:
class Solution:

	def fractionToDecimal(self, numerator, denominator):
		quotient = numerator // denominator
		remainder = numerator % denominator
		result = str(quotient)
		if remainder == 0:
			return quotient
		result += '.'
		mp = {}
		while remainder != 0:
			if remainder in mp:
				pos = mp[remainder]
				x = result[:pos]
				x += '('
				x += result[pos:]
				x += ')'
				result = x
				break
			else:
				mp[remainder] = len(result)
				remainder = remainder * 10
				quotient = remainder // denominator
				remainder = remainder % denominator
				result += str(quotient)
		return result


class Solution:

	def fractionToDecimal(self, numerator, denominator):

		def _tostr(lst):
			return ''.join(lst)
		(v, ints) = (numerator % denominator, numerator // denominator)
		(dec, flag) = ([], {})
		repeat = -1
		bit = 0
		while v and v not in flag:
			flag[v] = bit
			bit += 1
			v *= 10
			dec.append(str(v // denominator))
			v = v % denominator
		if v in flag:
			repeat = flag[v]
		if not dec:
			return str(ints)
		if repeat == -1:
			return str(ints) + '.' + _tostr(dec)
		return str(ints) + '.' + _tostr(dec[:repeat]) + '(' + _tostr(dec[repeat:]) + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		nr = numerator
		result = ''
		(quot, nr) = divmod(nr, denominator)
		result += str(quot)
		if nr:
			table = {}
			recurring = False
			fraction = ''
			pos = 0
			while nr:
				if table.get(nr) != None:
					recurring = True
					pos = table[nr]
					break
				else:
					table[nr] = pos
					nr *= 10
					(quot, nr) = divmod(nr, denominator)
					fraction += str(quot)
					pos += 1
			if recurring:
				fraction = fraction[:pos] + '(' + fraction[pos:] + ')'
			result += '.' + str(fraction)
		return result


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		l = []
		q = numerator // denominator
		numerator = numerator % denominator
		if numerator == 0:
			return str(q)
		l.append(str(q))
		l.append('.')
		f = ['#', '.']
		while numerator > 0 and numerator not in f:
			f.append(numerator)
			numerator = numerator * 10
			q = numerator // denominator
			numerator = numerator % denominator
			l.append(str(q))
		if numerator > 0:
			index = f.index(numerator)
			l.insert(index, '(')
			l.append(')')
		return ''.join(l)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		ans = ''
		ans += str(numerator // denominator)
		rem = numerator % denominator
		d = {}
		if rem == 0:
			return ans
		ans += '.'
		ans2 = ''
		while rem != 0:
			if d.get(rem, -1) == -1:
				d[rem] = len(ans2)
			else:
				ans += ans2[:d[rem]] + '(' + ans2[d[rem]:] + ')'
				return ans
			rem *= 10
			ans2 += str(rem // denominator)
			rem %= denominator
		return ans + ans2


class Solution:

	def fractionToDecimal(self, num, den):
		str1 = str(num // den) + '.'
		rem = num % den
		if rem == 0:
			return num // den
		dict = {}
		while rem != 0 and rem not in dict:
			dict[rem] = len(str1)
			rem = rem * 10
			rno = rem // den
			rem = rem % den
			str1 += str(rno)
		if rem == 0:
			return str1
		else:
			return str1[:dict[rem]] + '(' + str1[dict[rem]:] + ')'


class Solution:

	def fractionToDecimal(self, num, den):
		return calculateFraction(num, den)

def calculateFraction(num, den):
	if num == 0:
		return '0'
	sign = -1 if (num < 0) ^ (den < 0) else 1
	num = abs(num)
	den = abs(den)
	initial = num // den
	res = ''
	if sign == -1:
		res += '-'
	res += str(initial)
	if num % den == 0:
		return res
	res += '.'
	rem = num % den
	mp = {}
	index = 0
	repeating = False
	while rem > 0 and (not repeating):
		if rem in mp:
			index = mp[rem]
			repeating = True
			break
		else:
			mp[rem] = len(res)
		rem = rem * 10
		temp = rem // den
		res += str(temp)
		rem = rem % den
	if repeating:
		res += ')'
		x = res[:index]
		x += '('
		x += res[index:]
		res = x
	return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		no = str(numerator // denominator) + '.'
		remainder = numerator % denominator
		if remainder == 0:
			return numerator // denominator
		dp = {}
		while remainder != 0 and remainder not in dp:
			dp[remainder] = len(no)
			remainder = remainder * 10
			rno = remainder // denominator
			remainder = remainder % denominator
			no += str(rno)
		if remainder == 0:
			return no
		else:
			return no[:dp[remainder]] + '(' + no[dp[remainder]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		i = numerator // denominator
		rem = numerator % denominator
		if rem == 0:
			return i
		ans = str(i) + '.'
		n = len(ans)
		passed = {}
		finished = False
		while True:
			rem *= 10
			if rem == 0:
				return ans
			if rem in passed:
				ans = ans[:passed[rem]] + '(' + ans[passed[rem]:] + ')'
				return ans
			passed[rem] = n
			while rem < denominator:
				rem *= 10
				ans += '0'
				n += 1
				if rem in passed:
					ans = ans[:passed[rem]] + '(' + ans[passed[rem]:] + ')'
					return ans
				passed[rem] = n
			ans += str(rem // denominator)
			n += 1
			rem = rem % denominator


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator < 0 and denominator < 0:
			sign = '-'
		elif numerator < 0 or denominator < 0:
			sign = '-'
		else:
			sign = ''
		initial = numerator // denominator
		res = ''
		if sign == '-':
			res = res + sign
		res = str(initial)
		represent = False
		mp = {}
		num = abs(numerator)
		den = abs(denominator)
		if num % den == 0:
			return res
		res = res + '.'
		index = 0
		rem = num % den
		while rem > 0 and (not represent):
			if rem in mp:
				index = mp[rem]
				represent = True
				break
			else:
				mp[rem] = len(res)
			rem = rem * 10
			temp = rem // den
			res += str(temp)
			rem = rem % den
		if represent:
			res += ')'
			x = res[:index]
			x += '('
			x += res[index:]
			res = x
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		u = numerator // denominator
		res = list(str(u))
		rem = numerator % denominator
		if rem == 0:
			return ''.join(res)
		res.append('.')
		d = dict()
		while rem:
			if rem in d:
				res.insert(d[rem], '(')
				res.append(')')
				break
			else:
				d[rem] = len(res)
				t = rem * 10 // denominator
				res.append(str(t))
				rem = rem * 10 % denominator
		return ''.join(res)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		res = ''
		k = {}
		mod = numerator % denominator
		res += str((numerator - mod) // denominator)
		if mod == 0:
			return res
		res += '.'
		numerator = mod
		while numerator != 0 and numerator not in k:
			k[numerator] = len(res)
			numerator *= 10
			mod = numerator % denominator
			res += str((numerator - mod) // denominator)
			numerator = mod
		if numerator != 0:
			res = res[:k[numerator]] + '(' + res[k[numerator]:] + ')'
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator == 0:
			return 0
		sign = -1 if (numerator < 0) ^ (denominator < 0) else 1
		numerator = abs(numerator)
		denominator = abs(denominator)
		intPart = numerator // denominator
		res = ''
		if sign == -1:
			res += '-'
		res += str(intPart)
		if numerator % denominator == 0:
			return res
		res += '.'
		rem = numerator % denominator
		mp = dict()
		repeating = False
		index = 0
		while rem > 0 and (not repeating):
			if rem in mp:
				index = mp[rem]
				repeating = True
				break
			else:
				mp[rem] = len(res)
			rem *= 10
			temp = rem // denominator
			res += str(temp)
			rem = rem % denominator
		if repeating:
			res += ')'
			x = res[:index]
			x += '('
			x += res[index:]
			res = x
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator == 0:
			return '0'
		ans = ''
		if (numerator < 0) ^ (denominator < 0):
			ans += '-'
		numerator = abs(numerator)
		denominator = abs(denominator)
		ans += str(numerator // denominator)
		if numerator % denominator == 0:
			return ans
		ans += '.'
		dict = {}
		remainder = numerator % denominator
		while remainder:
			if remainder in dict:
				ans = ans[:dict[remainder]] + '(' + ans[dict[remainder]:] + ')'
				break
			dict[remainder] = len(ans)
			remainder *= 10
			ans += str(remainder // denominator)
			remainder %= denominator
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator % denominator == 0:
			output = [str(numerator // denominator)]
		else:
			output = [str(numerator // denominator) + '.']
			numerator %= denominator
			sub_res = [numerator % denominator]
			while numerator != 0:
				numerator *= 10
				(res_digit, numerator) = divmod(numerator, denominator)
				output.append(str(res_digit))
				if numerator not in sub_res:
					sub_res.append(numerator)
				else:
					output.insert(sub_res.index(numerator) + 1, '(')
					output.append(')')
					break
		return ''.join(output)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		ans = ''
		ans = ans + str(numerator // denominator)
		rem = numerator % denominator
		d = {}
		n = ''
		if rem == 0:
			return ans
		while rem != 0 and rem not in d:
			d[rem] = len(n)
			rem *= 10
			n += str(rem // denominator)
			rem = rem % denominator
		if rem == 0:
			return ans + '.' + n
		else:
			return ans + '.' + n[:d[rem]] + '(' + n[d[rem]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		(n, remainder) = divmod(abs(numerator), abs(denominator))
		result = [str(n), '.']
		stack = []
		while remainder not in stack:
			stack.append(remainder)
			(n, remainder) = divmod(remainder * 10, abs(denominator))
			result.append(str(n))
		idx = stack.index(remainder)
		result.insert(idx + 2, '(')
		result.append(')')
		return ''.join(result).replace('(0)', '').rstrip('.')


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		hm = {}
		res = ''
		rem = numerator % denominator
		s = str(numerator / denominator)
		start = 1
		while rem != 0 and rem not in hm:
			hm[rem] = len(res)
			rem *= 10
			start += 1
			quotient = rem // denominator
			res += str(quotient)
			rem = rem % denominator
		if numerator // denominator == numerator / denominator:
			return numerator // denominator
		if rem == 0:
			return numerator / denominator
		return s[:start - len(res[hm[rem]:]) + len(str(numerator // denominator))] + str('(') + res[hm[rem]:] + str(')')


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		ans = ''
		ans += str(numerator // denominator)
		d = {}
		rem = numerator % denominator
		if rem != 0:
			ans += '.'
		while rem != 0:
			rem *= 10
			try:
				ans = ans[:d[rem]] + '(' + ans[d[rem]:]
				ans += ')'
				break
			except:
				d[rem] = len(ans)
				ans += str(rem // denominator)
				rem = rem % denominator
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		sign = '-' if numerator * denominator < 0 else ''
		(quotient, remainder) = divmod(abs(numerator), abs(denominator))
		result_list = [sign, str(quotient), '.']
		remainders = []
		while remainder not in remainders:
			remainders.append(remainder)
			(quotient, remainder) = divmod(remainder * 10, abs(denominator))
			result_list.append(str(quotient))
		idx = remainders.index(remainder)
		result_list.insert(idx + 3, '(')
		result_list.append(')')
		result = ''.join(result_list).replace('(0)', '').rstrip('.')
		return result


class Solution:

	def fractionToDecimal(self, numerator: int, denominator: int):
		sign = '-' if numerator * denominator < 0 else ''
		integ = str(numerator // denominator)
		if numerator % denominator == 0:
			return sign + integ
		decimals = ''
		residuals = {}
		index = 0
		resid = numerator % denominator
		repetition = False
		while not repetition and resid > 0:
			if resid in residuals:
				index = residuals[resid]
				repetition = True
				break
			else:
				residuals[resid] = len(decimals)
			resid *= 10
			decimals += str(resid // denominator)
			resid %= denominator
		if repetition:
			decimals += ')'
			temp = decimals[:index]
			temp += '('
			temp += decimals[index:]
			decimals = temp
		res = sign + integ + '.' + decimals
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator == 0:
			return '0'
		neg = False
		if numerator > 0 and denominator < 0:
			neg = True
		if numerator < 0 and denominator > 0:
			neg = True
		res = '-' if neg else ''
		num = abs(numerator)
		den = abs(denominator)
		res += str(num // den)
		rem = num % den
		if rem == 0:
			return res
		res += '.'
		rem_map = {}
		while True:
			if rem == 0:
				break
			if rem in rem_map:
				index = rem_map[rem]
				res = res[:index] + '(' + res[index:] + ')'
				break
			rem_map[rem] = len(res)
			res += str(rem * 10 // den)
			rem = rem * 10 % den
		return res


class Solution:

	def fractionToDecimal(self, num, de):
		ans = ''
		quot = num // de
		rem = num % de
		ans += str(quot)
		if rem:
			d = dict()
			recurring = False
			fraction = ''
			pos = 0
			while rem:
				if rem in d:
					recurring = True
					pos = d[rem]
					break
				else:
					d[rem] = pos
					rem *= 10
					quot = rem // de
					rem = rem % de
					fraction += str(quot)
					pos += 1
			if recurring:
				fraction = fraction[:pos] + '(' + fraction[pos:] + ')'
			ans += '.' + str(fraction)
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		(n, remainder) = divmod(abs(numerator), abs(denominator))
		sign = '-' if numerator * denominator < 0 else ''
		result = [sign + str(n), '.']
		remainders = {}
		while remainder > 0 and remainder not in remainders:
			remainders[remainder] = len(result)
			(n, remainder) = divmod(remainder * 10, abs(denominator))
			result.append(str(n))
		if remainder in remainders:
			n = remainders[remainder]
			result.insert(n, '(')
			result.append(')')
		return ''.join(result).rstrip('.')


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		quotient = numerator // denominator
		quotient = numerator // denominator
		reminder = numerator % denominator
		remaining = ''
		position = {}
		while reminder != 0 and reminder not in position:
			position[reminder] = len(remaining)
			reminder *= 10
			in_quotient = reminder // denominator
			reminder = reminder % denominator
			remaining += str(in_quotient)
		if not remaining:
			return quotient
		elif reminder == 0:
			return str(quotient) + '.' + remaining
		else:
			return str(quotient) + '.' + remaining[0:position[reminder]] + '(' + remaining[position[reminder]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		ans = []
		num = numerator
		den = denominator
		q = num // den
		r = num % den
		ans.append(str(q))
		if r == 0:
			return ''.join(ans)
		else:
			ans.append('.')
			map = {}
			while r != 0:
				if r in map.keys():
					size = map[r]
					ans.insert(size, '(')
					ans.append(')')
					break
				else:
					map[r] = len(ans)
					r *= 10
					q = r // den
					r = r % den
					ans.append(str(q))
		return ''.join(ans)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		ans = ''
		ans += str(numerator // denominator)
		remdict = {}
		remainder = numerator % denominator
		if remainder == 0:
			return ans
		else:
			ans = ans + '.'
			while remainder > 0:
				remainder = remainder * 10
				if remainder in remdict.keys():
					ans = ans[:remdict[remainder]] + '(' + ans[remdict[remainder]:]
					ans = ans + ')'
					break
				else:
					remdict[remainder] = len(ans)
					ans = ans + str(remainder // denominator)
					remainder = remainder % denominator
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator % denominator == 0:
			return numerator // denominator
		intVal = numerator // denominator
		rem = numerator - intVal * denominator
		rem_array = [rem]
		decVal_array = []
		while True:
			decVal = rem * 10 // denominator
			decVal_array.append(decVal)
			rem = rem * 10 - decVal * denominator
			if rem == 0:
				x = ''
				for i in decVal_array:
					x += str(i)
				return str(intVal) if x == '0' else '{}.{}'.format(intVal, x)
			if rem in rem_array:
				y = ''
				rem_size = len(rem_array)
				i = 0
				for i in range(rem_size):
					if rem != rem_array[i]:
						y += str(decVal_array[i])
					else:
						break
				x = ''
				for z in range(i, len(decVal_array)):
					x += str(decVal_array[z])
				return '{}.{}({})'.format(intVal, y, x)
			rem_array.append(rem)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		mp = {}
		first = numerator // denominator
		rem = numerator % denominator
		S = str(first)
		if rem == 0:
			return S
		S += '.'
		while rem != 0:
			if rem in mp:
				S = S[:mp[rem]] + '(' + S[mp[rem]:]
				S += ')'
				return S
			else:
				mp[rem] = len(S)
				xyz = rem * 10 // denominator
				S += str(xyz)
				rem = rem * 10 % denominator
		return S


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		numr = numerator
		denr = denominator
		q = numr // denr
		i = len(str(q))
		res = str(q)
		mp = {}
		rem = numr % denr
		while rem != 0 and rem not in mp:
			mp[rem] = len(res)
			rem = rem * 10
			res_part = rem // denr
			res += str(res_part)
			rem = rem % denr
		if rem == 0:
			if res[i:] == '':
				return res
			return res[:i] + '.' + res[i:]
		else:
			r = res[mp[rem]:]
			return res[:i] + '.' + res[i:mp[rem]] + '(' + r + ')'


class Solution:

	def fractionToDecimal(self, num, den):
		q = num // den
		r = num % den
		ans = str(q)
		if r == 0:
			return ans
		else:
			ans += '.'
			remainders = {}
			while r != 0:
				if r in remainders:
					pos = remainders[r]
					ans = ans[:pos] + '(' + ans[pos:] + ')'
					break
				remainders[r] = len(ans)
				r *= 10
				q = r // den
				r = r % den
				ans += str(q)
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator == 0:
			return '0'
		sign = -1 if (numerator < 0) ^ (denominator < 0) else 1
		num = abs(numerator)
		den = abs(denominator)
		initial = num // den
		res = ''
		if sign == -1:
			res += '-'
		res += str(initial)
		if num % den == 0:
			return res
		res += '.'
		rem = num % den
		mp = {}
		ind = 0
		rep = False
		while rem > 0 and (not rep):
			if rem in mp:
				ind = mp[rem]
				rep = True
				break
			else:
				mp[rem] = len(res)
			rem = rem * 10
			temp = rem // den
			res += str(temp)
			rem = rem % den
		if rep:
			res += ')'
			x = res[:ind]
			x += '('
			x += res[ind:]
			res = x
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		res = ''
		q = numerator // denominator
		r = numerator % denominator
		if r == 0:
			return q
		mp = {}
		res += str(q)
		res += '.'
		while r != 0 and r not in mp:
			mp[r] = len(res)
			r *= 10
			q = r // denominator
			res += str(q)
			r = r % denominator
		if r == 0:
			return str(numerator / denominator)
		return res[:mp[r]] + '(' + res[mp[r]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		quo = numerator // denominator
		rem = numerator % denominator
		if rem == 0:
			return quo
		res = str(quo) + '.'
		mp = {}
		while rem != 0 and rem not in mp:
			mp[rem] = len(res)
			rem = rem * 10
			res_part = rem // denominator
			res += str(res_part)
			rem = rem % denominator
		return res if rem == 0 else res[:mp[rem]] + '(' + res[mp[rem]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		num = numerator
		den = denominator
		intial = num // den
		res = ''
		res += str(intial)
		if num % den == 0:
			return res
		res += '.'
		rem = num % den
		mp = {}
		index = 0
		repeating = False
		while rem > 0 and (not repeating):
			if rem in mp:
				index = mp[rem]
				repeating = True
				break
			else:
				mp[rem] = len(res)
			rem = rem * 10
			temp = rem // den
			res += str(temp)
			rem %= den
		if repeating:
			res += ')'
			x = res[:index]
			x += '('
			x += res[index:]
			res = x
		return res


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		result = []
		fin = str()
		rem = abs(numerator) % abs(denominator)
		sign = int(numerator / abs(numerator) / (denominator / abs(denominator)))
		if sign == -1:
			fin += '-'
		repeating = False
		rem_list = []
		rem_list.append(rem)
		indexs = 0
		while rem > 0 and (not repeating):
			rem = rem * 10
			ind = rem // denominator
			rem = rem % denominator
			result.append(str(ind))
			if rem in rem_list:
				repeating = True
				indexs = rem_list.index(rem)
				break
			rem_list.append(rem)
		fin += str(numerator // denominator)
		if repeating:
			fin = fin + '.' + ''.join(result[:indexs]) + '(' + ''.join(result[indexs:]) + ')'
		elif rem_list[0] == 0:
			fin = fin
		else:
			fin = fin + '.' + ''.join(result)
		return fin


def Fraction(n, d):
	ans = []
	q = n // d
	r = n % d
	ans.append(q)
	if r == 0:
		return ''.join([str(i) for i in ans])
	else:
		ans.append('.')
		m = {}
		while r != 0:
			if r in m.keys():
				l = m[r]
				ans.insert(l, '(')
				ans.append(')')
				break
			else:
				m[r] = len(ans)
				r = r * 10
				q = r // d
				r = r % d
				ans.append(q)
	return ''.join([str(i) for i in ans])

class Solution:

	def fractionToDecimal(self, numerator, denominator):
		return Fraction(numerator, denominator)


class Solution:

	def fractionToDecimal(self, num, den):
		sign = ''
		if num / den < 0:
			sign = '-'
		if num % den == 0:
			return str(int(num / den))
		int_value = num // den
		decimal = '.'
		stack = []
		result_decimal = []
		index_rep = 0
		remainder = num % den
		while str(remainder) not in stack:
			stack.append(str(remainder))
			index_rep += 1
			(num, remainder) = divmod(remainder * 10, den)
			result_decimal.append(str(num))
		rep_index = stack.index(str(remainder))
		return_value = sign + str(int_value) + decimal + ''.join(result_decimal[:rep_index]) + '(' + ''.join(result_decimal[rep_index:index_rep]) + ')'
		return return_value.replace('(0)', '')


class Solution:

	def fractionToDecimal(self, numr, denr):
		mp = {}
		(quo, rem) = (numr // denr, numr % denr)
		if rem == 0:
			return quo
		res = str(quo) + '.'
		while rem != 0 and rem not in mp:
			mp[rem] = len(res)
			rem = rem * 10
			res_part = rem // denr
			res += str(res_part)
			rem = rem % denr
		return res if rem == 0 else res[:mp[rem]] + '(' + res[mp[rem]:] + ')'


class Solution:

	def fractionToDecimal(self, n, d):
		q = n // d
		r = n % d
		ans = str(q)
		if r == 0:
			return ans
		ans += '.'
		dic = {}
		while r != 0:
			if dic.get(r) != None:
				l = dic[r]
				ans = ans[0:l] + '(' + ans[l:] + ')'
				break
			else:
				dic[r] = len(ans)
				r = r * 10
				newQ = r // d
				r = r % d
				ans += str(newQ)
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		res = ''
		if numerator < 0 and denominator > 0 or (numerator > 0 and denominator < 0):
			res += '-'
		num = abs(numerator)
		den = abs(denominator)
		res += str(num // den)
		rem = num % den
		if rem == 0:
			return res
		res += '.'
		di = {}
		index = 0
		repeating = False
		while rem > 0 and repeating == False:
			if rem in di:
				index = di[rem]
				repeating = True
				break
			else:
				di[rem] = len(res)
			rem = rem * 10
			temp = rem // den
			res += str(temp)
			rem = rem % den
		if repeating == True:
			x = res[0:index] + '(' + res[index:] + ')'
		else:
			x = res
		return x


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		whole = numerator // denominator
		rem = numerator % denominator
		rem_list = []
		dec = ''
		while rem != 0 and rem not in rem_list:
			rem_list.append(rem)
			rem *= 10
			dec += str(rem // denominator)
			rem = rem % denominator
		if rem == 0:
			if len(dec) == 0:
				return str(whole)
			ans = str(whole) + '.' + dec
			return ans
		else:
			temp = rem_list.index(rem)
			rec = dec[temp:]
			non_rec = dec[:temp]
			ans = str(whole) + '.' + non_rec + '(' + rec + ')'
			return ans
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, A, B):

		def _tostr(lst):
			return ''.join(lst)
		(v, ints) = (A % B, A // B)
		(dec, flag) = ([], {})
		repeat = -1
		bit = 0
		while v and v not in flag:
			flag[v] = bit
			bit += 1
			v *= 10
			dec.append(str(v // B))
			v = v % B
		if v in flag:
			repeat = flag[v]
		if not dec:
			return str(ints)
		if repeat == -1:
			return str(ints) + '.' + _tostr(dec)
		return str(ints) + '.' + _tostr(dec[:repeat]) + '(' + _tostr(dec[repeat:]) + ')'
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		q = numerator // denominator
		r = numerator % denominator
		ans = str(q)
		dic = {}
		if r == 0:
			return ans
		else:
			ans += '.'
			while r != 0:
				if r in dic.keys():
					ans = ans[:dic[r]] + '(' + ans[dic[r]:] + ')'
					break
				else:
					dic[r] = len(ans)
					r *= 10
					q = r // denominator
					r %= denominator
					ans += str(q)
			return ans
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, num, den):
		res = ''
		if num < 0 and den > 0 or (num > 0 and den < 0):
			res += '-'
		digit = num // den
		res += str(digit)
		rem = num % den
		if rem == 0:
			return res
		else:
			res += '.'
		i = 0
		rem_idx = {}
		is_repeat = False
		frac = digit
		while rem > 0 and (not is_repeat):
			if rem in rem_idx:
				i = rem_idx[rem]
				is_repeat = True
				non_repeat = res[:i]
				non_repeat += '('
				repeat = res[i:]
				res = non_repeat + repeat + ')'
				break
			else:
				rem_idx[rem] = len(res)
			rem *= 10
			frac = rem // den
			rem = rem % den
			res += str(frac)
		return res
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		res = ''
		quotient = int(numerator / denominator)
		remainder = numerator % denominator
		res += str(quotient)
		if remainder == 0:
			return res
		else:
			res += '.'
			mp = {}
			while remainder != 0:
				if remainder in mp:
					res = list(res)
					res.insert(mp[remainder], '(')
					res = ''.join(res)
					res += ')'
					break
				else:
					mp[remainder] = len(res)
					remainder *= 10
					quotient = int(remainder / denominator)
					remainder = remainder % denominator
					res += str(quotient)
			return res
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		if numerator == 0:
			return '0'
		sign = -1 if (numerator < 0) ^ (denominator < 0) else 1
		numerator = abs(numerator)
		denominator = abs(denominator)
		val = numerator // denominator
		res = ''
		if sign == -1:
			res += '-'
		res += str(val)
		if numerator % denominator == 0:
			return res
		res += '.'
		rem = numerator % denominator
		mp = {}
		index = 0
		repeating = False
		while rem > 0 and (not repeating):
			if rem in mp:
				index = mp[rem]
				repeating = True
				break
			else:
				mp[rem] = len(res)
			rem = rem * 10
			temp = rem // denominator
			res += str(temp)
			rem = rem % denominator
		if repeating:
			res += ')'
			x = res[:index]
			x += '('
			x += res[index:]
			res = x
		return res
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		(numerator, denominator) = input().split()
		numerator = int(numerator)
		denominator = int(denominator)
		ob = Solution()
		ans = ob.fractionToDecimal(numerator, denominator)
		print(ans)


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		val = numerator // denominator
		if numerator % denominator == 0:
			return val
		rem = numerator % denominator
		dic = {}
		res = ''
		while rem != 0 and rem not in dic:
			dic[rem] = len(res)
			rem = rem * 10
			quot = str(rem // denominator)
			res += quot
			rem = rem % denominator
		if rem == 0:
			return str(val) + '.' + res
		else:
			return str(val) + '.' + res[:dic[rem]] + '(' + res[dic[rem]:] + ')'


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		value1 = str(int(numerator / denominator))
		value2 = '.'
		flag = 0
		rem = numerator % denominator
		if rem == 0:
			value = value1
			return value
		rem_list = []
		while rem != 0 and rem not in rem_list:
			rem_list.append(rem)
			rem = rem * 10
			value2 += str(int(rem / denominator))
			rem = rem % denominator
			if rem == 0:
				flag = 1
		if flag == 1:
			value = value1 + value2
			return value
		else:
			value2 += ')'
			posi = rem_list.index(rem) + 1
			value2 = value2[:posi] + '(' + value2[posi:]
			value = value1 + value2
			return value


class Solution:

	def fractionToDecimal(self, num, den):
		sign = int(num * den >= 0)
		ans = ''
		num = abs(num)
		den = abs(den)
		if not sign:
			ans = '-'
		rem_set = set()
		rem = num % den
		q = num // den
		ans += str(q)
		st = []
		if rem:
			st.append(('', rem))
			ans += '.'
		s = ''
		while rem:
			if rem in rem_set:
				while st and st[-1][-1] != rem:
					s += str(st.pop()[0])
				s += str(st.pop()[0])
				s = '(' + s[::-1] + ')'
				break
			else:
				rem_set.add(rem)
				q = rem * 10 // den
				st.append([q, rem])
				rem = rem * 10 % den
		for i in st:
			ans += str(i[0])
		ans += s
		return ans


class Solution:

	def fractionToDecimal(self, numerator, denominator):
		start = ''
		if numerator >= denominator:
			(quotient, remainder) = divmod(numerator, denominator)
			start += str(quotient)
		else:
			result = 0
			remainder = numerator
			start += str('0')
		if remainder != 0:
			start += '.'
		remainders = {}
		rest = []
		while remainder and (not remainder in remainders):
			remainders[remainder] = len(rest)
			numerator = remainder * 10
			if numerator > denominator:
				(quotient, remainder) = divmod(numerator, denominator)
				rest.append(quotient)
			else:
				rest.append(0)
				remainder = numerator
		if remainder in remainders:
			i = remainders[remainder]
			return start + ''.join(map(str, rest[0:i])) + '(' + ''.join(map(str, rest[i:])) + ')'
		return start + ''.join(map(str, rest))