text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
On this line here, if m is less than or equal to 0,
4,798.88
4.06
it's really just a sanity check.
4,802.94
1.55
If the human was messing with me and somehow
4,804.49
3.15
was able to inject a negative value or a 0 value into this function,
4,807.64
4.609
I don't want to get stuck in some random, like infinite loop.
4,812.249
2.541
I want to catch that.
4,814.79
1.02
So I'm just going to say less than or equal to 0 just to be super-defensive
4,815.81
3.124
when programming return 0.
4,818.934
1.926
It's not correct to say that, but I just want--
4,820.86
2.31
and I'll document this in my comments, for instance.
4,823.17
2.65
Return 0 if the user does not cooperate.
4,825.82
1.96
I need that base case so that stuff stops eventually.
4,827.78
3.94
And then, here's kind of the amazing thing.
4,831.72
2.12
In my "else" condition, is one line of code
4,833.84
2.84
that implements the entirety of this function.
4,836.68
3.7
The result of asking me the question, what is sigma of m,
4,840.38
3.39
is really the same thing as saying m plus sigma of m minus 1.
4,843.77
8.07
In other words, if the goal is to count up all of the numbers between 1 and m,
4,851.84
4.28
logically that's the same as counting up all the numbers from m to 1.
4,856.12
4.12
And so if I view it that way, if you give me m,
4,860.24
4.48
I am going to sort of on my cheat sheet write down the number m,
4,864.72
3.35
and then say plus whatever the summation is from m minus 1
4,868.07
4.05
all the way down to 1.
4,872.12
1.11
And you know what?
4,873.23
0.75
I have a function, a piece of code, that gives me exactly that answer if I just
4,873.98
3.88
pass-in that slightly smaller value.
4,877.86
2.7
So on first glance, it might look like sigma calls sigma.
4,880.56
3.81
That can't possibly be a good thing.
4,884.37
2.19
But it doesn't do it forever.
4,886.56
1.48
Sigma might call sigma might call sigma might call sigma.
4,888.04
2.54
But at what point in the story does sigma stop calling sigma?
4,890.58
4.67
AUDIENCE: [INAUDIBLE].
4,895.25
1.5
SPEAKER 1: When it's counted.
4,896.75
1.32
When m, the input, is what?
4,898.07
3.01
Negative.
4,901.08
0.53
Or we shouldn't hit negative ever.
4,901.61
1.46
We should first hit?
4,903.07
1.231
AUDIENCE: 0.
4,904.301
0.499
SPEAKER 1: 0.
4,904.8
1.01
So once m minus 1 gets so small, 1 minus 1, that it equals 0, this line of code
4,905.81
6.507
is going to kick in.
4,912.317
0.833
And it's going to say return 0.
4,913.15
1.92
Thereby, short circuiting this seemingly infinite loop,
4,915.07
3.82
which does, in fact, terminate eventually once the input
4,918.89
3.74
gets small enough.
4,922.63
1.41
Now, probably you shouldn't bother implementing code like this.
4,924.04
3.195
If anything, we'll talk about this in future weeks.
4,927.235
2.125
Calling function after function after function after function after actually
4,929.36
3.66
can take up a lot of memory.
4,933.02
1.352
It can actually cause your computer to crash in some cases.
4,934.372
2.458
And we'll talk about when and why.
4,936.83
1.82
So this isn't necessarily the best technical decision,
4,938.65
2.95
but it's illustrative of the capability we have in programming to map intuition
4,941.6
4.48
from our binary search or our merge sort into actual code.
4,946.08
5.301
And indeed, there's going to be data structures that we soon
4,951.381
2.499
see far fancier than arrays.
4,953.88
2.11
We're going to see things like link lists, and trees,
4,955.99
2.39
and tries, and hash tables.
4,958.38
1.46
And other data structures still that are going
4,959.84
2.71
to allow us opportunities to implement algorithms on top of those data
4,962.55
5.22
structures so to speak, to answer questions
4,967.77
2.45
using an ingredient like this one here.
4,970.22
2.96
But of course, we're left then fortunately with a better algorithm
4,973.18
3.51
than something like bubble sort.
4,976.69
1.5
And indeed, this is fairly well-known now it would seem.
4,978.19
2.75
And I thought we'd conclude with a look at this interview that
4,980.94
3.13
was conducted with a certain senator some years ago who was running
4,984.07
3.71
for the presidency and was asked by Google's own Eric Schmidt,
4,987.78
4.03
the CEO at the time, a set of interview questions.
4,991.81
3.06
One of which we thought was appropriate note to end on here.
4,994.87
4.45
[AUDIO PLAYBACK]
4,999.32
3.666
[APPLAUSE]
5,002.986
2.334
-Now, Senator.
5,005.32
1.86
You're here at Google.
5,007.18
1.01
And I like to think of the presidency as a job interview.
5,008.19
5.71
Now, it's hard to get a job as president.
5,013.9
3.142
You're going [INAUDIBLE] now.
5,017.042
1.208
It's also hard to get a job at Google.
5,018.25
2.17
We have questions.
5,020.42
2.67
And we ask our candidates questions.
5,023.09
2.58
And this one is from Larry Schwimmer.
5,025.67
3.676
[LAUGHTER]
5,029.346
1.969
You guys think I'm kidding?
5,031.315
1.125
It's right here.
5,032.44
2.46
What is the most efficient way to sort a million 32-bit integers?
5,034.9
4.148
[LAUGHTER]
5,039.048
2.922
-Well--
5,041.97
2.4
-I'm sorry.
5,044.37
0.591
Maybe--
5,044.961
0.499
-No, no, no, no.
5,045.46
0.75
I think the bubble sort would be the wrong way to go.
5,046.21
4.98
-Come on.
5,054.19
0.745
Who told him this?
5,054.935
1.565
OK.
5,056.5
1.84
I didn't see computer science in your background.
5,058.34
2.352
-We've got our spies in there.
5,060.692
2.878
[END PLAYBACK]
5,063.57
1.32
SPEAKER 1: All right, so that's it for week 3.
5,064.89
2.23
We will see you next time.
5,067.12
1.6
SPEAKER 2: So you want to know the full story?
5,076.94
2.352