D o C P 学习笔记(3 - 3)Regular Expressions, other languages, interpreters - Office Hours 3

原创 2018年03月13日 22:41:31


Regular Expressions, other languages and interpreters


  • 定义正则表达式的语言;解释这个语言。
  • 定义被1个正则表达式匹配的字符串集合。
  • 其他语言。

Office Hours 3

Office Hours 3 - Udacity

Course Syllabus

Lesson 3: Regular Expressions, Other Languages and Interpreters

1. Question One

  • Design of Computer Programs - YouTube

Hi, and welcome to the third office hours. We’ve got, again, more good questions. Let’s get right at them. >>All right.

The first one comes from Voythos, and Voythos is taking CS262 as well with Wes Weimer. In that class they talked about finding state machines(状态机) as a representation of the underlying(基础的;优先的) engine for regular expressions. In our implementation, is that actually what we did or did we do something different?

That’s a great question. We didn’t explicitly(明白地,明确地) show that, but there is a one-to-one correspondence(对应;符合) between a regular expression and a finite state machine(有限状态机). And we can post some references(参考), some supplementary(增补的,追加的) material for that. But you can make up a little table of here is a regular expression–say the alt regular expression, a or b–and then here’s a finite state machine. I guess one way to implement that would be you’d have a start state, you’d have two epsilon(希腊语的第五个字母) transitions(过渡,转变) to the a or b, and then two epsilon transitions coming back. So any regular expression corresponds one-to-one to a little collection of nodes in the finite state machine. That means you can go in either direction. Does that mean that we’ve actually implemented that? Well, it kind of depends on how you look at it. Yes, you could say that we’ve implemented it, because you can show what they look like, but we aren’t really creating objects that correspond to these individual states. They’re sort of existing ephemerally(ephemeral 短暂的,瞬息的) when we execute the program rather than being defined exactly ahead of time. In that sense, no. I guess, maybe in the more important sense, the types of manipulations(操作;操纵;控制) we can do with what we implemented are slightly different than what you would do if you explicitly created these finite state machines–particularly this transition between deterministic(确定性的) and nondeterministic machines. That’s a little hard to do exactly in the representation that we had, although you could do something very much like it by starting to memoize(memorize 存储,记忆) your functions and so on. You could arrive at the same place as you would by doing that transition with machines.

Great. If you want an in depth discussion on finite state machines, you can check out the link below this video to Wes’s course.

2. Question Two

  • Design of Computer Programs - YouTube

The next question comes from Luca. Luca wants to know when work with a compiler is really, really heavy and you have a really long regular expression, is there any way of dumping out(dump out 倒出) the final set of these low level machine instructions so they can be culled(cull 挑选,剔除;) later.

Yep. Okay. That’s a great question. A couple answers to that.

One is that within the regular expression module, the re module in Python, there is a compile statement that takes a string in and returns a compiled version of that regular expression. So if you’re running your program once, you can compile that regular expression once at the very top of your program then use the compiled expression each time. Now, if you do that explicitly(明白地,明确地), then you’re all set. If you don’t, the regular expression module does most of the work for you, because what it does is it keeps a little cache–it does something like memoize and keeps the last few regular expression that’s done and says, I’ve seen this string before. I know what it compiles to. I’ll just fetch that compiled object. It does that automatically. That’s within one run of your program.

Now, another thing to think about is between runs of your program. What if you’ve compiled everything, and then you don’t want to have the startup time of compiling it over again? There’s another module called pickle. Well, what are pickles? They’re ways of storing cucumbers(腌黄瓜) for a long time so they don’t get rotten(腐烂的). That’s what the pickle module does. It takes an object that exists within the running Python interpreter and writes it out to disk in a form that can be read back in.

3. Question Three

  • Design of Computer Programs - YouTube

Next question comes from Thomas, and his question has to do with mistakes. Basically, you never seem to make any–at least in the videos. Maybe you can talk a little bit about your design process in coming up with these lectures and how mistakes figure in there.

There is always a trade off of how many mistakes we want to leave in, and believe me, I make lots of them. And I make them at various points in time. So as I’m first thinking up the questions we’re going to do, I’m coding up answers. And I make mistakes there in a couple of ways. One is, I just make errors. I write something. It computes the wrong answer. It generates an error. I swapped the order of two arguments, or I pass the wrong thing in. I’m making those mistakes all the time. Those aren’t very interesting ones, so I don’t show them. And then, when I’m recording the videos, I make mistakes there too. And, sometimes errors have crept(蹑手蹑脚地走) into the program that I didn’t notice when I was developing them. I didn’t write enough test cases, and then I have to decide what to do. And mostly, those have ended up on the cutting room floor(剪辑工作室). So, just as when you watch a movie or a TV show, you don’t see most of the outtakes(借用镜头). Sometimes after the credits, they roll a few of the outtakes So believe me the errors are there, we’re not just showing most of them.

