From b3b6692de8cbe6adee4098049cddad7d4517a6be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Sat, 23 Feb 2019 21:01:16 +0100 Subject: [PATCH] added exam ex13 --- exam/ex13/backend.py | 13 ++++++++----- exam/ex13/main.py | 11 +++++++---- exam/ex13/model.py | 5 ----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/exam/ex13/backend.py b/exam/ex13/backend.py index 283e74d..8f23221 100644 --- a/exam/ex13/backend.py +++ b/exam/ex13/backend.py @@ -35,9 +35,10 @@ def count_living_cells_around(model, i, j): cells_around = get_cells_around(model, i, j) living_cells_around = (cells_around & CELL_IS_ALIVE) == 1 living_cells_around[1][1] = False - _, counts = np.unique(living_cells_around, return_counts=True) - print(counts) - return counts[True] + unique, counts = np.unique(living_cells_around, return_counts=True) + if(True not in unique): + return 0 + return counts[np.where(unique == True)[0]] @@ -60,5 +61,7 @@ def after_tick_finished(model): def total_living_cells(model): living_cells = (model & CELL_IS_ALIVE) == 1 - _, counts = np.unique(living_cells, return_counts=True) - return counts[True] + unique, counts = np.unique(living_cells, return_counts=True) + if(True not in unique): + return 0 + return counts[np.where(unique == True)[0]] diff --git a/exam/ex13/main.py b/exam/ex13/main.py index bb56ed6..a4664ac 100644 --- a/exam/ex13/main.py +++ b/exam/ex13/main.py @@ -7,9 +7,9 @@ from executor import execute_tick def cells_alive_at_ticks(model, ticks): for i in range(ticks): - execute_tick(model) cell_count = total_living_cells(model) - yield i, cell_count + yield [i, cell_count] + execute_tick(model) plot_data = { 0.3 : ("r", "p=0.3") @@ -17,9 +17,12 @@ plot_data = { , 0.7: ("b", "p=0.7") } +handles = [] for p, (color, label) in plot_data.items(): model = prepare_model(100, p) - living_cells = np.array(list(cells_alive_at_ticks(model, 1000))) - plt.plot(living_cells[:, 0], living_cells[:, 1], color, label=label) + living_cells = np.array(list(cells_alive_at_ticks(model, 10))) + plot, = plt.plot(living_cells[:, 0], living_cells[:, 1], color, label=label) + handles.append(plot) +plt.legend(handles=handles) plt.show() diff --git a/exam/ex13/model.py b/exam/ex13/model.py index 80ebc84..135f07f 100644 --- a/exam/ex13/model.py +++ b/exam/ex13/model.py @@ -6,8 +6,3 @@ def prepare_model(n, p): """ return (np.random.rand(n, n) < p).astype(np.int8) - - - - -