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