Skip to content

Commit fe4036a

Browse files
committed
Updating solutions
1 parent 48fdd05 commit fe4036a

File tree

9 files changed

+138
-171
lines changed

9 files changed

+138
-171
lines changed

soln/Cell2D.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,40 @@ def draw(self, **options):
5151
"""Draws the array."""
5252
draw_array(self.array, **options)
5353

54-
def animate(self, frames, interval=None, step=None):
54+
def animate(self, frames, interval=0.001, step=None):
5555
"""Animate the automaton.
5656
5757
frames: number of frames to draw
5858
interval: time between frames in seconds
59-
iters: number of steps between frames
59+
step: function to call between frames
6060
"""
6161
if step is None:
6262
step = self.step
6363

64+
plt.ion()
6465
plt.figure()
6566
try:
6667
for _ in range(frames - 1):
6768
self.draw()
68-
plt.show()
69-
if interval:
70-
sleep(interval)
69+
plt.pause(interval)
7170
step()
7271
clear_output(wait=True)
7372
self.draw()
7473
plt.show()
7574
except KeyboardInterrupt:
7675
pass
76+
finally:
77+
plt.ioff()
7778

7879

7980
def draw_array(array, **options):
80-
"""Draws the cells."""
81+
"""Draws the cells.
82+
83+
array: numpy array
84+
options: passed to plt.imshow
85+
86+
returns: plt.imshow object
87+
"""
8188
n, m = array.shape
8289
options = underride(
8390
options,

soln/Life.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main(script, *args):
5858
life.add_cells(n // 2 + 12, col, *lwss)
5959
life.add_cells(n // 2 + 26, col, *lwss)
6060
life.add_cells(n // 2 + 19, col, *bhep)
61-
life.animate(frames=100)
61+
life.animate(frames=1000)
6262
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
6363
plt.show()
6464

soln/LifeRabbits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def main(script, *args):
2525
m = 600
2626
life = Life(n, m)
2727
life.add_cells(n // 2, m // 2, *rabbits)
28-
life.animate(frames=100, interval=1)
28+
life.animate(frames=1000)
2929
plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)
3030
plt.show()
3131

File renamed without changes.

soln/chap06.ipynb

Lines changed: 123 additions & 163 deletions
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)