They were tribal members who came up with an unbreakable code in their complex language that was adopted by American forces in the Pacific during World War II and used to confound the Japanese.
VOA: standard.2010.03.22
It's a way of breaking the code up into modules. Modules that makes sense on their own. Modules that we can reuse in multiple places. Modules that, if you like, Isolate components of the process.
能将代码分解为模块,模块能够独自运作,我们能在很多地方再利用这些模块,如果你喜欢的话,模块可以被看作是过程独立的组成部分。
And for reference, incidentally, in case you ever forget what the point of some exercise was, I'll almost always comment the code up top with a quick sentence or two that reminds you what this program does.
顺便作为参考,为了避免我们可能忘记一些代码的意思,我通常会用一两个简单的句子,在上面作出注释,那样会提醒我们这个程序是干嘛的。
What does that say? It says if in the code up here I get an exception of that sort, I'm going to go to this place to handle it.
这意味着如果在代码中,我得到一个这样的异常,我能到这里来处理它。
All right. So now, we've got it I've got the code up for you.
好,现在我们已经明白这一方法了,现在让我们来看看代码。
OK? And in fact, if you look at the code up here, and it's on your handout, the very first one, x 1, right here- if I could ask you to look at it-- is a piece of code to do it. And I'm less interested in the code than how we're going to analyze it, but let's look at it for a second.
实际上,如果你看看,你们课堂发的材料上面的代码,第一页上的,就是那-,大家请看看实现的这一部分代码,我不太关心,我们会怎么解释这个代码,首先让我们先看看。
I've certainly written code that has this problem, I've tried to use my own code that has this problem, and good to us, right, good hygiene, I'm going to use that word again here, of getting into the habit of writing defensive code up front, it's part of that collection of things that you ought to do is a great thing to do.
我举手了吧?,我当然写过犯这种错误的程序了,我也尝试过应用含有这种错误的代码,不错,好的卫生,我还是要用这个词,对我们有帮助,养成写防卫性,代码的好习惯,是你应该做的,一系列事情之一,也是我们日后要做的主要的事情。
So this line or these lines of code up here are arguably constant time steps to say if N is less than 2 in return, that it will always take maybe one step, maybe two steps, some number of fixed CPU cycles.
如果N小于2并返回,那么这些行所对应的代码,通常只需要执行一步,或者两步,具体数字与CPU周期有关。
So same code up here -- thankful is initialized to false.
所以这里的同样的代码--thankful被初始化为false值。
Up on the screen, I've got-- I commented it out, but I've got a piece of code that you've seen before, right up here. OK?
我现在在大屏幕上标出来的,是你们以前看过的一段代码,就在这儿?
I'm gonna zoom in a little more and notice it's pretty much 1, 2, 3, 4, 5, 6, 7, 8, 9 lines of code and that's kind of rounding up because of the white space.
我现在放大一点,大家会看到,2,3,4,5,6,7,8,9行代码,多亏了空格,这些代码才能看起来如此美观。
Given the code up there, if I want to move a tower of size n, what do I have to do?
鉴于这里的代码,如果我想移动N个圆盘,我该怎么做?
Having now used this pseudo code to line things up, I can start putting some additional structure on this.
用伪代码把这些事情串起来后,我可以开始进行,一些结构方面的优化了。
But the things we were trying to stress were that it's an important design decision when you are coming up with a piece of code, as to what kind of efficiency your code has.
但是我们这里要强调的是,当你写代码的时候,决定你的代码的效率的很重要的事情,就是你的设计决定。
So it's generally called distribution code and whereas for the previous problems that you pretty much started from scratch, blank files you opened up nano and there was nothing there unless you put it there.
它一般被称作分布码,然而,对于之前几乎要从头做起的问题,在空白文件中什么都没有,除非你在里面写入些东西。
OK, what I would like you to see is I've been printing out, as I went along, actually let's back up slightly and look at the code. There's merge sort.
好的,我想让你们看就是我运行过后,打印出来的结果,我们再回头看一下这段代码,这就是归并排序,输入一个列表。
Now in fairness, that particular problem kind of happened because so many companies ended up running so much code for many more years than the programmers actually thought they would be running it, but the idea is still the same.
公平起见,那个问题有时会发生,因为实际上,很多企业在这么多年里运行了,比程序员多得多的代码,虽然他们将运行它,但那个构想还是一样的。
Right now, what my code is doing is, it's printing them up for me, which is useful.
它的所有的除数,现在,我的代码要做的就是。
Delete those lines of code and move them up to the top and problem solved.
把这几行代码删除,然后把它们放到,前面去,问题就解决了。
When we start building up code, we're going to use commands. Or statements.
当我们写代码的时候,我们会用到命令或者声明。
Now, efficiency is obviously an important consideration when you're designing code, although I have to admit, at least for me, I usually want to at least start initially with code that works, and then worry about how I might go back and come up with more efficient implementation.
现在,当你设计程序的时候效率,是不得不考虑的问题,虽然我必须承认,至少对我来说,我是先担心程序能不能运行,再去把它的效率优化的,我喜欢可靠点的东西。
Let me just clean up the code a little bit.
让我停下来找找错。
We've got the code for b search up there.
代码在这儿。
Merge up here. There's a little more code there.
在这里进行合并,这里的代码有点多。
OK, how do we code that up?
我猜可能是这样的吧?
And that shows you one other nice little thing we want to do, which is if I look in this code notice I set up a variable up here called false Solution Found, initially bound to false.
这显示了我们想要做的另一件事,那就是我在这段代码中设置了一个,名为Solution,Found的变量,初始值设为。
In temp. So that means at the end of that first line of code just as before the value of 1 ends A up in a variable called temp because we said go to A and I look around, oh, there's A, what's there?
在temp中,在第一行代码的末尾,值1最终到了叫做temp的变量中,因为我们指明了定位到,并且四处查看,哦,这里是A,里面是什么?
SRC back One is called SRC for Source and one is called SRC.back which was my backup of today's source code in case I screw up.
其中一个叫SRC,用来装源代码,另一个叫,用来备份源代码,以防我搞砸了。
OK, so how can I code this up? Well, you might think, let's just jump in and write some code, I don't want to quite do that though, because I want to show you another tool that's valuable for thinking about how to structure the code, and that is a something called a flow chart. Now.
好,那么我怎么把这个以代码实现呢,你可能会想,让我们进去直接写代码把,但是我不想这么做,因为我想教给大家另外一项很有价值的,用来组织代码的工具,叫做流程图,现在,像Guttag教授,和我这么大岁数的人。
So here is the simplest program which is perhaps an ironic statement that you can implement with this language called C. So it takes a few lines of code to actually get something up and running and as you'll recall last Friday, I didn't even get this up and running because part of the process of writing a program is to one, write it.
让我们回到这个最简单的程序,你们也许觉得啼笑皆非,这么简单的例子,用C语言写出来就可以了嘛,的确只需要几行代码,就可以让这个程序跑起来,就像上周五的那样,我之所以没让它跑起来,因为编程步骤之一,就是“写“
应用推荐