A pattern finding python algorithm! (create algorithm How to) (blog python)

Pattern finding with python



Hey guys, hope you are all doing well! Today I am gonna show off a simple yet fun python pattern-finding algorithm I made. It's not the most advanced thing you will ever see but I think you could learn a lot from it and I challenge you guys to add your own spin to it. Because real learning comes from experience!

How is this going to work?

This project will be taking a string of letters like "ABCABCABC" and finding a pattern in them. So in this case, "ABC" is the pattern. To find this pattern we need to use a bit of common sense first. 

To start off we already know that the first letter in the pattern (it will be "A" for this example) will be the first letter in our string. so using this knowledge we can determine that if we find the next "A" (start letter) that will mark the end of one pattern and everything behind it is the pattern. But there can be exceptions for this, for example, the pattern ("ABAA-ABAA-ABAA") ends with two of its start letters so we need to add a workaround to this issue in our code. We do this by asking if the pattern we now have is the same as the next few characters in our text. If it is then we can say the pattern is ok and show it, but if it's not we need to keep adding one more letter from the text at a time until it matches the next few characters in the text. when it does we can present it!

ok so now that you know how the algorithm works we can get to programing it!. And don't worry if you are still a little confused. I'll explain it step by step and at least for me things usually make more sense when you write them out in code!

The code (How to)

Ok now let's get into creating the code!
First off we need to get our string of letters we will find the pattern in. We can just do this with input! So just add this code to your script

Now as I mentioned before we know that the first letter in our pattern will be the first letter in our text.
So we need to make a new variable and have it include the first letter of our text in it! We can do this by using indexing with python.
 If you don't know what this is its a pretty broad topic so if you want to learn more check this out

And now we just need one more thing before the algorithm! a variable called "full_pat". This is where we will store our final pattern and it's also what we will display to the user.
so just add this to the script!
Cool! now let's get into the algorithm. We can start with adding ours for a loop. this will run through the entire text we have checked every single letter. so just add this to the code
Nice now let's get into the logic here. first of we need to see if the letter we are on now is the same as the first letter in our text. We need to know this because if it's not we can add the letter we are on to our full_pat variable. We can do this with a simple if statement just like this! 
This code is here because as I said before we know that the first letter in the pattern needs to be the first letter in the text. So with this logic, we know that the next time we see this letter we can assume that the pattern may have just ended. 
Ok, now let's handle what we will do if the number we are on is the same as our first letter in our text. First off need to add the else statement. And then we need to check if the pattern we have now is the same as the next few characters. If it is then we can assume that we have the right pattern. so just add this code to your script!
Now let's say that we don't have the right pattern. Well, we can at least assume that we are close! So we can just add one more letter one by one then check if that is the correct pattern. So just add this code!
And that's it! now we just need some mini logic outside the loop to handle text without patterns. So we just make sure that the pattern is right by matching the pattern to the text. If it won't match we just tell the user that there is no pattern in this text! We do that with this code!

Final thoughts

And that is for this one! Thanks for watching guys and hope you enjoyed it! if you did I would really apricate it if you left a comment! It helps me grow and just makes me happy! If you don't know what to comment then you can just say "cool blog!". And besides that, if you have any thoughts or questions id be happy to answer them. Thanks for reading and with that keep on creating and exploring and ill be heading off for now!

Git and socials





thank you.

Comments

  1. thanks for reading! if you liked it leave a comment letting me know or follow the blog!

    ReplyDelete

Post a Comment

Popular posts from this blog

5 amazing sites where you can build your coding knowledge!