4. Question Four

  • Design of Computer Programs - YouTube

Next question comes from Eduardo Lopez. He points out that Python is a flexible language. We can use many different programming paradigms. We can do functional, procedural, or office-oriented programming. How do you decide which of these paradigms to use when approaching a new problem?

Yeah, I guess I try to think of things as how can I get as close to the problem as possible? And so I want to program at the level of the problem. And then, incidentally(顺便;附带), I have to program with a particular language. And so I start analyzing the problem and saying, what are the pieces of this problem, what are the objects I’m going to be manipulating, what are the ways I’m going to manipulate(操作,处理;操纵) them, and try to do most of the analysis at that level. And then once that analysis is done, then I can say, well, what do I have in my programming language? And there there might be some differences between languages. So if you’re using Python, you might have more functions. If you’re are using Java, you might have more classes. But they’re still implementing the same basic set of ideas. And I like that approach because there is a more direct connection between the problem and the solution, rather than a multistep of going from the problem to the language implementation and then back to the solution.

All right. Thank you. That’s all we have for this week. See you next week. See you next week.


  1. Design of Computer Programs - 英文介绍 - Udacity
  2. Design of Computer Programs - 中文介绍 - Udacity
  3. Design of Computer Programs - 中文介绍 - 果壳
  4. Design of Computer Programs - 视频列表 - Udacity
  5. Design of Computer Programs - 视频列表 - YouTube
  6. Design of Computer Programs - class wiki - Udacity
版权声明:本文为博主原创文章,可以转载,但转载前请联系博主。 http://blog.csdn.net/qq_33528613/article/details/79548039

《D o C P》学习笔记(3 - 0)Regular Expressions, other languages and interpreters - 简介

Regular Expressions, other languages and interpreters 你可以学到什么: 定义正则表达式的语言;解释这个语言。 定义被1个正则表达式匹配的字符...
  • qq_33528613
  • qq_33528613
  • 2018年03月04日 17:33
  • 71

N o v e m b e r 1 9 t h M o n d a y

T h e   k a n g   p r o j e c t   I   h a v e   t o   p u t   a s i d e   d u e   t o   w o r k   a ...
  • Lu_ming
  • Lu_ming
  • 2007年11月30日 21:34
  • 2826

N o v e m b e r 1 3 t h T u e s d a y

T o d a y   I   f i n i s h e d   c o d i n g   a n d   w r i t t i n g   t e s t i n g   r e p o r ...
  • Lu_ming
  • Lu_ming
  • 2007年11月30日 21:30
  • 2761

如果令 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 分别等于

如果令 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 分别等于 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15...
  • java_firefly
  • java_firefly
  • 2012年09月06日 19:33
  • 13293

int *p[3] 和int (*p)[3] 的区别

int *p[3] p是一个数组,此数组有3个元素,每个元素都是int*类型,也就是指向整型数据的指针类型。 int a=10,b=20,c=30; int*p[3]={&a,&b,&c}; 而i...
  • Sayesan
  • Sayesan
  • 2014年09月02日 09:33
  • 2735

N o v e m b e r 6 t h T u e s d a y

T h e r e   i s   a   j a v a   s o u r c e   a b o u t   e n c o d i n g   c h a r a c t e r s   s ...
  • Lu_ming
  • Lu_ming
  • 2007年11月30日 21:19
  • 3751

N o v e m b e r 2 6 t h M o n d a y

T h e   I P   a d d r e s s e s   f i l t e r   f u n c t i o n   m u s t   b e   c h a n g e d   a ...
  • Lu_ming
  • Lu_ming
  • 2007年11月30日 21:40
  • 2028


(*p)[3]与*p[3]的区别int p[3] p是一个数组,此数组有3个元素,每个元素都是int类型,也就是指向整型数据的指针类型。int a=10,b=20,c=30; int*p[3]={&...
  • zhang0558
  • zhang0558
  • 2016年09月27日 19:45
  • 313

N o v e m b e r 5 t h M o n d a y

 T h e   s e r v e r _ r e c   d e f i n e s   a   l o g i c a l   w e b s e r v e r .     I f   v i...
  • Lu_ming
  • Lu_ming
  • 2007年11月30日 21:17
  • 2508

main() { int a[3][3],*p,i; p=&a[1][1]; for(i=0;i<4;i++) p[i]=i+1; printf("%d\n",a[1][2]); } 求输出

#include void main() { int a[3][3],*p,i; int j,k; p=&a[1][1]; for(i=0;i   printf("%d\n",a[1][...
  • kendyhj9999
  • kendyhj9999
  • 2012年03月22日 19:46
  • 1936
您举报文章:D o C P 学习笔记(3 - 3)Regular Expressions, other languages, interpreters - Office Hours 3