human
Give me a code to implement * , and the other steps required given the following:
Implement * , | Function to flip the sign using only " + " operator ( It is simple with ' * ' allowed . We need to do a = ( - 1 ) * a.
If sign is + ve turn it - ve and vice - versa.
Check if a and b are of different signs.
Function to subtract two numbers by negating b and adding them.
Negating b.
Function to multiply a by b by adding a to itself b times.
because algo is faster if b < a.
Adding a to itself b times.
Check if final sign must be - ve or + ve.
Function to divide a by b by counting how many times ' b ' can be subtracted from ' a ' before getting 0.
Negating b to subtract from a.
Subtracting divisor from dividend.
Check if a and b are of similar symbols or not.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to implement * , and the other steps required.
 def flipSign ( a ) :
	neg = 0 ;
	tmp = 1 if a < 0 else - 1 ;
	while ( a != 0 ) :
		neg += tmp ;
		a += tmp ;
	return neg ;
def areDifferentSign ( a , b ) :
	return ( ( a < 0 and b > 0 ) or ( a > 0 and b < 0 ) ) ;
def sub ( a , b ) :
	return a + flipSign ( b ) ;
def mul ( a , b ) :
	if ( a < b ) :
		return mul ( b , a ) ;
	sum = 0 ;
	for i in range ( abs ( b ) , 0 , - 1 ) :
		sum += a ;
	if ( b < 0 ) :
		sum = flipSign ( sum ) ;
	return sum ;
def division ( a , b ) :
	quotient = 0 ;
	divisor = flipSign ( abs ( b ) ) ;
	for dividend in range ( abs ( a ) , abs ( divisor ) + divisor , divisor ) :
		quotient += 1 ;
	if ( areDifferentSign ( a , b ) ) :
		quotient = flipSign ( quotient ) ;
	return quotient ;
print ( " Subtraction ▁ is " , sub ( 4 , - 2 ) ) ;
print ( " Product ▁ is " , mul ( - 9 , 6 ) ) ;
a , b = 8 , 2 ;
if ( b ) :
	print ( " Division ▁ is " , division ( a , b ) ) ;
else :
	print ( " Exception ▁ : - ▁ Divide ▁ by ▁ 0" ) ;