Problem number 10, this is a stretch problem, and it is a difficult one.
It will need you to learn about several new things which we haven't learned yet.
So one of the things is for looks.
And the other thing is slices.
Both of these things are extra.
And if you're able to complete this problem, then I would be impressed.
It is a great feat to do it if you haven't learned them yet.
OK, now I will go ahead and show you how to solve this exercise.
So we have to calculate the median of numbers.
You were given one number first, which is the count of numbers we're going to read from the terminal
and then we have to calculate the median, which is calculated by taking the middle element among the
ones which are given.
So let's go ahead and solve this.
First, let's start with the simple stuff how to read the simple number.
Well, the reader first.
So the reader like so.
And then we read our line.
Down there up on this river.
OK.
That's our line.
Now we converted to an integer, so we have our number.
Or let's just call it count and error that is equal to a selection we don't take like we did in this
space in this line.
And we are error with them.
OK.
And now this counter, we use it to get the next and number.
So how do we do that?
Well, in order to do that, we need to use what's called a full.
We've already used it when several occasions in previous exercises.
In case you were watching this without watching the other ones, I'll quickly explain how we're going
to use it.
So what a for a loop does is it allows you to execute a piece of code repetitively while a condition
is met.
So the way you write this way, we write for air condition and then we execute this block of coal.
To hear the condition in this case is we want to, well, execute.
We want to read numbers from the terminal.
Well, we will load with five times in order to do five times.
What we have to do is we have to have some kind of a number which counts how many times we've read from
the terminal already.
And while this number is less than the target number we will read, we'll continue doing that.
Let me show you what I mean when you first great integer and that's cool with PI and it will start from
zero until I is less than the counter.
We'll be reading stuff.
Brilliant stuff.
And when we read something, we increment I by one.
OK.
So let me show you how this works.
Oh, Brent, step here.
Every time we execute this piece of code now, I run the program and I'll write five.
So five will be this counter we use to read several times from the terminal.
And if I write the size, this piece of code will be executive five times y five times because I started
zero in which is zero less than five.
Yes, it is.
We execute this and increment i by one next step, which is one less than five.
Yes.
This way, does it give this an increment?
I, by one, is still less than count.
Yep.
It is true.
Doesn't count.
Yes, or it is.
And finally, when we check is five less than five, we get false as a result, meaning that we exit
from this for a loop and we continue with the rest of the program in this case and just finish this,
OK?
And that's how it works, in a nutshell.
We explain it way more thoroughly in a separate lecture.
So for now, I just wanted to know enough and get an intuition about it so that you can solve the problem
at hand.
OK, now that we know how to read five times from the terminal, let's go ahead and do that.
So we'll read, well, several numbers and we'll do it by well using a line and an arrow to read our
terminal, as always.
Again, we check if anything goes wrong.
And then this line will be converted to a number.
So let's go ahead and do that.
In this case, we have phone numbers only, so we'll use the page to function well in this case.
And we verified that no error occurred.
OK.
And for now, let's been this number.
All right, given that we know for the for the West, nothing new should be present so far.
So we're writing five to execute this piece of code five times and then we're reading the next line.
So for the next line right then and when I write, then we read it.
Then we convert it to an integer imprinted in the end.
Then I'll write 12.
We get it from 13, 14, 15.
And the program ends because that's when we execute this for Luke five.
That's just to give you an example with something else.
If the initial number, I specify is still that, it will only ask me to input two numbers.
OK, now the question is how do you calculate the median of these numbers?
Well, in order to do that, one thing which is good is that these numbers are given in sort of order.
So that's really nice.
But the problem is that we have to store them somewhere.
And when we store them, we have to then get the middle element and that would be the medium.
So one way to achieve this is to somehow detect while the for loop is executing, whether you're in
the middle.
This is one way to do it.
Another way is to do something which is even a higher stretch than learning about four loops.
We'll have to learn about what's called a slice.
So if you're interested and you want to solve this problem on your own, try learning about slices at
a very basic level and use it to solve this problem.
OK?
In case you're tempted it, or in case you just want to watch me solve it?
Let me show you how to do it.
So what a slice is.
It's a way to specify a list of numbers.
And let me do this.
So if I run this now, notice that I get a list of numbers printed like this.
And let me zoom in a bit so that it's visible a bit better.
So what they can do with this list is I can create one.
Which is empty and when they print it, I get this.
Well, these brackets signify that I'm using a space and it's empty.
And what this data structure allows me to do is it allows me to add numbers to it.
The way you up numbers to it is by writing the name of it is important upwards.
And then we specify the name of the class again, and we are the number in this case at one.
So if I run this program now, this time I will get a slice which contains one number and that is one
if I execute this again.
And this time I add the number to.
The first time I get a slice of numbers, which has one element.
The second time I get two on it, so you can think of this as some kind of a bucket, which you fill
with data with numbers instead of having, let's say, five variables for five different numbers.
You can have a slice, which is five numbers long, and you can have all your five numbers in it and
then you can later fetch those numbers and get them as you need them.
But we'll get to that later first.
Let me show you how we can use this new technique to solve the problem.
Actually, first to collect the numbers.
OK, so what we will do is we have to initialize this slice.
The way we initialize it is by giving it the name for its algorithms, and it will be equal to square
brackets, both type integer.
And then we have these curly brackets.
The square brackets means that I'm creating a slice.
The integer means that the data type I'm going to store inside this slice is going to be of type and
it can be anything, can be strings and can be characters.
It can be whatever.
And these curly brackets just say that I'm initializing the slides and it has zero elements.
In the beginning.
I can initialize it with some elements, such as one, two three.
If I do this, I'll have a slice, which has three elements initially, but I don't want to have any
elements in it, so I'll just create an empty slice now.
And now having this slice, well, they can do it.
Every time I read this number, I can fill it by using that same text I showed you.
I can appends to that slice the current number.
I have just threats.
And finally, after I'm done with this for loop at the end of this program, our best friend, the numbers
I've collected so far.
And if I execute my program again and I want to, what story I have type numbers and the numbers will
be one two three four.
But at the end, I'll have a slice which have five numbers.
So this way, with the single variable, I'm able to store several numbers or several pieces of data
which are of type integer meaning that they're numbers.
So this allows me to collect all the numbers I'm reading, and then later on, I can use them for whatever
purpose I need.
I will show you how to use them in a second.
OK, so that's what the slices again allows you to collect numbers into some kind of a variable which
doesn't hold a single piece of data, but hold several pieces of data and the amount of data it holds.
The amount of elements in it varies.
And that's exactly what we need.
We want the number of elements in this list to vary if we input the number three after the program input
three numbers afterwards.
Note is that this variable now has three elements in them, but if instead I have five elements like
in this case, we have five elements.
So that is very useful data structure, which we will be using a lot throughout this course and later
on when we start building our games to, for example, hold the same screen or something like that.
OK.
So that's a slice in a nutshell.
And just a disclaimer, if you don't fully understand how slices works, that is actually totally expected.
Remember that this is a stretch problem.
So it's required that you study a bit more than what you already know and it will be challenging.
So there you go.
That's your challenge, essentially.
But even if you don't understand it fully, don't worry, we have a separate lecture which explains
what slices are in a little more detail.
OK, cool.
So that's how we collect the data numbers.
And now what we have to do is we have to take our numbers and get them middle one.
How do we get a number from a slice?
Well, we can get the number from a slice by using the variable name in this case numbers and then using
square brackets after we specify square brackets.
If we tell with which the number at which position do we want to fetch?
OK.
So if I hear say I want to fetch the number I positions, you know?
CARES what will happen.
I specified three numbers, one to three.
And when I print those lies and it's entirely, I will get three numbers, but if I don't need the other
three numbers, I just need one of them.
I can use this syntax to say I want the number at position zero.
So that's one caveat about twice in goal and in other languages.
The way you specified the position of the number begins from zero, so you don't start counting the
numbers from one owner to start counting them from zero.
So zero stands for the first number, one stands for the second number, etc., etc. So in this case,
if you want the surge, the first number, we use zero.
And if we want if it shifts in a second number, just to give you another example, we use one.
As you can see, when we have three numbers and we choose the number position one we get.
Number two as a result.
OK, cool.
Now knowing this, what we need to do is we have to actually take the middle number.
OK.
So the middle number is, well, this one, and the way we can get it is by take the count of all numbers
we have and dividing it by two.
Why?
Well, because if we have five elements and we divide five by two with hole number division, we'll
get two as a result.
And if we take the number at the position to, well, let's see which one that is.
This is the number two position zero.
This is the number position one and this is the second one.
So as you can see, the number which happens to be number at position two, is also the middle number.
So that's the way you can take the middle number in this sequence of numbers.
And here's how we're going to do it instead of fetching zero or one or two or whatever.
What we'll do is we'll say I want to take the number, which is that position, whatever the count of
the initial number is divided by two.
And once I print that, check out what will happen if I write three and a half one, two and three.
We have three elements.
Three divided by two gives us the element that position one.
So it gives us a result, a form because there's this whole number division and the number at position
one is still because the first element, which starts at zero is one.
The second element is two.
So that's how we get the middle number.
Let's try with five.
Well, something went wrong there.
OK, so if I find no one from people, but you can see that I have my slice of five numbers printed
and the middle number is three because five divided by two.
Results in the number two and the element of position two is three in this case.
OK, so that's how we get the middle number.
Oh, right.
And that's how we calculate our median because the median is our middle number.
One thing, though, about millions is that if we're looking for the million in a sequence of number
of which has an odd number of elements and the median is the middle number in this case, if we have
five elements, the median of this sequence is three.
But if we had six elements instead, what we have to do is we have to take the middle two elements and
then take their average essentially.
But for this problem, we won't really be bothering with that.
I don't think that will get too overwhelming and this stretch problem will turn into a stretch stretch.
So let's not bother with also handling calculating the median of an even sequence.
OK, so that's just to make things simpler for you anyways, having that constraint and just finish
the task, we already know how to get the middle element and what we need to do is just print it out.
So let's actually initialize our medium to be equal to the middle number, which we already know how
to fetch and simply print it out.
So we'll print.
Million is equal to present the.
And on this very tough because I'm going to format this thing, and here I've asked the media, I just
calculate it's OK.
Finally, I don't need this printer anymore.
And finally, let's make a final run by the elements one two three four five.
And the million in this case is three.
If we have three elements which are five, 10 and 15, the median is done, OK?
And that's actually pretty nice.
So we already covered a lot of ground and I think we learned a lot of new things.
You probably feel overwhelmed already, which is normal.
We learned about four seconds and the ball slices and about appending two slices and taking stuff from
them.
So if you are able to figure this thing out on your own, then well done.
This is amazing and a huge feat.
So congratulations, if not, not this totally reasonable, given that you haven't learned this yet.
This is a stretch probably enough to anyways hope this was interesting.
And if you didn't understand everything, don't worry this is perfectly normal, and there is a separate
lecture which covers everything in much more detail.
Now, in terms of the problem description, I will modify it so that you don't have to deal with that
edge case, I told you about where the number of elements is not an old number.
So don't bother with other.