Algorithms in Python – Full Course for Beginners

Accumulated each row to end with six three one we then reassign our original row as the final row and return the very first element which will be six our answer to the number of paths now this isn't super necessary but it will give you an idea of the path traversal within a three by three matrix and will give us a good launch point to moving up to a matrix consisting of letters we'll keep it in the realm of just two letters a and b and we'll write a very quick little program to see how many palindromes of a and b there are let's import intertools to help us and then create our rows and columns of letters a neat little feature of inter tools is product with an asterisk on our matrix this will print out every combination of letters in our matrix totaling 27. yes there will be duplicates here but it's nothing that python's set can't handle let's create an empty list called p1 and then iterate over our list of 27 tuples join them together to make it easy on the eyes and append them to our empty list then all that's left to do is check which ones are palindromes by making a new function plugging in our p1 create another empty list p2 and then iterating over a non-duplicate set of p1 incorporate an if statement is it a palindrome yes put it in our empty list then return it okay so far we've traversed a matrix in order to count the number of paths in it we created a three by three matrix of just two letters found every possible combination and created a function to pull the palindromes from it we are doing fabulously

If you know how to count the paths of a matrix and find the palindromes in a matrix you're totally equipped to take on the palindromic paths problem let's get cracking so before we start i have to tell you there's going to be a little bit of a difference here for this program it's the order of traversal we're going to go from the top and right to the bottom not the bottom up and left to the top so keep that in mind as we march forward we're going to throw in our little palindrome checker up here first before we print them out at the end of our function hopefully by now you're familiar with that next we're going to create our main function with several parameters a string that will be empty but we're just going to call it string here a for our array our pointers that we'll call i and j our row and column that we'll call m and n now our conditional if statements are going to traverse the paths of our array we minus one from m and n because as you know this is python and we only need to iterate from indices starting at zero then one then two for our total size of rows and columns three by three see the i plus one and the j plus one within our recursive callbacks look at these slides to see the paths you could also print i and j within the conditional statements on your own to see it as well but when we add one to our pointers we're moving to the next element then once the traversal is completed it hits our else statement and that hits up our palindrome checker if it's true it'll print then it moves on to do the next reversal over and over until it's done and there you have it four palindrome strings in our matrix yay so what did we learn on our final lesson we learned about dynamic programming which is finding the optimal solution to sub problems that can be used to find the optimal solutions to the overall problem we saw this in our lesson with ugly numbers the traveling salesman problem and palindromic matrix paths in the easy lesson we learned about ugly numbers what they are and how to write a program to find the nth ugly number in the sequence avoiding brute force and recursion we were able to implement dynamic programming to optimize the solution from over one thousand iterations down to just a little over one we looked at the very famous traveling salesman problem with its main focus in optimizing a solution to finding the most efficient route a solution used across the board for so many companies in order to keep operational costs low and lastly we broke down the mind breaker lesson by studying unique paths in a matrix string palindromes within a matrix and combining those two problems together to find all the palindromic paths within a matrix are you excited i know that i am we covered so much ground in this course it's crazy hopefully you took lots of notes so you can practice later you should be coding something every day even if it's just for a couple minutes 10 to 15. since every little bit helps now listen to your mother thank you so much for joining me today and if you made it all the way here kudos to you i wish you all the best in your programming journey bye