@@ -0,0 +1,2656 @@
+'''Conway's Game of Life algorithm.%0AFor reference: http://en.wikipedia.org/wiki/Conway%2527s_Game_of_Life%0A'''%0A%0Aimport itertools%0Aimport random%0Aimport time%0A%0A%0Adef transition(state):%0A    '''Returns the next state after transitioning the %60state%60 of the current%0A    generation of cells as follows:%0A    - Any live cell with fewer than two live neighbours dies, as if caused by%0A      under-population.%0A    - Any live cell with two or three live neighbours lives on to the next%0A      generation.%0A    - Any live cell with more than three live neighbours dies, as if by%0A      overcrowding.%0A    - Any dead cell with exactly three live neighbours becomes a live cell, as%0A      if by reproduction.%0A    '''%0A    next_state = set()%0A    cells_to_consider = state %7C set(itertools.chain(*map(neighbors, state)))%0A    for cell in cells_to_consider:%0A        num_neighbors = sum(neighbor in state for neighbor in neighbors(cell))%0A        if num_neighbors == 3 or (num_neighbors == 2 and cell in state):%0A            next_state.add(cell)%0A    return next_state%0A%0A%0Adef neighbors(cell):%0A    i, j = cell%0A    return ((i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j + 1),%0A            (i + 1, j - 1), (i + 1, j), (i + 1, j + 1))%0A%0A%0Adef state_to_string(state, alive_char='o', dead_char=' '):%0A    rows = sorted(%7Bi for i, j in state%7D)%0A%0A    max_col = max(j for i, j in state)%0A    min_col = min(j for i, j in state)%0A%0A    top_left_point = str((rows%5B0%5D, min_col))%0A%0A    s = top_left_point + ' ' + ('-' * (max_col - len(top_left_point))) + '%5Cn'%0A%0A    prev_row = rows%5B0%5D%0A    for row in rows:%0A        s += '%5Cn' * (abs(row) - abs(prev_row) - 1)%0A        prev_row = row%0A        prev_col = 0%0A        for col in sorted(j for i, j in state if i == row):%0A            s += dead_char * (abs(col) - abs(prev_col) - 1)%0A            s += alive_char%0A            prev_col = col%0A        s += '%5Cn' %0A%0A    bot_right_point = str((rows%5B-1%5D, max_col))%0A    s += ('-' * (max_col - len(bot_right_point))) + ' ' + bot_right_point%0A    s += '%5Cn%5Cn'%0A    return s%0A%0A%0Adef generate_random_state(rows, cols, chance_of_cell_being_alive=0.1):%0A    state = set()%0A    for i, j in itertools.product(range(rows), range(cols)):%0A        if random.randrange(100) %3C (100 * chance_of_cell_being_alive):%0A            state.add((i, j))%0A    return state%0A%0A%0Aif __name__ == '__main__':%0A    import time%0A    try:%0A        rows = 20%0A        cols = 30%0A        state = generate_random_state(rows, cols, 0.5)%0A        prev_state = None%0A        while state != prev_state:%0A            print(state_to_string(state))%0A            prev_state = state%0A            state = transition(state)%0A            time.sleep(0.5)%0A    except KeyboardInterrupt:%0A        pass%0A

Add Python implementation of Conway's Game of Life