A friend of mine told me privately: "I don't like palindromes". "why not?" - I replied. "Because when I want to do some programming challenges, I encounter 2 or 3 ones first related with palindromes. I'm fed up" - he confess me with anger. I said to myself:"Thankfully, that doesn't happen in Codewars".
Talking seriously, we have to count the palindrome integers. Doing that, perhaps, it will help us to make all the flood of palindrome programming challenges more understandable.

For example all the integers of 1 digit (not including 0) are palindromes, 9 cases.
We have nine of them with two digits, so the total amount of palidromes below ```100``` (10^(2)) is ```18```.
At least, will we be able to calculate the amount of them for a certain number of digits?
Let's say for ```2000``` digits?
Prepare a code that given the number of digits ```n```, may output the amount of palindromes of length equals to n and the total amount of palindromes below 10^(n).

You will see more examples in the box.
Happy coding!!

(You don't know what palindromes are? Investigate, :))

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Combinatorics

Mathematics

Starter code: def count_pal(n):
	

Solutions:
def count_pal(n):
	return [9 * 10 ** ((n - 1) // 2), 10 ** (n // 2) * (13 - 9 * (-1) ** n) // 2 - 2]


def count_pal(n):
	x = lambda y: 9 * int(10 ** ((y - 1) // 2))
	return [x(n), sum((x(i) for i in range(n + 1)))]


from itertools import accumulate
N_PALS = [0] + [9 * 10 ** ((n - 1) // 2) for n in range(1, 2001)]
S_PALS = list(accumulate(N_PALS))

def count_pal(n):
	return [N_PALS[n], S_PALS[n]]


def count_pal(n):
	pal = lambda n: 9 * 10 ** ((n - 1) // 2)
	return [pal(n), sum((pal(i) for i in range(1, n + 1)))]


from functools import lru_cache

@lru_cache(maxsize=None)
def count_pal(n):
	if n == 0:
		return [0, 0]
	current = 9 * 10 ** (n - 1 >> 1)
	previous = count_pal(n - 1)[1]
	return [current, current + previous]


count_pal = lambda n: [int('9' + '0' * (n // 2 - int(not n & 1))), int([['1', '10'][n & 1] + '9' * (n // 2 - 1) + '8', '9'][n == 1])]


def count_pal(n):
	res = '1' + '0' * n
	temp = '9'
	l = []
	for x in range(1, len(res[1:]) + 1, 2):
		q = res[x:x + 2]
		for _ in q:
			l.append(temp)
		temp += '0'
	return [int(l[-1]), sum((int(x) for x in l))]


def count_pal(n):
	total = 0
	for i in range(n):
		n_digit = 9 * 10 ** (i // 2)
		total += n_digit
	return [n_digit, total]


count_pal = lambda n: [10 ** (n - 1 >> 1) * 9, 10 ** (n - 1 >> 1) * (20 - n % 2 * 9) - 2]