Posts

Showing posts with the label palindrome

Finding out next palindrome of numbers in a given list?

 The following program will find out the palindrome of numbers in a given list. If the number in the list is less than or equal to 10, it will be printed as such; if the number in the list is greater than 10, the next palindrome of that number will be calculated. But if the number greater than 10 is already a palindrome, then 1 will be added to it and then the next palindrome of that number will be found. mylist = [1, 6, 10, 11, 88, 535, 5439] plist = [] for number in mylist: if number <= 10: plist.append(number) elif number > 10: # comment out the following if block if palindrome in list to be written as such if str(number) == str(number)[::-1]: number += 1 while str(number) != str(number)[::-1]: number += 1 plist.append(number) print(plist)

The next palindrome for given number(s)

 The following program takes user input as number(s) and tells whether the input is a palindrome. If it is not, it will return the next palindrome for that number. ncases = int(input("How many cases: ")) for i in range(ncases): case = input(f"Enter your case {i+1}: ") if case == case[::-1]: print("This is a palindrome") else: pali = case[:] while pali != pali[::-1]: pali = int(pali) + 1 pali = str(pali) if pali == pali[::-1]: print(f"The next palindrome for case {case} is {pali}")

Palindrome or NOT a Palindrome ?

 The following program takes a user input, be it a number or any word, and tells whether the input is a palindrome or not. ''' Whether a given word or number is a palindrome or not? ''' while True: word = input("Enter a word or number:\n") if word == word[::-1]: print(f"{word} is a palindrome") else: print(f"{word} is not a palindrome")