Python for Absolute Beginners Transcripts
Chapter: The big ideas of software development
Lecture: Big idea: Designing an algorithm

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So, you've got this jar of M&M's and you come into a store and it's sitting on the counter there. And they say, hey, if you can guess how many
0:08 M&M's exactly are in this jar you're going to win a discount. Or you'll have lunch for free, or something like that.
0:15 And, you're standing there, thinking well, I need a way to guess. Maybe they're even willing to give you five guesses.
0:22 They'll tell you if it's too low or too high or something like that. Give you a little bit of guidance 'cause
0:27 you know, pure guess, that's a little bit hard. Maybe they've been having the pure guess for two months and nobody's won.
0:32 They're like, fine let's try to give 'em some help. So, you've got to figure out a way to solve that problem and that would be developing
0:38 and algorithm that will address this situation. So, you're the woman here on the right come in to the store.
0:45 There's the guy behind the counter and he says guess how many M&M's, I'll give you five tries. If you get it right within five guesses
0:52 then lunch is free, on the house. She says, ah, okay, could you give me just a little hint, how many, max...like what
1:01 is their upper bound, what is the maximum number of M&M's there could be? He says sure, fair, a hundred. Now, what does she do with that information?
1:09 The least zero, I can see there's more than zero. Let's say there's zero to 100. You could have a very naive way kind of like a child would.
1:18 Is there one? No. Is it two? No. Is it three? No. If you have this approach on average if the M&M's were evenly distributed through zero to a hundred
1:29 you would guess, you would have to go through 50 attempts on average to get there. Sometimes you'd get lucky and it'd only
1:35 be 10 guesses other times it'd be 90. It would be horrible, right? People in line behind you'd be like hey, stop that number game. Get out of here.
1:42 I want to eat my lunch. Right and for you, of course you'd be very unlikely to get it within the five tries. So there's other ways we could do it.
1:49 We could say, well, let's try to look at it half. Like, right in the middle they're going to tell me if it's too high or too low.
1:54 Let me guess in the middle and that way the very first guess, I'll be able to exclude 50. Right? If I guess 50 and they say it's too high
2:01 I know it's zero to 50 now. If I guess then 25 out of that smaller one then it's going to be, when they say it's too low
2:08 well then I know I've excluded the lower half. And that's pretty much the most efficient way you can do it with no information.
2:14 So, she says is there 50? He says, nah-nah, that's too low. All right, half between 50 and a hundred, 75. No no, that's too high.
2:22 So she says okay, well let me try to go to the half between those two numbers which would be 62 or 63, minus any broken ones
2:29 which let's say there's not. So it's a whole number, so 62. Boom. It was 62. Lunch is on the house. Now it might seem unreasonable that you went
2:38 from 75 to 62 and got it but if you keep following this path eventually you're going to narrow in on exactly the answer every single time.
2:46 This is much more efficient than just going and saying is there one, is there two, is there three.
2:52 So this is a typical way to develop an algorithm, right? Think about, well, what ways could I most efficiently solve this problem?
3:00 Because often in programming there's nearly an unbounded number of ways in which you could go about accomplishing something but
3:07 there's usually a few better and efficient ways. So cool, lunch is on the house.


Talk Python's Mastodon Michael Kennedy's Mastodon