OK. Today, for the rest of the lecture, we're going to take a break from the topic of algorithms, and computation, and things of the sort. And do something pretty pragmatic.
好,今天剩下的时间里,我们不再讲算法,计算和排序这些话题了,我们会做一些非常实用的事,我们主要的讲讲测试。
Literally, return the control from this function, and take the value of the next expression, and return that as the value of the whole computation.
正如字面意义上说的,从这个函数返回,然后取得下一个表达式的值,并把这个值作为整个计算的结果返回。
For all students, what we want to do is we want to give you an understanding of the role computation can and cannot play in tackling technical problems.
对所有学生来讲,我们想做的就是,给你们关于计算在能否解决技术问题中,所扮演角色的,认识。
It's going to let us bury away the specifics of something, and treat that computation like a black box. And by black box, I mean, literally behaves like a mysterious little black box.
通过抽象我们可以将一些细节,掩埋起来并将这一段计算当成黑盒,说到黑盒我的意思就是,字面上的像一个神秘的小黑盒。
One of the things we want to help you do is identify the different classes of algorithms, what their costs are, and how you can map problems into the most efficient class to do the computation.
我们想要教会你们的事情之一就是,学会辨别不同种类的算法,它们的代价是怎么样的,以及怎么样把问题映射到,最有效的算法种类中去做计算。
But we think it's really important for the department to help everybody learn about computation, and that's what this course is about.
但是我们觉得让系里来帮助学生们,学习有关计算的知识是很重要的,这也是这门课所要传授给你们的内容。
And so what I see is that as I run this, I'm doing a lot of redundant computation.
因此我在这里,看到的是随着我的运行,我将会进行很多冗余的计算。
And as long as that smaller computation reduces to another smaller computation, eventually I ought to get to the place where I'm down in that base case.
如果我不属于基础事件,那么我需要把它简化为更简单的计算,随着计算的不断简化,最终我能分解成基础事件。
I can use that to return values, which I can then use elsewhere, which I did-- and if I just come back and highlight this-- inside of that computation.
我可以用它来返回值,然后我可以在别的地方使用这些返回值,正是我做的--如果我回过头来,在高亮一下这个--在这部分计算内部。
At that point we're in the base case and we can unwrap this computation.
在这一步我们就是属于基础事件了,也就是说我们可以打开这个计算了。
And that leads to a lot of redundant computation.
这就导致了累赘计算。
Let me write a function, in fact the literal thing I would do is to say, identify the things that change, give each of them a variable name because I want to refer to them, and then write a function that captures the rest of that computation just with those variable names inside.
让我来写个方法,实际上我的意思是,辨别出变化的东西,然后给它们逐个命名以区分,然后写一个用来计算,这些变量的,包括计算过程的方法。
And that's what they would do: they would do that specific computation.
那就是它们能做的事儿:,它们能做那个特定的运算。
And I unwrap the computation to get back.
最后我便可以打开计算返回最初嵌套了。
And here's the basis of the computation.
这就是计算的基础。
Computing square root. I'm going to capture it in a piece of code, I'm going to be able to refer to it by a name, and I'm going to suppress the details, meaning inside of that computation, you don't need to know what it does. You just need to know, if I give it the right kind of input, it'll give me back an input that satisfies the contract that I set up.
比如计算平方根,我会用一段代码来说明,我将给它命名,并且忽略细节,意思就是在这一段计算过程中,你不用知道它做了什么,你只需知道,如果我进行了正确的输入,它将会给我符合条件的输出。
Because in doing that, Python would then have a value that it could pass on into some other part of a computation, and if it wasn't what I wanted, I might be a long ways downstream in the computation before I actually hit some result that makes no sense.
因为如果这样做的话,Python会将,输入的值传递到下面的,一些运算中去,如果这个值的类型不是我想要的,我可能会在得到,一个毫无意义的结果之前,经历一个很长时间的,计算过程。
应用推荐