Hour of Python - Coding Challenge 7 _ Number of Things

The Art of Indexing: A Step-by-Step Guide to Solving a String Indexing Problem

As we embark on this coding challenge, I want to emphasize that we're going to approach this problem step by step. We'll be doing it live, and if I make any mistakes, we'll be making them together. The first step in solving this problem is to understand how indexing works. When we try to combine a string with an integer, Python throws a TypeError, indicating that the two types are not compatible. This is because strings can only be combined with other strings.

To solve this problem, we need to convert the integer into a string. Fortunately, Python has a built-in function called `str()` that allows us to do just that. So, if I pass in the string "there" and the integer 5 as arguments to the `str()` function, it will return the string "five". This is exactly what we need to make our indexing work.

Now that we have a way to convert integers into strings, let's apply this to our indexing problem. We want to create an index that lists the items in a list, but with a twist: each item is followed by its type (e.g., "there" followed by "int"). To achieve this, we can use the `str()` function to convert our integers into strings.

Here's an example of how we can do this:

```

return f"There are {index} Trinkets. There is one King."

```

In this code, we're using an f-string (a feature of Python 3.6 and later) to combine the string "there" with the index value `index`. We're also including the type of each item in the output string.

Next, let's talk about how to handle the last item in our list. Since indexing starts at zero, we can use a negative index to get the last item in the list. For example:

```

return f"There are {len(lst)} Trinkets. There is one King."

```

In this code, we're using the `len()` function to get the length of our list (i.e., the number of items), and then using a negative index to get the last item in the list.

One more thing to consider when creating an index: punctuation! In the original problem statement, it's mentioned that we should include a period at the end of each line. To achieve this, we can simply add a `.` character to our output string:

```

return f"There are {index} Trinkets. There is one King."

```

Now that we have all the pieces in place, let's put it together! Here's the complete code:

```

def create_index(lst):

return [f"there are space {i} Trinket." for i in lst]

lst = [1, 5]

index = create_index(lst)

print(index)

# Output:

# ['there are space 0 Trinket.', 'there are space 1 Trinkets. there is one King.']

```

As we can see, our index function returns a list of strings that include the type of each item followed by its value.

And that's it! With these steps and some simple Python code, we've solved the string indexing problem. If you didn't understand this explanation or need further clarification, don't worry – just come back later with a blank brain, and try to solve it on your own. Good luck!

