← Back to challenges

Almost-Palindrome Sieve

PythonHardarraysstringshigher_order_functions

Instructions

Create a function that takes in a list of integers and returns the integers that are either palindromes or almost-palindromes. An almost-palindrome is any integer that can be rearranged to form a palindrome.

For example, the numbers 677 and 338 are both almost-palindromes, since they can be rearranged to form 767 and 383, respectively.

Examples

palindrome_sieve([443, 12, 639, 121, 3232]) ➞ [443, 121, 3232]
# Since 443 => 434; 121 is a palindrome; 3232 => 2332 or 3223

palindrome_sieve([5, 55, 6655, 8787]) ➞ [5, 55, 6655, 8787]
# Single-digit numbers are automatically palindromes.

palindrome_sieve([897, 89, 23, 54, 6197, 53342]) ➞ []

Notes

Return an empty list if none of the numbers are palindromes or almost-palindromes.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.