-: Let's actually recall from a previous lesson
this LeetCode problem, right?
So what if we tinker with some of these things here, right?
And just to prove the point, let's actually,
this is a different chat window
from the other chat that we had,
where we gave them the LeetCode link.
So let's say I have the following sequence
or the following problem statement and paste that in.
My solution is not working. Why?
And then let's go ahead and paste in a modified version
where we kind of screw up the binary search,
add a one here, let's put a two here,
and let's maybe even misspelled right here
to become light, right?
And let's see if it's able to pick out some of these issues.
So clearly, first of all, there's a big issue in the logic,
it shouldn't even be correct,
and secondly, there's a syntax error,
which doesn't even allow it to compile correctly,
because light has never been declared as a variable
in this function.
So the issue in your solution
is with the calculation of the middle index mid,
so that's perfectly fine, so let's see here.
The things we ended up changing is this mod function here,
and let's see what they give us.
Yep, they've changed our mod right back to two.
You should be dividing by two to get to the middle index.
Change the calculation of mid
and allow your solution to work instead.
Yeah, so ChatGPT has made a few other modifications
to the code, including the check for mid mod three
equals zero to mid mod two equals zero as well as a typo.
So it's talking about literally everything
that it's fixed for us,
and if we go ahead and paste that in,
I don't doubt that this is pretty much
what we had had before, but yeah.
Nonetheless, it's a correct solution now.
and yeah, that works perfect.
It knew exactly what we had fixed,
even though this is a different context
from the conversation that we had in the previous lesson.
So it actually remembers those things,
which is really interesting.
Maybe we could even go back even further
to a previous time where we talked about primes, right?
So what if we say, so remember, the sieve of Eratosthenes
needs to have a certain rage of numbers
that it goes through, right?
So let's start it at five
and then let's have it go up until,
not the square root of X, but two,
like the square of X, right?
So let's see.
Why is my algorithm to generate primes slow and incorrect?
Because now not only are we not going up
into a square root factor of our input number, X,
but now we're going to a power of it,
and then we're also messing with the starting number,
so this is literally not even gonna find the factors
two, three, four for numbers,
which is gonna be extremely problematic, right?
So let's see if it's able to pick out what's wrong with that
after I change it.
It's incorrect because the range in the outer loop
should be two to the square root of X,
not five is a square of X.
Dang. This is really good.
This ChatGPT thing is really something else.
So I mean, if you're implementing some well-known algorithm
and you just don't know what's wrong
and you're getting something like incorrect input,
this thing is insane.
It can really just pick out the flaws with your algorithm
and give you the corrections
that you need to make, quite simply.
So yeah, and it even explains why, right?
It says the reason why this algorithm is efficient
is because you only need to check up
to the square root of X, which this is a very big component
of the really fast runtime that this algorithm has.
So if we iterate more up until the square of X,
that's just wasting a lot of compute
and giving us an incorrect answer.
So yeah, it's telling us exactly what's wrong,
so this is really powerful,
and if you know what you're doing,
once you've gone to this point as a coder
where you're able to write your own code,
ChatGPT can be a really useful tool
for kind of understanding what you may be doing wrong
as long as you're able to provide
the problem statement, right?
So the first and foremost important thing
is to tell them what your goal is, right?
So in the same way that, for code generation,
you have an idea of what kind of code
you want generated in the first place,
here with debugging, you need to tell ChatGPT what it is
that you want your program to accomplish in the first place
so that it can help you generate
the correct behavior, right?
So here, I told it I was trying to generate primes,
so that's how it knew what it needed to fix.
Hopefully, this is helpful for you
to understand how ChatGPT can help you debug,
and I'll see you in the next video,
talking about documentation.