Skip to content

Instantly share code, notes, and snippets.

@Logic2apply
Created November 17, 2021 15:11
Show Gist options
  • Save Logic2apply/da0ed1f9753d2ba0b1df27d2c00d18b1 to your computer and use it in GitHub Desktop.
Save Logic2apply/da0ed1f9753d2ba0b1df27d2c00d18b1 to your computer and use it in GitHub Desktop.
The Next Palindrome. A palindrome is a string that, when reversed, is equal to itself. Example of the palindrome includes: 676, 616, mom, 100001.

The Next Palindrom

A palindrome is a string that, when reversed, is equal to itself. Example of the palindrome includes:

  • 1001
  • 333
  • mom
  • dad, etc.

You have to take a number as an input from the user. You have to find the next palindrome corresponding to that number. Your first input should be the number of test cases and then take all the cases as input from the user.

Input

Enter number of test cases: 5

  1. Enter testcase: 198
  2. Enter testcase: 45
  3. Enter testcase: 37
  4. Enter testcase: 48
  5. Enter testcase: 95

Output

The next Pallindrom for 45 is 55.

The next Pallindrom for 37 is 44.

The next Pallindrom for 48 is 55.

The next Pallindrom for 95 is 99.

# Take Number of testCases
testCases = int(input("\nEnter number of test cases: "))
inputs = [] #empty list for storing inputs
# Generate Inputs
for i in range(testCases):
numForPallindrom = int(input(f"{i+1}. Enter testcase: "))
inputs.append(numForPallindrom)
outputs = [] # empty list for storing outputs
for data in inputs:
while True:
data += 1
init = str(data) # Convert integer into str to make it reversible
final = init[::-1] # Reverse the initial value
if init==final: # Check if initial and final value is same
outputs.append(init) # Append it to outputs for printing with inputs
break
else:
continue
for dataInp, dataOut in zip(inputs, outputs):
print(f"The next Pallindrom for {dataInp} is {dataOut}.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment