I can code this in a crude way but is there some intuitive way using list comprehension or itertools etc?
And also, How to do it if it is given the number is
k digits instead of just 3?
UPDATE: To be more memory and speed efficient, you can replace string formatting and int conversion by
UPDATE 2: And that's for
And that's optimized to NOT use strings!
I used permutations, and a different loop for the first digit, which cannot be
Suggestions to optimize this are welcome!
For the generalisation to
But it isn't very efficient - it generates every possible 3-digit number, and discards the ones that aren't palindromes. However, it is possible to generalise solutions like @jadkik94's - what you need to do is generate every combination of half the length (rounding down), and stick the mirror of it on the end:
Will work for all even