00:02
So in this question we are asked to obtain a list of numbers from the user and then check if the list is already a sorted list and print a message that it is sorted and if it's not sorted then print a message that it is not sorted.
00:17
So for that we have to write a function called is sorted.
00:21
Just shown here.
00:22
So first this part of the code gets the list from a user.
00:27
So we ask the user to enter a list of numbers several by commas and we assign that input from the user to the numbs variable using input.
00:38
And the input basically takes an input from a user and assigns a variable to a string.
00:44
So it always gives the output as a string.
00:47
So we use that fact and we reassign the numbs variable to the components of that string which are split by comma.
00:56
So we use the split attribute of the string and split it with comma.
00:59
So wherever there is a comma, it will take element either side and create a different element in and make it a list of elements which are still string which are still not integers but they are at least separated with commas and then we initialize an empty list where which we will add the numbers so we then say that for n in numbers or any you can take any variable here for it basically cycles through all the elements of the string of the list of comma separated values now which are still characters or let's say strings in themselves and then we check for if the string is empty because what can happen in certain cases that somebody can give you two three four and comma when you put a comma here there is a empty character which is given here or empty string which cannot be converted to a number so we want to drop that out and there could also be another case where you could have two two commas by mistake so there'll be another empty character here which again cannot be converted to a number so we must check for these cases and if that's not the case we append the empty list with the eval output of the element of the list of numbers because the eval will convert finally the number into or the string into a number.
02:16
So this is how the list is created and entered into the program and then we come to the program itself or the function itself is sorted and that's very simple logic.
02:28
So for example if you have a list here a, b, c, d, e.
02:34
So how would you find out that the list is sorted? you will check, i mean, the logic here is that you would just want one or the first instance where you find out it's not sorted is enough to say that the list is not sorted.
02:48
So anywhere you find out that the order of increment of index is not following the increment of the value themselves, you can say the list is not sorted.
02:57
So how you go about is you will check a and b.
02:59
If a is less than b, it's good.
03:02
You check b and c.
03:03
If b is less than b.
03:03
If b is less then see it's good like that keep checking anywhere you find that this condition is violated you know that the list is not sorted so that's exactly what we do here we go we take an index which ranges from one to the length of list because what we are doing is that we are trying to say that the second value is being confirmed compared to the first value so we need the index to start from one and like this you go all the way to the last value being compared to the one before that.
03:36
You could do it the other way as well.
03:37
You could take this value and compare it to second value.
03:39
In that case, you'll have to go only to the second last value...