Index Solution - Intro to Computer Science

• 0:00 - 0:04
[Evans] Here's 1 way to define find_element using index.
• 0:04 - 0:07
In the case where the element does exist in p,
• 0:07 - 0:09
index gives us exactly the result we want.
• 0:09 - 0:14
So we can use an if statement, check that t is in p using t in p,
• 0:14 - 0:18
and then if it is, the block for the if returns that value.
• 0:18 - 0:21
It returns p.index(t).
• 0:21 - 0:24
For the else clause the element did not exist in p,
• 0:24 - 0:26
so we return -1.
• 0:26 - 0:30
Here's another way we might define find_element using index.
• 0:30 - 0:34
Instead of checking if t is in p this time, we'll check if t is not in p.
• 0:34 - 0:38
If t is not in p, then we know the result is -1.
• 0:38 - 0:40
We can return -1 right away.
• 0:40 - 0:43
If we reach the next statement, then we know that t is in p
• 0:43 - 0:47
and we can return the result of p.index, passing in t.
• 0:47 - 0:50
So we've seen 4 different ways to define the find_element method.
• 0:50 - 0:54
They all have the same behavior, but we've defined it once with a while loop,
• 0:54 - 0:58
once with a for-loop, and 2 different ways using the index method.
• 0:58 - 1:01
When we use index, we need to use an if statement as well
• 1:01 - 1:04
to make sure that we produce the right result for the case where the element is not in p.
Title:
Index Solution - Intro to Computer Science
Description:

more » « less
Video Language:
English
Team:
Udacity
Project:
CS101 - Intro to Computer Science
Duration:
01:07
 Udacity Robot edited English subtitles for 11-43 Index Solution Udacity Robot edited English subtitles for 11-43 Index Solution Fran Ontanaya edited English subtitles for 11-43 Index Solution Udacity Robot edited English subtitles for 11-43 Index Solution Udacity Robot edited English subtitles for 11-43 Index Solution Amara Bot added a translation

English subtitles

Revisions Compare revisions

• API
Udacity Robot
• API
Udacity Robot
• API
Fran Ontanaya
• API
Udacity Robot
• API
Udacity Robot
• Amara Bot