Home « Palindrome

So now we're going to look at 4 examples of solving a particular problem and this


Will demonstrate the speed of each and the variation of how we solve it so I'll give you an idea what we're gonna be doing the rest the class as we look at different solutions for a problem and we are also going to show you one of the pitfalls of solving a problem that you have to look out for. So are talking about anagrams here's an example for him in a gram hearten earth I and here's another one tight on time typhoon and so what is an anagram you have two strings and its you could take the first ring and rearrange it in some way and get the second-string so struck in string the first ring have alt same characters and they're just a rearrangement up the are the months from one screen to the other are the strings don't have to be actual english words but usually when you're solving puzzles you'll give you'll be given two words for technically that's an n-gram so we look at different approaches and look at what are how much it cost us to do each approach so the first lucien he calls checking of and the idea is that we have two strings so we're going to are look at the letter H in the second string and check of what we find it so when you find it we're gonna check it off and we can check it off rely replacing the age with a non now we can't do that in a string the two mutable mean we can't modify anything in this crying so what we're going to do is convert these from strings to list and mister not immutable so we can actually replaced H in the first with non an so we go to the animate check often find the EU are the firstly we found and then we find today evening on check out of and so on and if we're all done we've checked everything of your and we've also finish going through this whole spring here we know it's an anagram now we're gonna show you two versions though version the book author did and the version I've done have tried to simplify some things a little user code understand and we're gonna take are some advantages of things for small if these two strings are not equal they automatically or not anagrams that's what something we can test immediately industry turn we find that they're not equal are so let's go ahead and see the author solution and are we go back and do so what you gonna do is you gonna pick the H and then for this age you're looking through the string and then you gonna pick the and for the economy looking string so you can see that we're gonna have a loop but I loop through the first string one at a time and then inner loop so that when the first ring is on the H it'll start looping through all the letters are the second-string so that's basically what he does here so he had she all these are gonna define an interim solution or two strings so here this is where it converts a string to a list and this is going to be useful to remember how to do anything that's a string you can convert to a list to the individual characters sold he's gonna you some variables cue track up where he is and each list so position one more point to where we are and the s1 worst an position to a point where we are and the s2 or the a-list and we only have two at nine today with us we don't have to are convert s 12 at worst and we have a flag that so it's still okay means we're still looking for an anagram and so while position one in the first ring is lost in late to the string and still okay to gonna reap set the pointer position are two in the second string 20 search found two faults we have another flag we're gonna set when we actually find it and then while position to pointing to the second string is less than a link to that list and not found we're gonna checkers the is the screening at position 1 equal to the list position to after two years we want to such found a true and when we sat down the true its gonna pack go back up to the while an hour cause the loop to terminate early if it's not equal then we increments opposition to a look at the next character in the list to see you if the what we're looking for from the first list is in the second position and so on so this will loop through looking at the second-worst are and here's the outer loop which is looping through the first screening now once we've done through all the way through the list we check found if we never found what you're looking for are families know if we did find what you're looking for we set position where we found it 29 so that's gonna market or check it off if we never found it we set still OK to faults that means we went through are this whole less time we never found a particular character that means it can't be an anagram so still a case at faults its gonna cause this outer loop to quit I and then finally we increment the outer blue we're from an the pointer where we are in the first ring by one and repeat outer if the outer loop still okay becomes faults or weaved run out screaming check we exit outer loop and wait to return still okay nets can return to your fault tan and brown are your cell the way I wrote it so on going to in a gram 1 so the way I wrote it for such check if the link to the two strains are not equal and if it is I returned faults an 10 I set in the quarter to the link to the first ring and I make a new list I called s2 worst so it s too was a list and it for each character in s1 then for each hi in the range %uh the link to the STU so I will be an index into the second list excuse me so if the character from the first are string is equal to bite position at the second-worst then we market of and then we r document and buy one so Ennis keeping track old are started out with the link to the second-string and so every time we str actor were marking off if we marked off everything in this second-string we know we're done and we found an anagram so that's going to be our flag which Jack I when you're all done so we don't mean it don't actually have any flags are but we actually found the character we can break out of the loop sold break you members a special key word in Python works just like in Java and C plus plus which will break out the inner loop and go to the next character the outer so what we gone through all the characters are the outer loop are then we just return it in a zero it's an anagram so checks return an equal 0 if it's not zero we didn't check turning off and so it's not a nanogram now we're gonna see more this this is testing so what I did as I made a top of tuples is called the test cases so each tuple that's inside as a whole list to top holes are three things each tuple Thursday word to check and a first ring the second string and what we should expect it to be so this is actually to anagrams these are anagrams ABC with an empty string is not an anagram to empty strings were gonna know just that they're true now this particular case it turns out the authors are I think daughters example fails at it or fails at one of these were you have an empty string are so you don't have to have this work all the time for empty strings is just doesn't make sense but it does work on our scores we test to the springs or he call and with check-in is early and so will be so the sole return true I and we have a bunch other test cases and so what we do is we loop through this is a special our wave looping through topple so we have this is a list of tuples so each particular piece %uh this is a tuple and so we're using that assignment where you can list three variables on the left so inside the top all s1 as to EU will be assigned to these three things in order so this is in the wave using a for loop on a plane out and they were using the assert statement and what the research statement doesn't play it on is it's always a cert and then something is true or false and then you can have optionally a string so I have a format string here which is %uh like an error message solar search you can add a bunch a search and if your program is it all these assertions of this are true assert doesn't do anything you won't print anything it doesn't stop your program so this is a lot of times how your test a program and we'll see this one you do fraction arm that when you did fraction the previous chapter is asserts so if our anagram solution is equal to what is expected the thats whats asserting if it's not it it will output in error message quit the program and their messages space layout putting what the two strings returned were so you know what data failed the test so stock up a little bit about big old this because we're were stepping through every character the first ring and then we have another look to the stepping sequentially through every character the second-string analyst art this is two nested for loops this one's going from one day in and this is going to one and where the end is the size of the problem so I put their own notes here this is if you counted how many assignments is one assignment here there's one assignment to make a new list you could argue that's actually an operation are then there's an square in squared assignment are this 102 and it depends on how far idk checks how far gets analyst can we have in squared here and these are all in square because you're inside at the nested who and then there's one assignment when rolled on and I didn't even count that so this this I've put a limitation as to what polynomial all this adds up to but the main thing you'll see is the biggest term I use upchurch for power so we have if we simplify this it's to an squared plus and plus two so the property predominant turner's in squared so this is all 11 squared