I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
The key thing to notice is, I now have the ability to create iterations, which extends well I can do.
要注意的关键点是,我们现在已经学会如何去创建迭代了,这大大扩展了我们能做的事情的范围。
Condition," this thing in the middle between the semicolons, is going to be checked every iteration of the loop.
条件,“这个在两个分号的中间,它将迭代地检查这个循环的条件。
We used iterative deletion in a relatively abstract setting, or not abstract, rather a play setting last time in which were choosing numbers.
我们在相对抽象的背景下探讨迭代剔除,其实也没那么抽象,比如上一讲,我们做的选数字的游戏
Successive approximation, Newton-Raphson was one nice example, but there's a whole class of things that get closer and closer, reducing your errors as you go along.
逐渐逼近,牛顿迭代是一个很好的例子,随着你不断的时行下去,你会不断的离结果越来越近,逐渐地减少误差。
OK. I know. It looks boring. But it's a structure of the things I want to think about when I go through trying to take a problem and mapping it into a iterative program.
好,我知道这看起来有点无聊,但是这是我要去以迭代的方式,去解决一个问题采取的,措施的大致结构。
I first tested it on the square root of 4 and in one iteration it found 2.
我首先测了下求4的平方根4,它只迭代了一次并返回了2。
If I want a copy of the string, I've got to whip out my week 1 skills of just iterating with a four loop from left to right and make copies of those characters.
我需要那个字符串的一个拷贝,我使用,第一周的技巧,从左到右做四次迭代,然后复制这4个字符。
We could do that; we could try iteratively deleting dominated strategies and see if that process converged.
可以尝试迭代剔除劣势策略,然后看看会不会趋向于某种结果
This should be something familiar from when we were deleting dominated strategies.
这和我们之前学过的,迭代剔除劣势策略类似
What you can do is you can iterate keys, which gives you the keys in the dictionary, and then you can choose them, but there's no guarantee in the order in which you get keys.
你能做的就是迭代得到键,这样就可以得到字典中,所有的键并进行选择,但是键的顺序,是没有保证的。
Once you hear that description, it's easy to write the code, in fact. This is a place where the recursive version of it is much easier to think about than the iterative one.
实际上一旦你听到了大致描述,就能很轻松的写出代码来,在这一点上应用递归来解决问题,比用迭代要容易理解多了。
Again, what I want you to see is, notice the characteristic of that.
这里面的特征,也就是迭代产生了两个小一号的问题。
We saw some quadratic algorithms, typically those are things with multiple nested loops, or iterative or recursive calls, where you're doing, say, a linear amount of time but you're doing it a linear number of times and so it becomes quadratic, and you'll see other polynomial kinds of algorithms.
我们看过一些平方算法,他们一般进行了多次嵌套循环,或者递归迭代调用,对一个线性操作调用线性次,这样就变成平方次了,以后你们能看到,一些多项式算法。
N But it's definitely not one and in fact it wasn't N in the case of Selection Sort because remember the algorithm we implemented on stage last week had me going back and forth across the stage selecting on iteration, the smallest person I can find, the smallest number and then putting them into place.
但在选择排序中,肯定不会是1,也不是,注意,上周我们在这儿,实现的算法中,反复地,迭代进行选择,选出最小的数,然后将其放在合适的位置。
Because the right way to do it is iteratively.
因为正确的作法是迭代。
Iterative Deletion of Dominated Strategies.
迭代剔除劣势策略
> So now that might not have felt like the fastest algorithm but think about what you could have done with that algorithm in each iteration, much like the phonebook up front here, you literally split that problem in two because on each iteration roughly half of you were sitting down and then another half and then another half.
虽然这并不是最快的算法,但如果把这种算法每次迭代,就像刚才查电话簿一样,你便将这个问题一分为二了,因为每一次迭代后只有一半坐下来,以此类推。
We're going to see variations of this, we're going to see a variation of it called recursion, a little later on, but for now we're just going to talk about how do we do iterations.
我们稍后会去,看它的变量,它的所谓的递归数的变量,但是现在我得先讲讲,怎么来实现迭代。
Which is iteration. Or loops.
也就是迭代,或者循环。
If I want to try and decide how to tackle a problem in an iterative matter, here the steps I'm going to go through. First thing I'm going to do, is I'm going to choose a variable that's going to count.
但是这就是我对它的观点,如果我试着去以迭代的方式,解决一个问题的话,下面是我应该采取的步骤,第一件要做的事情。
Again as I said this is my version of it, but you can see, every one of the examples we've used so far has that pattern to it.
形成了迭代的思想,我还是想用我的话,来表达表达,但是你们可以看到,我们讲过的每个例子都用到了这个模式。
So if we do the procedure of iteratively deleting dominated strategies, going back again, looking what's dominated, all that's left is 5 and 6.
如果我们按照这个程序,迭代剔除劣势,不断回头看看那些策略是劣势的,最终将只剩下立场5和6
So have I convinced you that there's something you can do with iterative deletion?
大家都理解,有关迭代剔除的内容了吧
We also saw in that numbers game last time that in some games, but by no means all games, in some games this process actually converges to a single choice.
我们同样可以发现,在某些博弈中,不是所有的博弈,迭代剔除劣势策略,最终会导致唯一的选择
So if on each iteration of merging I'm doing eight things or more generally, N. That then begs the question, how many levels of this tree are there actually?
可见对于合并我需要迭代8次,一般情况下是N,这取决于具体问题,那么在这棵树中一共有多少层呢?
And I'm going to show you an example in a 1 second, just to drive this home, but notice the characteristics. In the first two cases, the problem reduced by 1 at each step.
在前面两个例子里,每一部问题的规模缩小了,不管是迭代的还是递归的,这表明这个问题的复杂性可能是线性的。
And then I would reiterate this process using that as guess i, and do it again.
然后我会用这个猜想,继续迭代计算下去。
And the right way to do it is not starting at the top, it's starting at the bottom.
而且正确的迭代不是,从头开始而是从尾开始。
And along the way I'm keeping track of how many iterations at the end I'll print how many iterations I took, before I return the final guess.
提到的方法去运行,并且我会跟踪到底,进行了多少次迭代,并在最后返回结果之前,显示进行了多少次迭代。
应用推荐