human
Represent the fraction of two numbers in the string format | Function to return the required fraction in string format.
If the numerator is zero , answer is 0.
If any one ( out of numerator and denominator ) is - ve , sign of resultant answer - ve ..
Calculate the absolute part ( before decimal point ) ..
Output string to store the answer.
Append sign.
Append the initial part.
If completely divisible , return answer ..
Initialize Remainder.
Position at which fraction starts repeating if it exists.
If this remainder is already seen , then there exists a repeating fraction ..
Index to insert parentheses.
Calculate quotient , append it to result and calculate next remainder.
If repeating fraction exists , insert parentheses ..
Return result ..
Driver code"
Solution in Python:
assistant
#Here is code to represent the fraction of two numbers in the string format and the other steps required.
 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
if __name__ == " _ _ main _ _ " :
	num = 50
	den = 22
	print ( calculateFraction ( num , den ) )
	num = - 1
	den = 2
	print ( calculateFraction ( num , den ) )