human
Sum of two numbers where one number is represented as array of digits | Function to return the vector containing the answer.
Vector v is to store each digits sum and vector ans is to store the answer.
No carry in the beginning.
Start loop from the end and take element one by one.
Array index and last digit of number.
Maintain carry of summation.
Push the digit value into the array.
K value is greater then 0.
Push digits of K one by one in the array.
Also maintain carry with summation.
Reverse the elements of vector v and store it in vector ans.
Driver code.
Print the answer"
Solution in Python:
assistant
#Here is code to sum of two numbers where one number is represented as array of digits and the other steps required.
 def addToArrayForm ( A , K ) :
	v , ans = [ ] , [ ]
	rem , i = 0 , 0
	for i in range ( len ( A ) - 1 , - 1 , - 1 ) :
		my = A [ i ] + ( K % 10 ) + rem
		if my > 9 :
			rem = 1
			v . append ( my % 10 )
		else :
			v . append ( my )
			rem = 0
		K = K // 10
	while K > 0 :
		my = ( K % 10 ) + rem
		v . append ( my % 10 )
		if my // 10 > 0 :
			rem = 1
		else :
			rem = 0
		K = K // 10
	if rem > 0 :
		v . append ( rem )
	for i in range ( len ( v ) - 1 , - 1 , - 1 ) :
		ans . append ( v [ i ] )
	return ans
if __name__ == " _ _ main _ _ " :
	A = [ 2 , 7 , 4 ]
	K = 181
	ans = addToArrayForm ( A , K )
	for i in range ( 0 , len ( ans ) ) :
		print ( ans [ i ] , end = " " )