# Решение на Игра на живот от Виктор Бечев

Обратно към всички решения

Към профила на Виктор Бечев

## Резултати

• 6 точки от тестове
• 0 бонус точки
• 6 точки общо
• 1 успешни тест(а)
• 0 неуспешни тест(а)

## Код

#!/usr/bin/env python3
import os
import time
from random import Random
from itertools import product
BOARD_SIZE = 48
def print_board(board, wait = False, sleep = False):
os.system(['clear','cls'][os.name == 'nt'])
print('=' * (BOARD_SIZE + 2), end = '|\n')
for row in board:
print(*['X' if x else ' ' for x in row], end = ' |\n', sep = '')
print([[x & 1 for x in row] for row in board])
input("Press enter to continue.")
def create_board(fill_rate = 0.3):
rand = Random()
return [[int(x and x != BOARD_SIZE + 1 and y and y != BOARD_SIZE + 1
and rand.normalvariate(0.5,0.2) < fill_rate) for x in range(0,BOARD_SIZE + 2)] for y in range(0,BOARD_SIZE + 2)]
def step_board(board):
"""The official solution to the second Python homework.
Note that this solution is not for real men.
Use this at your own risk!
I used 'for' so the function doesn't become too 'fat' with
list comperhensions.
"""
assistant_board = []
for row in range(1,49):
assistant_board.append([int(column and column != 49 and
for column in range(50)])
board[1:-1] = assistant_board
"""This is a function that decides whether a cell lives or dies.
In a certain sense it is the cell god.
And you are composed of cells.
Therefore it is advised that you treat it nicely!
More concretely it decides whether the cell from the matrix
'board' with coordinates 'row' and 'column' lives or dies.
The decision is tough, but someone must make it.
"""
_ = product([-1, 0, 1], [-1, 0, 1])
cell_consortium = [board[row+i][column+j] for i, j in _]
number_of_neighbours = sum(cell_consortium) - board[row][column]
return int(number_of_neighbours == 3 or (number_of_neighbours == 2 and
board[row][column]))
def step_board_for_real_men(board):
"""This is the single-expression solution for real men!
The function's line-wrapping is not very nice for the sake of...
"""
board[1:-1] = [[column and column != 49 and (sum(board[row+i][column+j]
for j in range(-1,2) for i in range(-1,2)) - board[row][column] == 3 or
(sum(board[row+i][column+j] for j in range(-1,2) for i in range(-1,2)) -
board[row][column] == 2 and board[row][column]))
for column in range(50)] for row in range(1,49)]
if __name__ == '__main__':
board = create_board()
while True:
step_board(board)
print_board(board)

## Лог от изпълнението

.
----------------------------------------------------------------------
Ran 1 test in 0.251s

OK

## История (1 версия и 0 коментара)

### Виктор обнови решението на 29.03.2012 18:13 (преди над 8 години)

+#!/usr/bin/env python3
+import os
+import time
+from random import Random
+from itertools import product
+
+BOARD_SIZE = 48
+
+def print_board(board, wait = False, sleep = False):
+ os.system(['clear','cls'][os.name == 'nt'])
+ print('=' * (BOARD_SIZE + 2), end = '|\n')
+ for row in board:
+ print(*['X' if x else ' ' for x in row], end = ' |\n', sep = '')
+
+ print([[x & 1 for x in row] for row in board])
+ input("Press enter to continue.")
+
+def create_board(fill_rate = 0.3):
+ rand = Random()
+ return [[int(x and x != BOARD_SIZE + 1 and y and y != BOARD_SIZE + 1
+ and rand.normalvariate(0.5,0.2) < fill_rate) for x in range(0,BOARD_SIZE + 2)] for y in range(0,BOARD_SIZE + 2)]
+
+def step_board(board):
+ """The official solution to the second Python homework.
+
+ Note that this solution is not for real men.
+ Use this at your own risk!
+
+ I used 'for' so the function doesn't become too 'fat' with
+ list comperhensions.
+
+ """
+ assistant_board = []
+ for row in range(1,49):
+ assistant_board.append([int(column and column != 49 and
+ for column in range(50)])
+ board[1:-1] = assistant_board
+
+ """This is a function that decides whether a cell lives or dies.
+
+ In a certain sense it is the cell god.
+ And you are composed of cells.
+ Therefore it is advised that you treat it nicely!
+
+ More concretely it decides whether the cell from the matrix
+ 'board' with coordinates 'row' and 'column' lives or dies.
+
+ The decision is tough, but someone must make it.
+
+ """
+ _ = product([-1, 0, 1], [-1, 0, 1])
+ cell_consortium = [board[row+i][column+j] for i, j in _]
+ number_of_neighbours = sum(cell_consortium) - board[row][column]
+ return int(number_of_neighbours == 3 or (number_of_neighbours == 2 and
+ board[row][column]))
+
+def step_board_for_real_men(board):
+ """This is the single-expression solution for real men!
+
+ The function's line-wrapping is not very nice for the sake of...