Python Challenges: WiErD StRiNg CaSeS

Transcript

English (Auto-generated)

nobody is doing really well. So uh in this Congress we're gonna be doing a bit of a more difficult challenge is not really super difficult. Anyway, it's still very beginner friendly. And it basically consists of not that much knowledge of strength, mainly just knowledge of basically reading instructions really well. So basically we have to we have a string and we have to return that string with all the even index characters upper case and all the odd ones lower case. And each time that we begin a new word, as in like after a space, the index starts from the beginning. So let's let me give you a quick example. Let's say we have a string that's just um the word. So this would be this would be index zero then one and two. And since this new work it will start in 01 and two just like that. So yeah, so I'm gonna be using indexes quite a bit. We have a index counter. I'm gonna index, it's gonna be zero. And then there's a lot of ways you can do this. I mean for example you could change the string itself but I'm gonna be creating the and string with it. Okay. And then we're gonna loop through for a letter and actually I want to make this word, I'm gonna name this bird The four Letter and Word. We're going to, first of all, the first thing we want to do is check if it's a space or not because it's a space. We're just gonna make the index zero again. Right? So we're gonna do here. My indexing is all we're gonna say okay to do this and this letter if letter is equal to a string then we're just gonna Make the in Mexico zero next zero right there. Because you want to start from the beginning every time when we have a string and then but we still want to add string to the new work. Thank you. So in the class B quote we can either do string like an empty string, a little string or we can just put add letter because if letter is equal to the string we just put better. Okay and this is where the actual even or odd thing comes in. So we're gonna do l if um index module two is equal to zero. Right, explain this in previous code cats? But basically what module does gets the remainder of the division between index and two in this case. So let's say index was equal to 00 divided by two is equal to the remainder of it is zero. Same thing with two divided by two the remainder zero. And that's how you know if this is even basically I'm not gonna get into details what an even or odd number is. So so if that happens we want to make um a new live make and no letter is equal to or actually like letter We pulled two letter that birth which is basically a function in python where it just makes it uppercase same thing with lower case letter and then we're gonna add that letter to new words plus equals like okay let's speak to me and else basically if it's an odd number we're gonna do letter dot letter equals letter dot lower this time. Actually I'm not going to make them the same variable due up letter here and then we blew up right there and I'm gonna call this one low low to make it is a little more readable. So that we're gonna do we're gonna have a word plus low letter to add that letter to a new strength. Now we're just gonna return no word that should work perfectly. Now we're just gonna here to print men weird streaming case and then let's give it a string that says this is a sentence. Okay so we're gonna do is we're just gonna pass through this really quick and just kind of put you in a comment How they should turn out. So t should be capitalized because X. zero it shouldn't be because one is not any number then I should be capitalized because you need a number as you can kind of see the pattern here. And since the start of a new word I is going to be capitalized because indexes once again zero that should not be capitalized. New word they should be capitalized because it's zero. Again your word should be capitalized because index is zero, he shouldn't be and should be t shouldn't be so odd. I'm just gonna write that really quick now. Like so this is a sentence. So now I'm gonna go ahead and write from three here and are coming up and then us showing up with this. Okay. Oh dr hold it right there. Okay. And now how are we doing with high? Right, okay. And let's do it. Oh okay. Okay. So I'm gonna go into the file into the folder that I wanna be in. Okay. Okay. It says okay C. D. Or a strange case. Just checking except for string. Okay. I'm just oh because it's not capitalized. Okay. Sorry I apologize. Okay, I'm gonna get that. Okay. What I'm gonna do is just called rename this folder because this is getting too complicated. Weird. Mm. Oh it's not learning code. Okay. We're gonna try to run this on idle because this is clearly not working. So copy that. Okay, I'm gonna try to fix this problem really quick. I apologize. So it says that you cannot accept. Okay, sorry to be a sec guys for some reason. Okay, I see what I need to do. Okay, thank you so much guys for waiting for me. So um now let's try to run this. Okay, so we're gonna try and run this code really quick. Let's see you see that guy. Oh wait, I need to put 500 man that really caught me off guard and file thing. I apologize. Not again. This is a sentence. Okay, so it has a case. Let's see what's going on. No, the classic actually had this problem when they first tried to do it in code wars because by the way credits code wars to whoever made it. But when I first tried it, I forgot completely to um change the index. So now we're adding one to the index this time so it should work and look okay, okay, so let's see this is a sin. So this is correct. Is am sentence isn't and I can see that the problem is basically that were knocked at the checker of the space is basically not working. So let's just see here really quick what we're doing. Oh yeah, I remember now. So it starts at zero and What was I gonna say? And it adds one Basically. So basically what we're gonna do is make this negative one and that should have worked. Okay, so this is a sentence and as you can see this is just copy and paste it here. So you guys can see it better. Yes. And it's the exact same thing. So we can try this with something else for that. But I think we've basically got the gist we basically also wanted to check it with multiple words just to check things like this for example where the space all and all that got very confusing. But yeah, this is how you would solve this problem. And um if you guys have any fabric Factory ization that you guys think of doing like there's so many ways to re factor this and different ways to solve this obviously. But um, this is the way that I found efficient and honestly looks very readable to me. I think that's kind of the goal when you're writing code in python. So yeah, thank you guys so much for watching and goodbye.
72 Views 0 Likes 0 Comments

Basically in this codecast we're going to be covering a little bit of a more difficult challenge that's still very beginner friendly :) It involves not...

Comment
Leave a comment (supports markdown format)