"WEBVTTKind: captionsLanguage: enhey guys how's it going welcome back we are doing again the hour of python coding challenges this is kazy from clever program.com and hopefully the Oxford challenge went really well for you guys in this video what we're going to cover is the number of things challenge this one is actually kind of cool so take a try take a stab at this if you're watching on YouTube pop open your own command line on my website code it right below this video and uh we're just going to kind of jump right into this one okay so the problem basically says if I give you something like um if I give the function how many a list that that is a number like five followed by you know string like trinket it should say there are five Trinkets and if I gave you something like one king it should go there is one king so this is kind of tricky right how do I take this list and then turn it into these human sentences that kind of make sense that's tricky uh and notice it even ends with a period so how do we go about and do all this stuff well one thing that's important for us to know is like how to concatenate strings which is just a fancy way of saying how do we put strings together the things in the quotes So if I have like a string that says hello if I want to add a period after it I could just do something simple like this hello plus that's the concatenation part okay fancy word remember that because when you're at that job interview you can impress them with this but that plus sign is used to combine two strings just like plus combines two numbers right 1 plus one and it combine the hello and the period and turn it into one string not two strings not three strings one string okay so let's go back just like when you do 1+ 1 you don't get 11 think of it like that okay so now what I want to do is I want to take anything that's greater if anything is one then I want you to return the singular version and otherwise I want you to return the plural version we'll keep it simple so I'm going to say if how do we index again give this a try before you see my solution and we are doing this live so if I'm making any mistakes I'm making it live okay we're not going to like edit this because I want us to go through that process of thinking and how we kind of go about solving it so if the first guy is uh one then what I would like to say is um there I would like to say there is so maybe I will say return there is right um and put a space here plus the list and then I'll actually get the number and the number is not a string it's an integer so what happens when you try to combine a string with an integer well let's see if you have something like there are and then I want to add here an integer I get an error and error says type error must be string not int means string can only combine other strings with it they're not they're they have to be like terms all right so in this case h um there are is a string but five is an integer right so what we want to do is we want to convert that five into a string then we could add it and there's a function that allows us to do that so as you can see if I pass in string if I pass in five to the function string then the five turns into a string so what I can do is just call string like this and there you go you get there are five and not that stupid type error that we got last time so return there is and then the list we index a zeroth item zeroth item is going to be the number they give us so there is one and then we want to do plus again here and we want to wrap this in string remember and what I want to say here is the last item okay so I can just do the list and then give me the last item like that or I could be fancy and do this I put a negative one there that goes wraps around gets the last element but we'll keep it simple so I'll just say give me the second element now the only thing that we have to watch out for here is that we need to put that space that all important space so what we're going to do is just do this okay and so this goes there is space one space king okay that's how it should read and then what we'll do for the other one is pretty much the S similar thing okay else we'll have the same statement except we'll switch it with there are and then remember we need to add s and a period at the end okay okay so when we want to say King we want to say Kings period and we want to say trinkets period and here we just want to say period Okay add a little uh that guy right there okay let's give this a try and let's see what happens I could be totally off here there are are five trinkets there is one King five trinket so five trinket should give us there are five Trinkets and we get there are five Trinkets and how many one king should give us there is one king and when we call that on line 19 we do get there is one king all right guys we nailed this one this one was beautifully done we did a great job uh if you didn't get it that is totally fine just close this out come back 30 minutes later with a blank brain and try to do it on your own a lot of the times when you're doing these coding challenges from scratch they don't flow like how I'm doing this right now because right now python is more of a language to me that is like English I'm like thinking in it for you that might not be the same thing you're you're going to struggle with it that's what would happen to me and I would like literally be pulling my hair trying to solve these challenges but just not give up uh so there will you know that headache that you get from sometimes doing these problems is how how I think of it is literally your brain actually expanding so don't take it as a bad sign and if it takes you like 2 three 4 hours to actually solve this that's not a bad thing at all okay don't be like it took C you like 2 minutes it should take me the same time no of course not I've done this more times than you have unless you're a c programmer you probably smoke me too but hopefully this made sense hopefully you were able to solve this you learned something new put your Solutions in the comments below I would love to check them out and so would the other people in at clever programmer anyways guys thank you so much for watching I love you guys so much and as always I'll see you in the next videohey guys how's it going welcome back we are doing again the hour of python coding challenges this is kazy from clever program.com and hopefully the Oxford challenge went really well for you guys in this video what we're going to cover is the number of things challenge this one is actually kind of cool so take a try take a stab at this if you're watching on YouTube pop open your own command line on my website code it right below this video and uh we're just going to kind of jump right into this one okay so the problem basically says if I give you something like um if I give the function how many a list that that is a number like five followed by you know string like trinket it should say there are five Trinkets and if I gave you something like one king it should go there is one king so this is kind of tricky right how do I take this list and then turn it into these human sentences that kind of make sense that's tricky uh and notice it even ends with a period so how do we go about and do all this stuff well one thing that's important for us to know is like how to concatenate strings which is just a fancy way of saying how do we put strings together the things in the quotes So if I have like a string that says hello if I want to add a period after it I could just do something simple like this hello plus that's the concatenation part okay fancy word remember that because when you're at that job interview you can impress them with this but that plus sign is used to combine two strings just like plus combines two numbers right 1 plus one and it combine the hello and the period and turn it into one string not two strings not three strings one string okay so let's go back just like when you do 1+ 1 you don't get 11 think of it like that okay so now what I want to do is I want to take anything that's greater if anything is one then I want you to return the singular version and otherwise I want you to return the plural version we'll keep it simple so I'm going to say if how do we index again give this a try before you see my solution and we are doing this live so if I'm making any mistakes I'm making it live okay we're not going to like edit this because I want us to go through that process of thinking and how we kind of go about solving it so if the first guy is uh one then what I would like to say is um there I would like to say there is so maybe I will say return there is right um and put a space here plus the list and then I'll actually get the number and the number is not a string it's an integer so what happens when you try to combine a string with an integer well let's see if you have something like there are and then I want to add here an integer I get an error and error says type error must be string not int means string can only combine other strings with it they're not they're they have to be like terms all right so in this case h um there are is a string but five is an integer right so what we want to do is we want to convert that five into a string then we could add it and there's a function that allows us to do that so as you can see if I pass in string if I pass in five to the function string then the five turns into a string so what I can do is just call string like this and there you go you get there are five and not that stupid type error that we got last time so return there is and then the list we index a zeroth item zeroth item is going to be the number they give us so there is one and then we want to do plus again here and we want to wrap this in string remember and what I want to say here is the last item okay so I can just do the list and then give me the last item like that or I could be fancy and do this I put a negative one there that goes wraps around gets the last element but we'll keep it simple so I'll just say give me the second element now the only thing that we have to watch out for here is that we need to put that space that all important space so what we're going to do is just do this okay and so this goes there is space one space king okay that's how it should read and then what we'll do for the other one is pretty much the S similar thing okay else we'll have the same statement except we'll switch it with there are and then remember we need to add s and a period at the end okay okay so when we want to say King we want to say Kings period and we want to say trinkets period and here we just want to say period Okay add a little uh that guy right there okay let's give this a try and let's see what happens I could be totally off here there are are five trinkets there is one King five trinket so five trinket should give us there are five Trinkets and we get there are five Trinkets and how many one king should give us there is one king and when we call that on line 19 we do get there is one king all right guys we nailed this one this one was beautifully done we did a great job uh if you didn't get it that is totally fine just close this out come back 30 minutes later with a blank brain and try to do it on your own a lot of the times when you're doing these coding challenges from scratch they don't flow like how I'm doing this right now because right now python is more of a language to me that is like English I'm like thinking in it for you that might not be the same thing you're you're going to struggle with it that's what would happen to me and I would like literally be pulling my hair trying to solve these challenges but just not give up uh so there will you know that headache that you get from sometimes doing these problems is how how I think of it is literally your brain actually expanding so don't take it as a bad sign and if it takes you like 2 three 4 hours to actually solve this that's not a bad thing at all okay don't be like it took C you like 2 minutes it should take me the same time no of course not I've done this more times than you have unless you're a c programmer you probably smoke me too but hopefully this made sense hopefully you were able to solve this you learned something new put your Solutions in the comments below I would love to check them out and so would the other people in at clever programmer anyways guys thank you so much for watching I love you guys so much and as always I'll see you in the next video\n"