Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse

1 Generators  
Generator和list comprehension非常类似
Generators are a kind of iterator that are defined like functions. 

http://www.codeskulptor.org/#examples_generators.py

https://wiki.python.org/moin/Generators

generater function的函数体中必须写上yield, 能够写或者不写return。

Generators functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop.
Each time the next(), method is applied to the resulting generator, the code is run until the next yield expression, and the yield expression's value is returned from that method call. Thus, the creation of the generator does not wait for all of its elements to be generator, and the generator could even represent an infinite number of elements.

# A list comprehension
print "max in list:", max([num * 2 - 3 for num in range(7)])

# A generator expression
print "max in gen:", max(num * 2 - 3 for num in range(7))

# A generator function
def genfunc(limit):
    num = 0
    while num < limit:
        yield num  #generator function一般和iteration搭配使用。yield说明这个值产生了。放在一边。如今循环继续进行,等循环结束了,再回来处理之前yield产生的值。
        num = num + 1

print genfunc(7)
# Iteration using a generator function
print "Iterate over generator:"
for number in genfunc(7):
    print number


2 stack and queue

stack http://www.codeskulptor.org/#user36_ZHLkI0d7kb_2.py

queue  http://www.codeskulptor.org/#user35_AtoP6ttM6w_0.py




3 inheritance
http://www.codeskulptor.org/#examples_inheritance.py
http://www.codeskulptor.org/#examples_inheritance2.py


4 grid collision
http://www.codeskulptor.org/#poc_physics_quadratic.py
https://class.coursera.org/principlescomputing-001/wiki/view?

page=fun_growth


https://class.coursera.org/principlescomputing-001/wiki/view?

page=grids

Principle of Computing (Python)学习笔记(5) BFS Searching + Zombie Apocalypse


5 grid类的实现  
http://www.codeskulptor.org/#poc_grid.py 
注意当中的”def get_index(self, point, cell_size):”的转化方法,把实际的screen position转化为了index

6 Conway’s game of life 模拟
game of life 简单介绍 http://en.wikipedia.org/wiki/Conway's_Game_of_Life
game of life 进行grid操作的练习 http://www.codeskulptor.org/#poc_gol_student.py

7 BFS
BFS动画 野火烧不尽 春风吹又生 
http://www.codeskulptor.org/#poc_wildfire_student.py
http://www.codeskulptor.org/#poc_wildfire_gui.py
http://www.codeskulptor.org/#poc_grid.py
BFS原理 https://class.coursera.org/principlescomputing-001/wiki/view?page=bfs

8 grid的用途 使用bucket sorting进行string sorting
https://class.coursera.org/principlescomputing-001/wiki/view?page=strings
http://www.codeskulptor.org/#poc_string_sort.py
# 产生26个字母组成的list
list= [ chr(ord("a") + char_num) for char_num in range(26)]
print list


9 stack and queue
老师实现的queue http://www.codeskulptor.org/#poc_queue.py
自己实现的stackhttp://www.codeskulptor.org/#user36_ZHLkI0d7kb_2.py

10 Zombie Apocalypse
http://www.codeskulptor.org/#poc_zombie_template.py
1)  Passable cells in the grid correspond to EMPTY cells while FULL cells are impassable
2)  However, several humans and zombies may inhabit the same grid cell. 

3) 注意用for each in list1仅仅能读取list1中的元素,不能改动list1中的元素,假设要改动的话。要使用下标操作。如

4) zombie和human移动的原理是这种。
首先对zombie计算一个distance_grid。这个distance_grid中的每一个cell都表达从当前cell到距离近期的一个zombie的距离,接下来,move_human的时候,就从human周围的cell中选一个distance_grid相应值最大的cell,这样human 就移动到了一个human最安全的点。

类似,对human计算一个distance_grid。中的每一个cell都表达从当前cell到距离近期的一个human的距离,接下来,move_zombie的时候。就从zombie周围的cell中选一个distance_grid相应值最小的cell,这样human 就移动到了一个最接近human的点。

5) 题目做了非常多简化。

比方计算distance_grid的时候,无论是zombie还是human,都假定当前cell的相邻cell仅仅有4个。而不是8个。

当zombie追上human的时候,仅仅是grid改变了颜色,假设下一步继续human move,human还是活着的。

for idx in range(len(list1)): 
    list1[idx] += 1 


我的作业