English subtitles

← General Concepts Solution - Programming Languages

Get Embed Code
3 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Hello, I am Peter Chapman, your assistant instructor for
  2. this course on programming languages, "Building a Web Browser."
  3. You may have seen me on the forums and kind of managing
  4. the course in general, but one of the major things I do,
  5. helping you learn this material, is going over the homework answers.
  6. With that said, let's get started with the first problem on the first homework assignment,
  7. and that is a multiple choice question on general concepts covered in the first lecture.
  8. Statement 1 says, "Web pages can control their behavior and appearance
  9. through embedded JavaScript."
  10. This is definitely true.
  11. Almost any web page that you use on a daily basis
  12. has tons of JavaScript that make things nice and interactive and speedy.
  13. If you were to visit, say, our web site without JavaScript enabled,
  14. there would be almost nothing you can do.
  15. Nothing would load. Nothing would be interactive.
  16. And it's really not an overstatement that JavaScript is the basis
  17. for modern web applications.
  18. Statement 2, "While English sentences can be broken up into words,
  19. HTML and JavaScript cannot."
  20. This statement is false.
  21. Let's look at a piece of HTML to show you why.
  22. Here we can break up this sample bit of HTML into smaller pieces,
  23. namely the beginning of the bold tag,
  24. the actual text that says "Irvin," and the ending of the bold tag.
  25. Individually, each of these things has meaning
  26. in the same way that the sentence above, the word and,
  27. allows HTML and JavaScript to together be applied to the cannot.
  28. Some of these words have semantic meaning.
  29. Others are more syntactic structure.
  30. But nonetheless, they're words, just in a different language.
  31. Statement 3, "For every Python program that calls re.findall(), there is another
  32. Python program that does not call re.findall() but still obtains the same results."
  33. As it turns out, this statement is true.
  34. Before I deal with re.findall(), let's think of an alternative example and build up to that.
  35. Let's say instead of wanting to do find all, we just want to find one.
  36. That is, given a regular expression and an input text,
  37. we want to determine whether or not the text matches
  38. exactly the regular expression, and we can do this
  39. using the tools that we developed in lecture, particularly
  40. the procedure fsmsim.
  41. What we have to do is create a finite state machine representation
  42. for this regular expression, and that's pretty straightforward.
  43. We process the input text on this state machine using fsmsim,
  44. so you'd have to represent the state machine using the map
  45. that we described in lecture, and if the input text ends on an accepting state,
  46. then we can say we've found one match,
  47. so that's how we use fsmsim to find matches for the regular expression.
  48. But find all does something a little different.
  49. Let's say you had this code.
  50. This statement takes in a regular expression, the map numbers,
  51. and an input string that contains 2 numbers, 12 and 34.
  52. What find all is going to return looks like this.
  53. It's going to return 2 strings, 12 and 34,
  54. so we can use fsmsim to do kind of one matching.
  55. If we were just given the string 12, then we could do that,
  56. but we want to do a little different style of interpretation with find all,
  57. and we can still use fsmsim just with a little more effort.
  58. So let's say we have our black box fsmsim.
  59. And it has a representation for that regular expression.
  60. We also have our string that consists of 5 characters.
  61. What we're going to do is we're going to feed in the first character
  62. into our regular expression simulator.
  63. One matches the regular expression,
  64. so so far we have a match for 1.
  65. In order to match larger strings, we're going to try adding one more character,
  66. and we're going to feed in 12 to our regular expression simulator.
  67. 12 matches, so far, so good.
  68. Now we're going to try 12+.
  69. 12+ doesn't match, so we've realized that 12 is the largest possible
  70. prefix that will match this regular expression, and it's going to be one of the results.
  71. We're going to go back, and we're just going to try +.
  72. + doesn't work.
  73. We're going to try +3. That doesn't work either.
  74. That's not a number, and +34, which also doesn't work.
  75. We're going to give up on the +, and we're going to do the 3.
  76. 3 matches. Now we can try the 4.
  77. 34 matches, and since that's all the characters left in this string, we're done.
  78. And that's kind of how we can use fsmsim to simulate
  79. the same exact functionality of find all.
  80. Statement 4, "Every regular expression that involves neither
  81. + nor * matches a finite set of strings."
  82. This is true.
  83. In the regular expressions that we define in class,
  84. the only repetition is through + and *.
  85. We can do some fancy things still such as or
  86. or optional characters, but that doesn't even get us near
  87. the possibilities that these repeating operators can give us.
  88. I can enumerate a lot of different types in my regular expression,
  89. a lot of different strings, but it's never going to be infinite.
  90. And so if you don't have the + or the *,
  91. you're stuck with a finite set of strings.
  92. It may be a very large finite set, but it's still finite.