 Alright, what's up everybody? My name is John Hammond and in this video We're gonna be cranking up the solution to that spelling states challenge that I released just the other day So it's asking us Well, there are 50 United States and each of them contains at least one of two particular letters One being a constant and the other being a vowel. What are these letters and what states don't use them? So to actually work with this I'm actually gonna be using a Python library that has all the United States in them So I think it's a if I just Google Python United States and we'll show us this US module Okay, cool. It does show it down at the bottom At least in one of those results. I saw earlier. It does have a Package that is full of United States States territories postal abbreviations other information blah, blah, blah We just need to be able to get the state names easily and real quickly So if you don't have that installed you should be able to just pseudo pip install us Enter your password and then it should be able to crank it out for you. Mine's already done So let's get started and actually import that module import us and You can access all the states. I'm sorry just by that module name and then capital States so just cranks all out for us and we'll work through it as a list So let's actually get those in a working form. Let's say states. I'll do a little bit of list comprehension here We will actually take the state that we retrieve for state in us dot states and That's actually just going to be we want to be able to like actually turn this to a lowercase stuff So if I were to print out states Right now it's just all in the state form that the module works for us, but we just want it as a string So I would want the lowercase version of it, but it's going to have a problem when it's not in a string form So let's cast it to a string and now they're all lowercase That works just fine. So we can work with them, but they all have You'll notice the space characters in them if they happen to be multiple words like New Hampshire, New Jersey, New Mexico So let's actually Remove all of those space characters of the simple replace line So now I've got all the states working for us just fine And now let's crank out the vowels and constants. So we since we know that is the other data We want to be working with I know those vowels are a e i o u and Now we have the vowel set. Let's actually get the constants We're gonna want those out of the entire alphabet. So let's import string as well Just so we can easily work with all of the alphabets I'm gonna use string ascii lowercase And I'm just setting this to a constants variable right now But we're gonna replace and take all the vowels out of our consonant string So for a vowel in vowels what we can do is we can say consonants totally equals consonants Once it's been replaced With the vowel that we're currently looking at with nothing so Now we can print out vowels and consonants and they should be a e i o u and then all of the Constants without the vowels in them That works just fine for us now since we have all the possibilities of vowels and consonants We're actually going to be all want to we don't want to be able to like piece them together one by one and I'm going to actually do that with I'll show you it's part of the iter tools module It should be the product of a list Python iter tools product Product will reduce the Cartesian product. So if we put together two lists It'll give us all the possibilities of those to kind of put together and I'll let you look more into that if you need to but in our case It does exactly what we needed to by kind of jamming together all the possible consonants and vowels So let's get that module. Let's from iter tools. Let's just import Product because that's the only function we need So now that we've generated the Constants and the vowels we can actually start to loop through them as possibilities So for a consonant and vowel in a product of consonants and vowels Now we can see for one thing. Are they all part of the states that we're looking through so for state in states We're looking through every single state if The consonant that we're looking at currently since we're looping through a bunch of them here I'll actually just print these out. So you get a understanding what it is that I'm talking about We'll see in the big list here I'm gonna pull the screen up It's gonna go through all the possibilities of the consonants with all the possibilities of the vowels It's just looping through all of them So we're also gonna be looking through the states for each one of these possibilities though Just like I had started to type before in this other for loop if the consonant is in the state or The vowel is in the state we're using or because remember the prompt says it could be either at least one of two Particularly letters one being the constant and the other being a vowel So if the continent or the vowel is in the state, then we know we've got it. We know we got one success But we want to be able to have that success for 47 of the 50 United States, right? It says all but three of the 50 United States have this So let's actually keep track of how many that we've currently found currently that we that we've actually noted to work So let's keep a variable. Uh, I guess restarting every time that we're looking through these states And if we found one we can increment our found variable and if not Well, we'll know that that's one of the states one of the three hopefully if it actually finds the constant vowel pair that we need One of the three that does not contain the vowel and the consonant. So let's actually keep an array of missing states Since that of course is going to be updating every time with our constant vowel pairing else missing states can append The current state we're looking at and since it's lowercase. Let's actually bring it back to you upper case So I'll just call state title on it and Now at the end of our loop we should have Hopefully a found variable filled up all the way to the total number of occurrences that we saw with that constant and vowel pairing Being in all of the states. So since we have all but three of the 50 United States containing these That's obviously going to be the whole 50 states Minus three so we should have 47 possible things that we found right so if found is equal to 47 Then well, we know we've got the right pairing of the constant and vowel so we can print out What constant we're looking at? Consonant we'll do the same for the vowel that we're looking at and We'll actually look at all the missing states as well. I'm gonna put a little Some spaces in here and Missing states right now is an array, but let's just join these with a comma. So it looks kind of nice and With that we should be good. Let's try and run this Hey, okay, we got an answer for us the constant we found was n Looks like the vowel that we found was a and the missing states were Mississippi, Missouri and Ohio Those do not have a currency of n or a and that looks like there are three Looks like it found apparently all the other 47 states did have an n and an a so What we just wrote our solution seems to work just fine What we did was we just took vowels We took all the consonants and then we started to hunt for every possibility of both that constant and that vowel with all the all the different possibilities between one constant and one vowel and We checked is it in every single state? Or at least is it in 47 different states if it is Tell us what that pairing is tell us what constant value we were just looking at and what states did we miss? What ones did it not find it in and that was Mississippi, Missouri and Ohio so Cool, we did it that is the solution for the spelling states challenge we could of course return and break out of this because We're done so that we don't have to keep looping just add a little bit more optimization to our code, but That's it. It's all we had to do I hope you enjoyed this one I thought it was a little bit of a kind of clever solution also using the US module to get all the states listings using the intertools module to take the Cartesian product and getting like a combination of both Constants and vowels hope that was pretty cool. I hope you guys liked this one So thanks for watching and hey, I'll see you in the next the next challenge