A python program to find palindromes! (algorithm code How to) (blog python)

Python palindrome finding algorithm! 

Hey guys, I often find myself bored and scrolling through the internet for something to program with python and correct me if I'm wrong but I'm pretty sure I'm not the only one who has done this. And I recently got quite interested in creating math-related algorithms. Now I know how that sounds math? no way! But bare with me it's super easy and when you add math to programing which you already like trust me it can get fun.


What is a palindrome?

So I'm guessing if you don't already know what a palindrome is you would like to know. Well is pretty simple the google definition for a palindrome is (a word, phrase, or sequence that reads the same backward as forward) so this would be things like the name Hannah or the number 5335. And in this case, we will be working with numbers.


The code explanation!

So let's get into the code for this project. Well be making a 28 line simple terminal python program it will have 2 functions one to reverse numbers and another to find the next closest palindrome. Now let's explain the algorithm! First, we need to get the number we start from. We will start from this number and find the next closet palindrome. We then add and subtract an amount from the amount we started from and check if it's a palindrome! If that did not make very much sense don't worry I'll explain the code as we go on!

The code how to!

So let's get into the code! first off we need to get our start number this is the number we will find the next closest palindrome to. And we can easily do this with inputs in python so add this line to the code
This code simply asks the user for a number which we will use as our starting number. 

Now let's create our function to reverse numbers. We need to reverse numbers so we can check if they are the same reversed as they are not reverse which would tell us if they are a palindrome or not. To do this add this function to your code!

Ok, so now that we have what we need for prep we have to create our algorithm, and ill walk you through this step by step so it won't be hard at all! First, of  we need to make our function so add this code to your script 
we pass in the number which will be our starting number

Now let's make 2 variables in the function. These variables will be used to keep track of how much we will be subtracting and adding from our main/starting number. So basically we subtract/add from these variables and then check if that number is a palindrome.


Great, we now need to add some logic but first let's make a while loop for this to go into so it runs till it finds a palindrome. Adds this loop with the extra code into the script
the extra code just adds to the variables before making sure we are actually checking a new number every time.

ok, now we can get into creating some logic! we need to check if the number we are checking backward is the same as the number we are checking forwards. And this can be done easily with two if statements so add this to the script yours!
this code just checks if the number reversed is the same as the number not reversed. If it is then print our results and break out of the loop, and If not keep going.


Finally, we just call the function with our number like so 
And we are done!



Final thoughts

I hope you enjoyed my first acritical here on blogger and learned something new. I plan on posting here often in the feature and sharing what I have done and how I made it. If you liked this it would be cool if you could leave a comment or if you have any thoughts or questions id be happy to answer them. And with that keep on creating and exploring and ill be heading off for now!

Git and socials





thank you.

Comments

  1. This was good fun to make if you have any comment go ahead and leave them here! Thanks 😊

    ReplyDelete
  2. Palindrome numbers are really interesting!

    I think you should explain a bit more on what exactly your math is doing. =)

    ReplyDelete
    Replies
    1. Yea they are very cool!

      Thanks for the input I might edit this blog later and explain it more!

      Delete
  3. This looks like a whole lot of fun to make! glad you shared it with us!

    ReplyDelete
    Replies
    1. Np man I'm happy to share things like this and I'm glad you could enjoy!

      Delete

Post a Comment

Popular posts from this blog

5 amazing sites where you can build your coding knowledge!