Return to Video

Measure Udacity Solution - Intro to Computer Science

  • 0:00 - 0:03
    [Evans] Let's define our procedure in the Python interpreter.
  • 0:03 - 0:05
    We'll give it the name measure_udacity,
  • 0:05 - 0:08
    and we'll give the parameter the name p.
  • 0:08 - 0:14
    Actually, we should probably give the parameter the name U, and we'll use an upper case U.
  • 0:14 - 0:18
    Not the most standard name for a parameter but appropriate in this case.
  • 0:18 - 0:20
    We'll introduce the variable count.
  • 0:20 - 0:22
    We'll set its initial value to 0.
  • 0:22 - 0:26
    We're going to use count to keep track of the number of strings that we found
  • 0:26 - 0:29
    that satisfy the udacity property.
  • 0:29 - 0:33
    And we'll use the for-loop to go through all of the elements in U.
  • 0:33 - 0:36
    Now we need to decide whether or not to count this element.
  • 0:36 - 0:38
    We can use an if statement to do this.
  • 0:38 - 0:41
    We're going to need to use a comparison to check if the first character of the string
  • 0:41 - 0:43
    is an upper case U.
  • 0:43 - 0:46
    If it is, we want to add 1 to the value of count.
  • 0:46 - 0:48
    If it's not, we do nothing.
  • 0:48 - 0:53
    So we use the equality test to check if the value at position 0 in e,
  • 0:53 - 0:56
    the current element of the list, is an upper case U.
  • 0:56 - 0:58
    If it is, we add 1 to count.
  • 0:58 - 1:02
    If not, we don't need to do anything, so we don't need an else clause.
  • 1:02 - 1:05
    And now we just need to return the value of count.
  • 1:05 - 1:07
    Let's test our procedure.
  • 1:07 - 1:12
    Our first example, the input strings were Dave, Sebastian, and Katy.
  • 1:12 - 1:15
    We see the result is 0, as expected.
  • 1:15 - 1:21
    We'll try the second example where we have 2 strings that start with the letter U
  • 1:21 - 1:25
    and see that we get the result 2 as the second value printed out.
  • 1:25 - 1:31
    We should also test a string that has a mix of Us and non-Us.
  • 1:31 - 1:33
    Now we're passing in 4 strings.
  • 1:33 - 1:36
    They all start with a U, but only 2 of them start with an upper case U.
  • 1:36 - 1:41
    So the result for the third number printed out should also be a 2.
  • 1:41 - 1:44
    And we see that we get the expected result.
Title:
Measure Udacity Solution - Intro to Computer Science
Description:

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

English subtitles

Revisions Compare revisions