🛠️ToolsShed

Verificatore Palindromi

Controlla se una parola, frase o numero è un palindromo.

Domande Frequenti

Implementazione del Codice

import re

def is_palindrome(text: str, ignore_case=True, ignore_spaces=True) -> bool:
    """Check if a string is a palindrome."""
    cleaned = text
    if ignore_spaces:
        # Remove all non-alphanumeric characters
        cleaned = re.sub(r'[^a-zA-Z0-9]', '', cleaned)
    if ignore_case:
        cleaned = cleaned.lower()
    return cleaned == cleaned[::-1]

# Examples
print(is_palindrome("racecar"))        # True
print(is_palindrome("Hello"))          # False
print(is_palindrome("A man a plan a canal Panama"))  # True
print(is_palindrome("Never odd or even"))            # True
print(is_palindrome("12321"))          # True
print(is_palindrome("Was it a car or a cat I saw"))  # True

# Find all palindromic words in a sentence
def find_palindromes(sentence: str):
    words = sentence.lower().split()
    return [w for w in words if len(w) > 1 and w == w[::-1]]

print(find_palindromes("The racecar level went noon to noon"))
# ['racecar', 'level', 'noon', 'noon']

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.