human
Write me a code to minimum number of palindromes required to express N as a sum and the other steps required given the following:
Minimum number of palindromes required to express N as a sum | Set 2 | A utility for creating palindrome.
checks if number of digits is odd or even if odd then neglect the last digit of _input in finding reverse as in case of odd number of digits middle element occur once.
Creates palindrome by just appending reverse of number to itself.
Function to generate palindromes.
Run two times for odd and even length palindromes.
Creates palindrome numbers with first half as i . Value of j decides whether we need an odd length or even length palindrome ..
Function to find the minimum number of palindromes required to express N as a sum.
Checking if the number is a palindrome.
Getting the list of all palindromes upto N.
Sorting the list of palindromes.
The answer is three if the control reaches till this point.
Driver code"
Solution in Python:
assistant
#Here is code to minimum number of palindromes required to express N as a sum and the other steps required.
 def createPalindrome ( _input , isOdd ) :
	n = palin = _input
	if isOdd :
		n //= 10
	while n > 0 :
		palin = palin * 10 + ( n % 10 )
		n //= 10
	return palin
def generatePalindromes ( N ) :
	palindromes = [ ]
	for j in range ( 0 , 2 ) :
		i = 1
		number = createPalindrome ( i , j )
		while number <= N :
			palindromes . append ( number )
			i += 1
			number = createPalindrome ( i , j )
	return palindromes
def minimumNoOfPalindromes ( N ) :
	b = a = str ( N )
	b = b [ : : - 1 ]
	if a == b :
		return 1
	palindromes = generatePalindromes ( N )
	palindromes . sort ( )
	l , r = 0 , len ( palindromes ) - 1
	while l < r :
		if palindromes [ l ] + palindromes [ r ] == N :
			return 2
		elif palindromes [ l ] + palindromes [ r ] < N :
			l += 1
		else :
			r -= 1
	return 3
if __name__ == " _ _ main _ _ " :
	N = 65
	print ( minimumNoOfPalindromes ( N ) )