Palindrome


def is palindrome(number):

#Convert the number to a string for easy comparison num str = str(number)

#Check if the string is the same when read backward

return num str == num_str[::-1]

def count digit occurrences (number):

Convert the number to a string for easy iteration

num_str = str(number)

Initialize a dictionary to store digit occurrences digit_occurrences {}

Count occurrences of each digit

for digit in num str: digit_occurrences[digit] = digit occurrences.get(digit, 0) + 1

return digit_occurrences

if name main ":

try:

#Get input from the user

input_number = int(input("Enter a number: "))

#Check if the number is a palindrome

if is palindrome(input number): print(f"(input number) is a palindrome.")

else: print(f"[input_number} is not a palindrome.")

Count occurrences of each digit

occurrences = count digit occurrences (input number)

print("\nDigit occurrences:") for digit, count in occurrences.items(): print(f"Digit (digit): (count) times")

except ValueError: print("Invalid input. Please enter a valid integer.")

1 Answer

1 year ago by

.

1 year ago by mohammed fahmi don